World of Electronics and Cyber Consultancy

Create a Backup Arduino For Your Project! Full Code & Lab

For this tutorial, you’ll need:

  1. 2 x Arduino UNO
  2. 4 x Resistor 330E
  3. BreadBoard
  4. Jumper Wires
  5. 4 x Led 5MM
  6. 2 x DIP Switch 6 pole

Watch this full video:

This video is sponsored by C.B.Electronics & Produced by Lebanese Park

First Version of Code:

#define Working 12
#define led1 10
#define led2 9
#define led3 8


int led=0;


void setup() {
  // put your setup code here, to run once:


  for (int i=led3; i<=led1; i++)
  {
    pinMode(i, OUTPUT);
  }
  pinMode(Working, OUTPUT);
  
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led1, LOW);
  
  digitalWrite(Working, HIGH);


}


void loop() {
  // put your main code here, to run repeatedly:


  switch(led){
    case 0:
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led1, HIGH);
      delay(1000);
      led++;
      break;


    case 1:
      digitalWrite(led1, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led2, HIGH);
      delay(1000);
      led++;
      break;


    case 2:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, HIGH);
      delay(1000);
      led=0;
      break;


    default:
      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      break;
  }


}

Second Version of Code (First Arduino):

#include <SoftwareSerial.h>
SoftwareSerial s(5,6); // (Rx, Tx)


#define Working 12
#define led1 10
#define led2 9
#define led3 8


int led=0;


int availability =0;


void check_if_available(int );


void setup() {
  s.begin(9600);
  delay(1000);
  
  for (int i=led3; i<=led1; i++)
  {
    pinMode(i, OUTPUT);
  }
  pinMode(Working, OUTPUT);
  
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led1, LOW);


  check_if_available(10);  




}


void loop() {
  // put your main code here, to run repeatedly:


  switch(led){
    case 0:
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led1, HIGH);
      delay(1000);
      check_if_available(0);  
      led++;
      break;


    case 1:
      digitalWrite(led1, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led2, HIGH);
      delay(1000);
      check_if_available(1);
      led++;
      break;


    case 2:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, HIGH);
      delay(1000);
      check_if_available(2);
      led=0;
      break;


    default:
      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      check_if_available(100);
      break;
  }


}


void check_if_available(int a)
{
  s.write(a); //Hello
  delay(10);


  if(s.available()>0) //check if there is incoming data
  {
      int data = s.read();


    if(data==11) //Hello back
    {
        digitalWrite(Working, HIGH);
        return 1;
    }
    if((data==0)||(data==1)||(data==2)) //Data Received
    {
        digitalWrite(Working, HIGH);
        return 1;
    }
    else
    {
        digitalWrite(Working, LOW);
        return 0;
    }
  }
  else
  {
        digitalWrite(Working, LOW);
        return 0;
  }
}

Second Version of Code (Backup Arduino):

#include <SoftwareSerial.h>
SoftwareSerial s(6,5); //(RX, TX)


int unavailable=0;
int state=0;
int received_data=0;


#define Working 13
#define led1 10
#define led2 9
#define led3 8


int led=0;
int counter=0;
int time1=1000;




void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  s.begin(9600);
  delay(1000);


  for (int i=led3; i<=led1; i++)
  {
    pinMode(i, OUTPUT);
  }
  pinMode(Working, OUTPUT);
  
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led1, LOW);
  digitalWrite(Working, LOW);


  if(s.available()>0)
  {
    if(s.read()==10)
    {
      s.write(11); //Hello back
    }
  }




}


void loop() {


while (unavailable!=1)
{
  if(s.available()>0) //check if there is incoming data
  {
    received_data=s.read();
    if(received_data==10) //Hello
    {
        s.write(11); //Hello back
        unavailable =0;
        counter=0;
    }
    if((received_data==0)||(received_data==1)||(received_data==2))
    {
        s.write(received_data);
        state=received_data;
        unavailable=0;
        counter =0;
    }
  }
  else
  {
    counter = counter +1;
    delay(500);
    if(counter ==30)
    {
      unavailable =1;
    }
  }
}


if(unavailable ==1)
{
    digitalWrite(Working, HIGH);


  switch(state){
    case 0:
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led1, HIGH);
      delay(1000);
      state++;
      break;


    case 1:
      digitalWrite(led1, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led2, HIGH);
      delay(1000);
      state++;
      break;


    case 2:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, HIGH);
      delay(1000);
      state=0;
      break;


    default:
      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      break;
  }


}


}