In our earlier posts we have seen the steps to create the software using Simulink and Stateflow for the LED Blinking program and deployed the software to the Arduino Board. In this article we will look at another way of generating the software and loading to the Arduino board. We will use Arduino IDE and C programming language to achieve the same functionality as the earlier post.
If you already doesn't have the Arduino IDE head to the following link and download the IDE.
https://www.arduino.cc/en/software
The following are the functions used in our C Program
- pinMode
- digitalWrite
- delay
Functions used in the LED Blinking software for Arduino
pinMode function helps to set the Arduino pins either in write or read mode. This function takes two inputs. First one is the Arduino pin number and the second input is the mode. When ever we need to send output from the Arduino we will use the OUTPUT as the mode and when we need to read from the Arduino pin we will use INPUT as the mode.
digitalWrite function helps to write the voltage value to the digital pin of the Arduino. This function takes two inputs. First one is the Arduino pin number and the second input is the voltage value. When ever we need to activate the digital output we use HIGH otherwise we will use LOW to set the Arduino pin to 0 volts.
delay function takes the number of milliseconds as the input and stops the program execution for the supplied milliseconds. For example if we need to wait for 2 seconds at the same state we will call delay function with 2000 as input.
Let employ the divide and conquer strategy and divide our program into smaller steps. Following are the steps we will follow to achieve the end goal.
Step 1: Set the pin mode in setup function
#define LED_PIN 9 void setup() { // put your setup code here, to run once: //Set the Arduino Digital Pin 9 Mode pinMode(LED_PIN, OUTPUT); }
Step 2: Define loop function
void loop() { // put your main code here, to run repeatedly: // Toggle the LED state digitalWrite(LED_PIN, HIGH); delay(500); digitalWrite(LED_PIN, LOW); delay(500); }Set the value of the digital pin to HIGH and LOW repeatedly for every 0.5 seconds. By default Arduino sketch comes up with loop function so we don't have to worry about creating our own loop. What ever coded in the loop function will be called in a cycle.
Complete Code for the LED Blinking Using Arduino and Arduino IDE
Now look at the complete code which forms our Arduino board software to Blink the LED.
#define LED_PIN 9 void setup() { // put your setup code here, to run once: //Set the Arduino Digital Pin 9 Mode pinMode(LED_PIN, OUTPUT); } void loop() { // put your main code here, to run repeatedly: // Toggle the LED state digitalWrite(LED_PIN, HIGH); delay(500); digitalWrite(LED_PIN, LOW); delay(500); }
No comments:
Post a Comment