Starting a program using a Touch Sensor

From ROBOTC API Guide
Jump to: navigation, search

Contents

Video

LEGO + Arduino robot driving forwards and backwards after the button is pressed


Connecting the Touch Sensor

For this program and every program for the rest of the section, we want to have use of the Lego motors again. So, we're going to have to reconnect the motors to the breadboard and the motor shield. Their configuration and wiring is still the same, as is the touch sensor's; Once again, the motors will use the special blue motor ports, and the touch sensor will be wired into digital 2.


breadboard layout
Motor shield layout.
Touch sensor schematic with motors
Virtual breadboard layout
Virtual breadboard layout long view

Configuring ROBOTC

Since we have everything connected, we need to tell ROBOTC how to configure the pins before we can program. Like always, we need to open the Motors and Sensors Setup tab and change the controller board to DFRobot Motor Shield, and in the motors tab, input the drive servos' setup to pins 5 and 6. However, we also need to tell ROBOTC that pin 2 is a digital input (the touch sensor) and name it "startButton".

configuring the pin for the touch sensor

Once that is done you should have the following at the top of your source code file.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  startButton,    sensorTouch)
#pragma config(Motor,  motor_5,         rightServo,    tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl5, dgtl4)
#pragma config(Motor,  motor_6,         leftServo,     tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl6, dgtl7)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//


Programming

For this project we just want to demonstrate how the touch sensor can be used to start a program. So to keep it simple, we will just have it drive forwards then backwards and stop, until started again. Since we already have code to drive forwards and backwards, we will just copy that code into the current file and modify it.

task main()
{
 
  while(true) //repeat indefinitely
  {
    // code to drive forward for 2 seconds
    motor[leftServo] = 100;   //Set the left servo to go forward at power level 100
    motor[rightServo] = 100;  //Set the right servo to go forward at power level 100
    wait1Msec(2000);         //pause code execution for 2000ms (2 seconds)
 
 
    // code to stop the robot for 1 second
    motor[leftServo] = 0;    //Set the left servo stop
    motor[rightServo] = 0;   //Set the right servo stop
    wait1Msec(1000);         //pause code execution for 1000ms (1 second)
 
 
    // code to drive backwards for 2 seconds
    motor[leftServo] = -100;  //Set the left servo to go backwards at power level -100 (absolute power of 100)
    motor[rightServo] = -100; //Set the right servo to go backwards at power level -100 (absolute power of 100)
    wait1Msec(2000);         //pause code execution for 2000ms (2 seconds)
 
 
    // code to stop the robot for 1 second
    motor[leftServo] = 0;    //Set the left servo stop
    motor[rightServo] = 0;   //Set the right servo stop
    wait1Msec(1000);         //pause code execution for 1000ms (1 second)
  }
}

To read the status of the touch sensor we use the command

SensorValue[startButton]

Since we basically want the code execution to "stop" while the touch sensor is not pressed, we can use a while loop that runs while the touch sensor is not pressed.

while(SensorValue[startButton] == 0)
{
}

The empty while loop serves as a pause until the touch sensor has been pressed. Basically the while loop continuously checks the touch sensor, and as long as it is not pressed, it will run whatever is in the loop, which in this case is nothing. The "==" tells the program to check if the values to either side are equal. If the values are equal, then it will return true, otherwise it will return false.

Now by inserting that code block into the existing file we get the following code which waits to be started by pressing the start touch sensor.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  startButton,    sensorTouch)
#pragma config(Motor,  motor_5,         rightServo,    tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl5, dgtl4)
#pragma config(Motor,  motor_6,         leftServo,     tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl6, dgtl7)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
 
  while(true) //repeat indefinitely
  {
    //wait for start touch sensor to be pressed
    while (SensorValue[startButton] == 0)
    {
    }
 
    //wait a second so that you can get your finger out of the way
    wait1Msec(1000);
 
    // code to drive forward for 2 seconds
    motor[leftServo] = 100;   //Set the left servo to go forward at power level 100
    motor[rightServo] = 100;  //Set the right servo to go forward at power level 100
    wait1Msec(2000);         //pause code execution for 2000ms (2 seconds)
 
 
    // code to stop the robot for 1 second
    motor[leftServo] = 0;    //Set the left servo stop
    motor[rightServo] = 0;   //Set the right servo stop
    wait1Msec(1000);         //pause code execution for 1000ms (1 second)
 
 
    // code to drive backwards for 2 seconds
    motor[leftServo] = -100;  //Set the left servo to go backwards at power level -100 (absolute power of 100)
    motor[rightServo] = -100; //Set the right servo to go backwards at power level -100 (absolute power of 100)
    wait1Msec(2000);         //pause code execution for 2000ms (2 seconds)
 
 
    // code to stop the robot for 1 second
    motor[leftServo] = 0;    //Set the left servo stop
    motor[rightServo] = 0;   //Set the right servo stop
    wait1Msec(1000);         //pause code execution for 1000ms (1 second)
  }
}

Notepad.gif NOTE: If you're feeling adventurous and want to use what you've learned a bit more, you can create your own functions for the motor controls or use ones from previous lessons.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox