// By Fahed Al Riachi - 2013 // Use to control 8 digital outputs ON/OFF and 2 PWM analog outputs from PC over Serial/USB port #include int val = 0; //serial command Servo myservo1; //servo1 object Servo myservo2; //servo2 object //Define Alarm pins, Total of 10 on/off int alarm1 = 2; int alarm2 = 3; int alarm3 = 4; int alarm4 = 5; int alarm5 = 6; int alarm6 = 7; int alarm7 = 8; int alarm8 = 10; //Define servos pins and related parameters, Total of 2 int servo1 = 9; int servo2 = 11; int servo1Pos = 5; // variable to store the servo1 position int servo2Pos = 5; // variable to store the servo2 position void setup() { Serial.begin(9600); //uncomment while loop for Arduino leonardo or Yun //while (!Serial) { // ; // wait for serial port to connect. Needed for Leonardo only //} //set all 12 digital i/o to OUTPUTS pinMode(alarm1, OUTPUT); pinMode(alarm2, OUTPUT); pinMode(alarm3, OUTPUT); pinMode(alarm4, OUTPUT); pinMode(alarm5, OUTPUT); pinMode(alarm6, OUTPUT); pinMode(alarm7, OUTPUT); pinMode(alarm8, OUTPUT); myservo1.attach(servo1); // attaches the servo1 on pin 9 to the servo1 object myservo2.attach(servo2); // attaches the servo1 on pin 11 to the servo1 object myservo1.write(servo1Pos); //initialize servo1 position myservo2.write(servo2Pos); //initialize servo2 position } void loop() { if (Serial.available()) { val = Serial.parseInt(); //Alarm 1: 10,11 if(val == 10) { digitalWrite(alarm1, LOW); Serial.println("OFF1"); } else if(val == 11) { digitalWrite(alarm1, HIGH); Serial.println("ON1"); } //Alarm 2: 20,21 else if(val == 20) { digitalWrite(alarm2, LOW); Serial.println("OFF2"); } else if(val == 21) { digitalWrite(alarm2, HIGH); Serial.println("ON2"); } //Alarm 3: 30,31 else if(val == 30) { digitalWrite(alarm3, LOW); Serial.println("OFF3"); } else if(val == 31) { digitalWrite(alarm3, HIGH); Serial.println("ON3"); } //Alarm 4: 40,41 else if(val == 40) { digitalWrite(alarm4, LOW); Serial.println("OFF4"); } else if(val == 41) { digitalWrite(alarm4, HIGH); Serial.println("ON4"); } //Alarm 5: 50,51 else if(val == 50) { digitalWrite(alarm5, LOW); Serial.println("OFF5"); } else if(val == 51) { digitalWrite(alarm5, HIGH); Serial.println("ON5"); } //Alarm 6: 60,61 else if(val == 60) { digitalWrite(alarm6, LOW); Serial.println("OFF6"); } else if(val == 61) { digitalWrite(alarm6, HIGH); Serial.println("ON6"); } //Alarm 7: 70,71 else if(val == 70) { digitalWrite(alarm7, LOW); Serial.println("OFF7"); } else if(val == 71) { digitalWrite(alarm7, HIGH); Serial.println("ON7"); } //alarm 8: 80,81 else if(val == 80) { digitalWrite(alarm8, LOW); Serial.println("OFF8"); } else if(val == 81) { digitalWrite(alarm8, HIGH); Serial.println("ON8"); } //servo1: 200-299 ==> 100 step to turn servo1 from full righ to full left else if(val >=200 && val <= 299) { servo1Pos = map(val,200,299,0,180); myservo1.write(servo1Pos); // tell servo1 to go to position in variable 'servo1Pos' delay(15); Serial.print("SRV1"); Serial.println(servo1Pos); } //servo2 300-399 ==> 100 step to turn servo2 from full righ to full left else if(val >=300 && val <= 399) { servo2Pos = map(val,300,399,0,180); myservo2.write(servo2Pos); // tell servo2 to go to position in variable 'servo1Pos' delay(15); Serial.print("SRV2"); Serial.println(servo2Pos); } else if (val == 1000) { Serial.print("Alarm1: pin"); Serial.print(alarm1); Serial.print(";Alarm2: pin"); Serial.print(alarm2); Serial.print(";Alarm3: pin"); Serial.print(alarm3); Serial.print(";Alarm4: pin"); Serial.print(alarm4); Serial.print(";Alarm5: pin"); Serial.print(alarm5); Serial.print(";Alarm6: pin"); Serial.print(alarm6); Serial.print(";Alarm7: pin"); Serial.print(alarm7); Serial.print(";Alarm8: pin"); Serial.print(alarm8); Serial.print(";Servo1: pin"); Serial.print(servo1); Serial.print(";Servo2: pin"); Serial.print(servo2); Serial.println(";"); } else Serial.println("UNKNOWN_CMD"); } //end while } // end loop // By Fahed Al Riachi - 2013