In our earlier post we have seen creating the software for Arduino using MATLAB and Stateflow. 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 and Initial state in setup function
#define Red_LED 3 #define Green_LED 4 #define Yellow_LED 5 void setup() { // put your setup code here, to run once: // Set the pin mode for arduino digital pins. pinMode(Red_LED, OUTPUT); pinMode(Green_LED, OUTPUT); pinMode(Yellow_LED, OUTPUT); // Set the initial state for the digital pins. digitalWrite(Red_LED, LOW); digitalWrite(Green_LED, LOW); digitalWrite(Yellow_LED, LOW); }
Step 2: Define loop function
void loop() { // put your main code here, to run repeatedly: // Turn on Green LED for two seconds digitalWrite(Green_LED, HIGH); delay(2000); digitalWrite(Green_LED, LOW); // Turn on Yellow LED for one seconds digitalWrite(Yellow_LED, HIGH); delay(1000); digitalWrite(Yellow_LED, LOW); // Turn on Red LED for two seconds digitalWrite(Red_LED, HIGH); delay(2000); digitalWrite(Red_LED, LOW); }
We have set the Arduino Digital pin connected to the Green LED to high and called the delay function with 2000. The effect is Arduino sets the digital pin 4 to high and waits for two seconds to execute next code line so the Green LED turns on and stays at the state for two seconds and then the next code line is executed that sets the digital pin 4 to low so the Green LED turns off.
The same concept is applied for digital pin 5 and 6 which are connected to the Yellow LED and Red LED respectively. So the complete code forms a state machine which turns on and off each LED one after another.
Complete Code for the LED's Sequential Lighting Using Arduino
#define Red_LED 3 #define Green_LED 4 #define Yellow_LED 5 void setup() { // put your setup code here, to run once: // Set the pin mode for arduino digital pins. pinMode(Red_LED, OUTPUT); pinMode(Green_LED, OUTPUT); pinMode(Yellow_LED, OUTPUT); // Set the initial state for the digital pins. digitalWrite(Red_LED, LOW); digitalWrite(Green_LED, LOW); digitalWrite(Yellow_LED, LOW); } void loop() { // put your main code here, to run repeatedly: // Turn on Green LED for two seconds digitalWrite(Green_LED, HIGH); delay(2000); digitalWrite(Green_LED, LOW); // Turn on Yellow LED for one seconds digitalWrite(Yellow_LED, HIGH); delay(1000); digitalWrite(Yellow_LED, LOW); // Turn on Red LED for two seconds digitalWrite(Red_LED, HIGH); delay(2000); digitalWrite(Red_LED, LOW); }
I hope you enjoyed the LED's sequential lighting demo in this article and in our next article we will explore the potentiometer interfacing with Arduino.
No comments:
Post a Comment