In this example, we are going to do something exciting. We will use an Arduino program to turn the LED on and off by controlling the output voltage on one of micro-controllers GPIO pins. This helps us learn the key aspect of micro-controller. However, before going further, we need to understand basic structure of an Arduino program.
Structure of an Arduino Program
Arduino programs can be divided in three main parts: Structure, Values (variables and constants), and Functions. In this tutorial, we will learn about the Arduino software program, step by step, and how we can write the program without any syntax or compilation error.
Let us start with the Structure. Software structure consist of two main functions:
Setup( ) function
Loop( ) function
void setup ( ) {
}
The setup() function is called when a sketch starts. Use it to initialize the variables, pin modes, start using libraries, etc. The setup function will only run once, after each power up or reset of the board. If you turn the board upside down, there is a reset button which is used to reset the board.
void loop ( ) {
}
After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Xbot board.
Blinking LED Program
/** Blinking LED: The program below turns an LED on for one second, * then off for one second, and continuous endlessly. LED is connected to pin 10** At VEEROBOT, we invest time and resources providing this open source code,* Please support VEEROBOT and open-source hardware by purchasing products* from us @ http://veerobot.com* -----------------------------------------------------------------------------* You are free to redistribute it and/or modify it under the terms of the GNU* Lesser General Public License as published by the Free Software Foundation,* either version 3 of the License, or (at your option) any later version.** This Code is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU Lesser General Public License for more details.** See <http://www.gnu.org/licenses/>*/int led =10; // LED is connected to Pin 10voidsetup() { // Initialize digital pin as outputpinMode(led, OUTPUT);}// the loop function runs over and over again forevervoidloop() {digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)delay(1000); // wait for a seconddigitalWrite(led, LOW); // turn the LED off by making the voltage LOWdelay(1000); // wait for a second}
Type or Copy/Paste the above program into your Arduino IDE, Compile and upload to your micro:Xbot board. Once uploaded, you should see the LED blink every one second. Modify the delay to change the blinking frequency.
The above code can be modified to include our config.h file we created in "Pin Mapping" section so that we don't have to define the pin number in our program, and also set a variable for the blinking frequency. Note that you need to create a config.h file and copy the contents created before.
/** Blinking LED: The program below turns an LED on for one second, * then off for one second, and continuous endlessly. LED is connected to pin 10** At VEEROBOT, we invest time and resources providing this open source code,* Please support VEEROBOT and open-source hardware by purchasing products* from us @ http://veerobot.com* -----------------------------------------------------------------------------* You are free to redistribute it and/or modify it under the terms of the GNU* Lesser General Public License as published by the Free Software Foundation,* either version 3 of the License, or (at your option) any later version.** This Code is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU Lesser General Public License for more details.** See <http://www.gnu.org/licenses/>*/#include"config.h"voidsetup() { // Initialize digital pin as outputpinMode(LED_BUILTIN, OUTPUT);}// the loop function runs over and over again forevervoidloop() {digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)delay(1000); // wait for a seconddigitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOWdelay(1000); // wait for a second}
All the sketches are available on our GitHub repository. You can download the complete package and upload the programs. Clone the below repository and follow the tutorials along.