Adding a second LED to the Arduino

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 like the first LED, the second LED will be connected to digital pin 6. The circuit for the two LEDs will be constructed as depicted below.

Schematic for two LEDs
Schematic for two LEDs
Breadboard setup for two LEDs
Breadboard setup for two LEDs

Programming Two Flashing LEDs

In this case, we want to turn led1 on while keeping led2 off for a second, then turn led1 off and put led2 on for a second, and make this sequence repeat indefinitely.

Configuring ROBOTC

First, we need to create a new source code file and tell ROBOTC what type of Arduino you are using. Then we need to configure the pins. Open up the Motors and Sensors Setup window:

Opening the Motors and Sensors Setup window

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

Accept your changes 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(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, we will 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. Because 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 already controlling the one LED, it is very simple to add a second. 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(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);
  }
}