Arduino: toggling leds through SERIAL monitor

ok here is a sample code which¬†receives¬†data from serial and toggles devices to test it you can plug in led’s to test it,

//for keeping the current status of leds
int status1 = ;
int status2 = ;
int status3 = ;
int status4 = ;
//leds connected to Arduino int pin1 = 2;
int pin2 = 4;
int pin3 = 6;
int pin4 = 8;
void setup() {
	//open serial ports
	Serial.println("Serial Ready.");
	//open led ports
	pinMode(pin1, OUTPUT);
	pinMode(pin2, OUTPUT);
	pinMode(pin3, OUTPUT);
	pinMode(pin4, OUTPUT);
void loop() {
	//keep the incoming byte, in readByte
	int readByte;
	boolean readSomething = false;
	//read serial data
	while(Serial.available() > ){
		readByte =;
		readSomething = true;
	//if something was read toggle the selective device
		readSomething = false;
		if(readByte==1){ status1^=1; digitalWrite(pin1,status1);}
		if(readByte==2){ status2^=1; digitalWrite(pin2, status2);}
		if(readByte==3){ status3^=1; digitalWrite(pin3, status3);}
		if(readByte==4){ status4^=1; digitalWrite(pin4, status4);}
	}// status^=1 is xoring if its 0 it will be 1 and if its 1 it will be 0..

comments powered by Disqus