Hello everyone, I hope you all are fine and having fun. In today's tutorial, I am going to show you How to write Arduino code. In the previous tutorial, we have seen the Simple Arduino LED Example so if you haven't read that tutorial then I must suggest you to read it first because I am gonna use the same simulation with some advancements in it.
Moreover, you should also have a look at How to do Arduino Serial Communication because we are also gonna use Serial Port in today's tutorial and one more tutorial which you must read is How to use digitalRead in Arduino because we are dealing with digital pins here. So, I hope that you have read those tutorial and are ready to learn How to write Arduino code. So, let's have a look at How to write Arduino Code:
How to write Arduino code ?
In the previous tutorial named as Arduino LED Example, we have designed a simulation in which I have made some changes and added few buttons in it.
This new modified simulation is shown in below figure:
You can see in the above figure that I have connected LEDs with all the digital Pins and Logic State with all the analog pins and a Virtual Terminal is connected with Pin # 0 and 1 of Arduino.
You can download the complete simulation with Proteus code for this tutorial How to write Arduino Code by clicking the below button:
If you have a look at the previous code then you must have remembered that it was quite lengthy and I have mentioned that we will make it efficient in the next tutorial.
So, now let's make a small code in which we will blink these LEDs one by one.
Upload this hex file in Proteus and if everything goes fine then you will get results as shown in below figure:
The abovecode is quite small as compared to the previous one but let's make it more short and efficient.
Now, I am gonna use the For Loop which I haven't used before and that way I don't need to call that function every time instead I will just call it in For Loop so let's have a look at the below code:
// ===== It's the Second Version ===========
int Led1 = 13;
int Led2 = 12;
int Led3 = 11;
int Led4 = 10;
int Led5 = 9;
int Led6 = 8;
int Led7 = 7;
int Led8 = 6;
int Led9 = 5;
int Leda = 4;
int Ledb = 3;
int Ledc = 2;
int Led = 0;
void setup()
{
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Led4, OUTPUT);
pinMode(Led5, OUTPUT);
pinMode(Led6, OUTPUT);
pinMode(Led7, OUTPUT);
pinMode(Led8, OUTPUT);
pinMode(Led9, OUTPUT);
pinMode(Leda, OUTPUT);
pinMode(Ledb, OUTPUT);
pinMode(Ledc, OUTPUT);
}
void loop()
{
for(int x = 1; x < 13; x++)
{
LedBlinkFunction(x);
delay(1000);
}
LedsOFF();
delay(1000);
}
void LedBlinkFunction(int LedNo)
{
if(LedNo == 1){Led = Led1;}
if(LedNo == 2){Led = Led2;}
if(LedNo == 3){Led = Led3;}
if(LedNo == 4){Led = Led4;}
if(LedNo == 5){Led = Led5;}
if(LedNo == 6){Led = Led6;}
if(LedNo == 7){Led = Led7;}
if(LedNo == 8){Led = Led8;}
if(LedNo == 9){Led = Led9;}
if(LedNo ==10){Led = Leda;}
if(LedNo ==11){Led = Ledb;}
if(LedNo ==12){Led = Ledc;}
digitalWrite(Led, HIGH);
}
void LedsOFF()
{
for(int x = 2; x < 13; x++)
{
digitalWrite(x, LOW);
}
}
Now, you can see in the above code that I have used the For Loop in Main Loop function as well as in LedsOFF() Function.
And you can see the code has become quite small and more understanding.
The result of this code is exactly the same as the First Code.
Now let's have a look at those switches, I will design another code in which I will add different LEDs routines on each button press.
Like if you press the first button then it will start from top and if you press the second button then it will start from bottom and similar functions on other buttons.
So, here's the code which you need to add in your Arduino:
int Led1 = 13;
int Led2 = 12;
int Led3 = 11;
int Led4 = 10;
int Led5 = 9;
int Led6 = 8;
int Led7 = 7;
int Led8 = 6;
int Led9 = 5;
int Leda = 4;
int Ledb = 3;
int Ledc = 2;
int Led = 0;
int Button1 = A0;
int Button2 = A1;
int Button3 = A2;
int Button4 = A3;
int Button5 = A4;
int Button6 = A5;
void setup()
{
Serial.begin(9600);
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Led4, OUTPUT);
pinMode(Led5, OUTPUT);
pinMode(Led6, OUTPUT);
pinMode(Led7, OUTPUT);
pinMode(Led8, OUTPUT);
pinMode(Led9, OUTPUT);
pinMode(Leda, OUTPUT);
pinMode(Ledb, OUTPUT);
pinMode(Ledc, OUTPUT);
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
pinMode(Button4, INPUT_PULLUP);
pinMode(Button5, INPUT_PULLUP);
pinMode(Button6, INPUT_PULLUP);
}
void loop()
{
if(digitalRead(Button1) == HIGH)
{
for(int x = 1; x < 13; x++)
{
LedBlinkFunction(x);
delay(1000);
}
LedsOFF();
delay(1000);
}
if(digitalRead(Button2) == HIGH)
{
for(int x = 13; x > 0; x--)
{
LedBlinkFunction(x);
delay(1000);
}
LedsOFF();
delay(1000);
}
if(digitalRead(Button3) == HIGH)
{
LedsON();
delay(1000);
LedsOFF();
delay(1000);
}
if(digitalRead(Button4) == HIGH)
{
Serial.print("www.TheEngineeringProjects.com");
while(digitalRead(Button4) == HIGH);
}
if(digitalRead(Button5) == HIGH)
{
if(Serial.available())
{
char data = Serial.read();
data = data - 49;
digitalWrite(data, HIGH);
}
}
if(digitalRead(Button5) == HIGH)
{
}
}
void LedBlinkFunction(int LedNo)
{
if(LedNo == 1){Led = Led1;}
if(LedNo == 2){Led = Led2;}
if(LedNo == 3){Led = Led3;}
if(LedNo == 4){Led = Led4;}
if(LedNo == 5){Led = Led5;}
if(LedNo == 6){Led = Led6;}
if(LedNo == 7){Led = Led7;}
if(LedNo == 8){Led = Led8;}
if(LedNo == 9){Led = Led9;}
if(LedNo ==10){Led = Leda;}
if(LedNo ==11){Led = Ledb;}
if(LedNo ==12){Led = Ledc;}
digitalWrite(Led, HIGH);
}
void LedsOFF()
{
for(int x = 2; x < 14; x++)
{
digitalWrite(x, LOW);
}
}
void LedsON()
{
for(int x = 2; x < 14; x++)
{
digitalWrite(x, HIGH);
}
}
Now when you upload the code, you will get the similar results but now you must press the buttons and you will see different functionalities of LEDs.
The results are given in the below video:
I hope you have enjoyed How to write Arduino code and are gonna get help from it. That's all about How to write Arduino code. So, will meet you guys in the next tutorial. Take care and have fun !!! :)
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ How to write Arduino code ? ‘’ ( 0 )