Making an LED flash
To demonstrate how to use an LED with the Arduino on a VEX robot, we are going to have the Arduino make the LED flash.
|
NOTE: This is a place holder for a video.
Please add the id for the YouTube video with description "VEX + Arduino Flashing LED."
Configuring ROBOTC
Before we can do anything, we need to create a new source file in ROBOTC.Once we have done that you need to tell ROBOTC what type of Arduino you are using. In this tutorial, we will be showing you how to use the Arduino UNO. To specify the board go to Robot > Platform Type > Arduino Types > Standard Arduino then click on Arduino UNO.
Now that ROBOTC knows what board you are using, it is time to configure the pins on the Arduino. To do this, open the Motors and Sensors Setup window by opening the Robot drop-down menu and clicking on Motors and Sensors Setup.
We are not using a shield with the VEX robot, so there is no need to configure one.
Now we want to tell ROBOTC to configure digital pin 5 as a Digital Output and name it "led1". To do this, we navigate to the Digital 0-13 tab. In the text field next to "Dgtl5" type in "led1", then select Digital Out from the drop down list next to the text field.
Now that everything is configured, click OK. The Motors and Sensors Setup window will close and your file should contain the following code at the top.
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, dgtl5, led1, sensorDigitalOut) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// |
Programming task main()
Now that the configuration is done, it is time to add the code to make the LED flash. To do this, we want the Arduino to set the output on digital pin 5 to high, pause for a moment, then set the output to low, pause again, then repeat. The first step in any code is to crate a task main() to run all of the commands.
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, dgtl5, led1, sensorDigitalOut) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { } |
Now we want to command the pin to be set on High (turning the LED on) and pause for a second. To do this we use the following code:
//turn the LED on SensorValue[led1] = true; //wait 1 second wait1Msec(1000); |
NOTE: In ROBOTC and most other programming languages, true = 1. So the above code is setting the LED pin to 1 which is high (5V).
We now need to turn the LED off, then pause again. By setting the value to false instead of true in the above code, the pin will go back to Low, and the LED will turn off. Using that information, the following code will turn the LED on for one second, then turn it off and wait another second before continuing.
//turn the LED on SensorValue[led1] = true; //wait 1 second wait1Msec(1000); //turn the LED off SensorValue[led1] = false; //wait 1 second wait1Msec(1000); |
NOTE: In ROBOTC and most other programming languages, false = 0. So the above code is setting the LED pin to 0 which is low (0V).
We want this to happen indefinitely, so we surround the previous code with a while(true) loop. Insert the code into the task main() and you will end up with the program:
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, dgtl5, led1, sensorDigitalOut) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { while(true) //repeat indefinitely { //turn the LED on SensorValue[led1] = true; //wait 1 second wait1Msec(1000); //turn the LED off SensorValue[led1] = false; //wait 1 second wait1Msec(1000); } } |
The While loop is a command that tells the program to repeatedly execute a block of code while some condition is true. Since the condition we provided, "true", is always true, the program will never stop running the block of code in the brackets.


