how to write arduino code, arduino code, arduino coding, arduino code writing
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:
how to write arduino code, arduino code, arduino coding, arduino code writing
  • 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:

Download Simulation & Code

Note:
  • We can also use Analog Pins as digital and that's what we are gonna do in today's tutorial.
  • So, that's why I have placed digital logic state which is actually acting as a button here.
  • Moreover, if you haven't worked with Virtual Terminal then you should read How to use virtual Terminal in Proteus.
  • 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.
  • But before going any further, you must first read Getting Started with Arduino Programming so that you also know the basics of Arduino Programming structure.
  • So, now I am going to make a small function which I will call in the Main loop function every time I need to blink the LED.
  • So, that lengthy code is now gonna compress to small code and is given below:
// ===== It's the First 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() 
{
    LedBlinkFunction(1);   
    delay(1000);
    LedBlinkFunction(2);   
    delay(1000);
    LedBlinkFunction(3);   
    delay(1000);
    LedBlinkFunction(4);   
    delay(1000);
    LedBlinkFunction(5);   
    delay(1000);
    LedBlinkFunction(6);   
    delay(1000);
    LedBlinkFunction(7);   
    delay(1000);
    LedBlinkFunction(8);   
    delay(1000);
    LedBlinkFunction(9);   
    delay(1000);
    LedBlinkFunction(10);   
    delay(1000);
    LedBlinkFunction(11);   
    delay(1000);
    LedBlinkFunction(12);   
    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()
{
    digitalWrite(Led1,  LOW);
    digitalWrite(Led2,  LOW);
    digitalWrite(Led3,  LOW);
    digitalWrite(Led4,  LOW);
    digitalWrite(Led5,  LOW);
    digitalWrite(Led6,  LOW);
    digitalWrite(Led7,  LOW);
    digitalWrite(Led8,  LOW);
    digitalWrite(Led9,  LOW);
    digitalWrite(Leda,  LOW);
    digitalWrite(Ledb,  LOW);
    digitalWrite(Ledc,  LOW);   
}
  • You can see the above code is totally different from the one we have used in Arduino LED Example.
  • In the above code I have created two functions named as LedBlinkFunction(int LedNo) and LedsOFF().
  • So, that way, I have made the code short as well as efficient.
  • So, now add this code in your Arduino sofware and Get your Arduino Hex File.
  • Upload this hex file in Proteus and if everything goes fine then you will get results as shown in below figure:
how to write arduino code, arduino code, arduino coding, arduino code writing
  • 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 !!! :)