// declair pins for Relay controls
const int relay1 = 3;
const int relay2 = 5;
const int relay3 = 6;
const int relay4 = 9;
const int input10 = 10;
const int input11 = 11;

const int analogInPinA0 = A0; // Analog input pin that the potentiometer is attached to
int sensorValue0 = 0; // value read from the pot
int senValue0 = 0; // value read from the pot
const int analogInPinA1 = A1; // Analog input pin that the potentiometer is attached to
int sensorValue1 = 0; // value read from the pot
int senValue1 = 0; // value read from the pot
int switchState1=0;
int switchState2=0;

void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(input10, INPUT);
pinMode(input11, INPUT);
}
byte command;
byte run=0;

void loop() {
if (run==1){
digitalWrite(relay1, HIGH); // turn the relay1 on
checkSerial();
delay(100); // wait for a second
digitalWrite(relay2, HIGH); // turn the relay2 on
checkSerial();
delay(100); // wait for a second
digitalWrite(relay3, HIGH); // turn the relay3 on
checkSerial();
delay(100); // wait for a second
digitalWrite(relay4, HIGH); // turn the relay4 on
checkSerial();
delay(300); // wait for a second
digitalWrite(relay1, LOW); // turn the relay1 off
digitalWrite(relay2, LOW); // turn the relay2 off
digitalWrite(relay3, LOW); // turn the relay3 off
digitalWrite(relay4, LOW); // turn the relay4 off
checkSerial();
delay(200); // wait for a second
}
checkSerial();
}
void checkSerial(){
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
command = Serial.read();
// set the relay state that matches the command
switch (command) {
case 1:
digitalWrite(relay1, HIGH); // turn the relay1 on
break;
case 2:
digitalWrite(relay1, LOW); // turn the relay1 off
break;
case 3:
digitalWrite(relay2, HIGH); // turn the relay2 on
break;
case 4:
digitalWrite(relay2, LOW); // turn the relay2 off
break;
case 5:
digitalWrite(relay3, HIGH); // turn the relay3 on
break;
case 6:
digitalWrite(relay3, LOW); // turn the relay3 off
break;
case 7:
digitalWrite(relay4, HIGH); // turn the relay4 on
break;
case 8:
digitalWrite(relay4, LOW); // turn the relay4 off
break;
case 9:
digitalWrite(relay1, HIGH); // turn the relay1 on
digitalWrite(relay2, HIGH); // turn the relay2 on
digitalWrite(relay3, HIGH); // turn the relay3 on
digitalWrite(relay4, HIGH); // turn the relay4 on
break;
case 10:
digitalWrite(relay1, LOW); // turn the relay1 off
digitalWrite(relay2, LOW); // turn the relay2 off
digitalWrite(relay3, LOW); // turn the relay3 off
digitalWrite(relay4, LOW); // turn the relay4 off
break;
case 11:
run = 1;
break;
case 12:
run = 0;
break;
case 13:
sensorValue0 = analogRead(analogInPinA0);
senValue0 = map(sensorValue0, 0, 1023, 0, 255);
if (Serial.availableForWrite()){
Serial.write(senValue0);
}
delay(2); //wait for A2D to settle down for next value
break;
case 14:
sensorValue1 = analogRead(analogInPinA1);
senValue1 = map(sensorValue1, 0, 1023, 0, 255);
if (Serial.availableForWrite()){
Serial.write(senValue1);
}
break;
case 15:
switchState1 = digitalRead(input10);
if (Serial.availableForWrite()){
Serial.write(switchState1);
}
break;
case 16:
switchState2 = digitalRead(input11);
if (Serial.availableForWrite()){
Serial.write(switchState2);
}
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
}
return;
}