Tutorials/Arduino Projects/Mobile Robotics/BoeBot/Add an LED

From ROBOTC API Guide
Jump to: navigation, search

Wiring the Second LED

The wiring for the second LED is almost identical to the first. The only difference is that instead of connecting to digital pin 5 line the first LED, the second will be connected to digital pin 6. The circuit for the two LEDs will be constructed as depicted below.

Schematic for two LEDs
Breadboard setup for two LEDs

Programing Two Flashing LEDs

In this case we want to have led1 on and led2 off for a second then have led1 off and led2 on for a second, and have this repeat.

Configuring ROBOTC

First create a new source code file and tell ROBOTC what type of Arduino you are using. Now we need to configure the pins. Open up the Motors and Sensors Setup window and tell ROBOTC that you have a Parallax Boe Bot Shield connected to the Arduino.

Opening the Motors and Sensors Setup window
Parallax Boe Bot Sheild configured

Now tell ROBOTC to configure digital pins 5 and 6 both as a Digital Out. Give pin 5 the name "led1" and give pin 6 the name "led2".

Digital pins 5 and 6 configured

Now submit by clicking OK. The Motors and Sensors Setup window will close and your source code file should contain the following code.

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


Programming task main()

Now that everything is configured, it is time to add the code to make the LEDs flash. Since we already have code that turns led1 on for one second then turns it off for a second and repeats, lets use that as a starting point. Since the code already handles led1, we need to add some code to turn led2 on and off. Based on the work for one LED, we know that

SensorValue[led2] = true;

will turn led2 on and that

SensorValue[led2] = false;

will turn it off. Since we want both LEDs to change at the same time, we want those commands as close, instruction-wise, as possible. Since the existing code is just controlling the one LED, it is very simple. We just insert the led2 control command after the led1 command but before the pause, and we end up with

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl5,  led1,           sensorDigitalOut)
#pragma config(Sensor, dgtl6,  led2,           sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  while(true) //repeat indefinitely
  {
    //turn led1 on
    SensorValue[led1] = true;
 
    //turn led2 off
    SensorValue[led2] = false;
 
    //wait 1 second
    wait1Msec(1000);
 
    //turn the led1 off
    SensorValue[led1] = false;
 
    //turn led2 on
    SensorValue[led2] = true;
 
    //wait 1 second
    wait1Msec(1000);
  }
}