// define pin D7 and D8 to control the two relays int relay[2] = {7, 8}; int MAX_RELAYS = 2; // for the incoming serial data int incomingByte = 0; int togglePin = 0; void setup() { // initialize the digital pins as output for (int index = 0; index < MAX_RELAYS; index++) { pinMode(relay[index], OUTPUT); digitalWrite(relay[index], HIGH); } // use the serial port with 9600 bps Serial.begin(9600); } void loop() { // wait for data on the serial line if (Serial.available() > 0) { // read the incoming byte incomingByte = Serial.read(); // print received character as decimal value Serial.print("I received: "); Serial.println(incomingByte, DEC); togglePin = 2; // enable or disable the relay(s) if (incomingByte == 49) { togglePin = 1; } else if (incomingByte == 50) { togglePin = 0; } if (togglePin != 2) { for (int index = 0; index < MAX_RELAYS; index++) { pinMode(relay[index], OUTPUT); digitalWrite(relay[index], togglePin); } } } }