Making a program start using a switch

From ROBOTC API Guide
< Tutorials‎ | Arduino Projects/Mobile Robotics/VEX
Revision as of 12:58, 10 August 2012 by Dwest (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Wiring

The wiring for this lesson is virtually the same as for the previous lesson, but without the LED.

schematic diagram for the program start switch
Virtual breadboard layout
breadboard layout

Configuring ROBOTC

We need to tell ROBOTC that we have the drive servos connected to pins 10 and 11. We also need to tell ROBOTC that pin 2 is a digital input (the start switch) and name it "startSwitch".

configuring the pin for the start switch

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

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  startSwitch,    sensorDigitalIn)
#pragma config(Motor,  motor_11,        LeftServo,     tmotorServoContinuousRotation, openLoop, IOPins, dgtl10, None)
#pragma config(Motor,  servo_10,        RightServo,    tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl11, None)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//


Programming

For this project we want to demonstrate how the switch 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] = 50;   //Set the left servo to go forward at power level 50
    motor[rightServo] = 50;  //Set the right servo to go forward at power level 50
    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] = -50;  //Set the left servo to go backwards at power level -50 (absolute power of 50)
    motor[rightServo] = -50; //Set the right servo to go backwards at power level -50 (absolute power of 50)
    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 switch we use the command

SensorValue[startSwitch]

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

while(SensorValue[startSwitch] == 1)
{
}

The empty while loop serves as a pause until the switch has been pressed. Basically the while loop continuously checks the switch, and as long as it is not pressed, it will run what ever is in the loop, which in this case is nothing. The "==" means 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 switch.

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  startSwitch,    sensorDigitalIn)
#pragma config(Motor,  motor_11,        LeftServo,     tmotorServoContinuousRotation, openLoop, IOPins, dgtl10, None)
#pragma config(Motor,  servo_10,        RightServo,    tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl11, None)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
 
  while(true) //repeat indefinitely
  {
    //wait for start switch to be pressed
    while (SensorValue[startSwitch] == 1)
    {
    }
 
    //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] = 50;   //Set the left servo to go forward at power level 50
    motor[rightServo] = 50;  //Set the right servo to go forward at power level 50
    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] = -50;  //Set the left servo to go backwards at power level -50 (absolute power of 50)
    motor[rightServo] = -50; //Set the right servo to go backwards at power level -50 (absolute power of 50)
    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 feel like being a little more independent you can create your own or use previous functions for the motor controls.