Difference between revisions of "Tutorials/Arduino Projects/Mobile Robotics/BoeBot/Add an LED"

From ROBOTC API Guide
Jump to: navigation, search
(add another LED and program both to flash.)
 
Line 1: Line 1:
== Adding Another LED ==
+
{{tl|1|}}
=== Wiring the Second LED ===
+
== 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.
 
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.
  
Line 7: Line 7:
 
[[image:BoeBot_Breadboard_2_LEDs.png|thumb|c|center|300px|Breadboard setup for two LEDs]]
 
[[image:BoeBot_Breadboard_2_LEDs.png|thumb|c|center|300px|Breadboard setup for two LEDs]]
  
=== Programing Two Flashing 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.
 
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.
  
Line 32: Line 34:
 
<br />
 
<br />
 
=== Programming task main() ===
 
=== Programming task main() ===
Now that everything is configured, it is time to add the code to make the LEDs flash. 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. Since we already have code that turn led1 on for one second then turns it off for a second and repeats, lets use that as a starting point.
+
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   
 
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   
 
{|
 
{|

Revision as of 20:04, 6 June 2012

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
File:BoeBot Breadboard 2 LEDs.png
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);
  }
}