Making Arduino ON/OFF Pin Programs via serial

In this post, I will share the Arduino ON/OFF pin via serial communication.

Assemble the equipment as shown below:
- Arduino UNO
- LEDs: 2 pcs
- Resistor 330 Ohm : 2 pcs



Arduino program as follows:

 int led1 = 7;
 int led2 = 8;

 void setup() {   
   Serial.begin(9600);  
    
   pinMode(led1, OUTPUT); 
    pinMode(led2, OUTPUT); 
    
 }

 void loop() {
   char i= Serial.read();

   if(i=='a'){////////////////
     if(digitalRead(led1)){// if HIGH
   digitalWrite(led1, LOW); 
  }else if(!digitalRead(led1)// if LOW
   digitalWrite(led1, HIGH);  
 }

  if(i=='b'){////////////////
     if(digitalRead(led2)){////////////
   digitalWrite(led2, LOW);  ////////////
  }else if(!digitalRead(led2))//////////
   digitalWrite(led2, HIGH);  //////////
 }


 
}

Upload the program to Arduino.

Open Arduino Serial Monitor,





- type 'a' , Arduino will turn on the LED on pin 7
- If pin 7 is ON, then you type 'a' again then the led on pin 7 will be OFF.

- type 'b' , the Arduino will turn on the LED on pin 8
- If pin 8 is ON, then you type 'b' again then the led on pin 8 will be OFF.

Good luck guys...

Post a Comment

0 Comments