Touch sensor (button)

Technical specifications

  • Type: mechanical switch
  • Resistance: 100 Ω

Description

Buttons let a robot detect contact with a wall and can also be used to make a robot controller.

Button type 2 comes with a special perforated cap that can be attached to construction-kit parts. The sensor has an inverted output: voltage is present while the button is released and absent while it is pressed.

Use

Connection

The sensor uses a standard 3-pin connector and connects to any IN port on Trackduino.

Programming

The required block is called “button” and is in the “Sensors” block group.

Block and generated functionDescription
Button reading block

bool buttonRead(int port)
Returns true when the button is pressed.

Argument: int port — the port the sensor is connected to.

Project example

Trigger an alarm when the button is pressed

Generated code:

void setup()
{
  pinMode( IN4 , INPUT);
}

void loop()
{
  delay( 50 );
  if (buttonRead(IN4))
  {
    tone(OUT1, 1000);
    delay( 1000 );
    noTone(OUT1);
    delay( 1000 );
  }
  else
  {
    noTone(OUT1);
  }
}