Difference between revisions of "Tutorials/Arduino Projects/Mobile Robotics/Lego/Using a Touch Sensor to Light an LED"

From ROBOTC API Guide
Jump to: navigation, search
(Configuration)
(Programming the LED)
Line 168: Line 168:
 
|}
 
|}
  
{{FuncDetails|Control_Structures|if() else statements}}
+
{{FuncDetails|Control_Structures|if-else statements}}

Revision as of 18:51, 7 August 2012

ArduinoArduino Tutorials and Guided ProjectsLego + Arduino, Mobile Robotics Platform → Tutorials/Arduino Projects/Mobile Robotics/Lego/Using a Touch Sensor to Light an LED

Connecting the Touch Sensor

To connect the touch sensor to the Arduino, you will need the touch sensor, Three jumper wires, and a LEGO jack connector.

parts for connecting the touch sensor and LED

For this project, you will also need all of the parts required to light an LED: an LED, two wires, and a 470Ω resistor (color code: yellow-violet-brown).

LED parts

Once you have all the parts you need in order to connect the components, you need to wire the touch sensor to the Arduino using the breadboard. AN IN on the touch sensor should be connected to digital port two, and GRND and VBUS to the ground and 5V slots respectively. The LED, like usual, should be connected to digital port 5 on the cathode end, and the ground on the anode end.

Breadboard layout
Touch sensor schematic with LED
Virtual breadboard layout
Virtual breadboard layout including LED wiring

Notepad.gif NOTE: The virtual board may be slightly different from the physical one. This is because of some limitations of the program used. Both the physical and virtual layout will work.

Configuration

Open up the Motors and Sensors Configuration window and tab over to Digital 0-13. Go to dgtl2, rename it to "touchSensor" and change its type to "Touch." Then change dgtl5's name to "led1" and type to Digital Out.


Configuring the digital output

Once this is done, you should get a block of code from Robot C which looks something like this:

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  touchSensor,    sensorTouch)
#pragma config(Sensor, dgtl5,  led1,           sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

Programming the LED

Our objective in this lesson is to make the LED light up whenever the touch sensor is pressed. For starters, we want to keep this program going for an infinite duration, so we'll begin (after putting in task main () of course) with a while loop.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  touchSensor,    sensorTouch)
#pragma config(Sensor, dgtl3,  led1,           sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main ()
{
  while(true)
  {
    //code to run indefinitely 
  }
}

Now we need to find a way to determine the Robot's actions based on whether the touch sensor has been pressed or not. In order to do this, we will need an if() statement. As presented here in its basic form, an if() statement performs an action if its condition in parentheses is met.

if (/*conditional statement, for example x=1*/)
{
  //action taken if condition is true
}

If the condition is not met, the robot will simply skip over anything in the curly brackets following the if() statement.

So, to apply the if() statement to our current project:

if (SensorValue[touchSensor]==1)
{
  //action taken if condition is true
  SensorValue[led1]=true;
}

This means that whenever the sensor value = 1 (the condition), the LED will light up. So if we add it to the code we already have, we get:

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  touchSensor,    sensorTouch)
#pragma config(Sensor, dgtl3,  led1,           sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main ()
{
  while(true)
  {
    if (SensorValue[touchSensor]==1)
    {
      //action taken if condition is true
      SensorValue[led1]=true;
    }
  }
}


There is one small problem with this program, however. Once the LED is turned on, it will not turn back off, even if the button is released. So we need to add another statement, a complement to if() called else. An else statement attaches to the end of an if() statement and provides an action to take if the conditions are not met.

if (/*conditional statement, for example x=1*/)
{
  //action taken if condition is true
}
else
{
  //action taken if condition is not true
}

You can do more with else statements than just that, but for the purposes of this program, this use of else will suffice. When we add the else statement with an appropriate action (turning the LED off), our final program should look something like this:

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  touchSensor,    sensorTouch)
#pragma config(Sensor, dgtl5,  led1,           sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main ()
{
  while(true)
  {
    if (SensorValue[touchSensor]==1) //conditional statement, for example x=1
    {
      //action taken if condition is true
      SensorValue[led1]=true;
    }
    else
    {
      SensorValue[led1]=false;
    }
  }
}