//commands (send via com port):
//1 - open channel 1, 2 - close channel 1
//3 - open channel 2, 4 - close channel 2
//initially both channels set to be opened
const byte PINbut1=11; //pin for button 1
const byte PINbut2=12; //pin for button 2
const byte PINled1=5; //pin for LED 1
const byte PINled2=6; //pin for LED 2
const byte PINservo1=10; //pin for Servo 1
const byte PINservo2=9; //pin for Servo 2
const byte blink=150; //blink interval in ms
const byte brightness=100; //brightness of diodes 0...255
#include <Servo.h>
Servo servo1; // create servo object to control a servo1
Servo servo2; // create servo object to control a servo2
bool STATUSservo1=0; //status of servo 1: 0 -- opened, 1 -- closed
bool STATUSservo2=0; //status of servo 2: 0 -- opened, 1 -- closed
bool STATUSled1=0; // status of LED 1: 0 -- off , 1 -- on
bool STATUSled2=0; // status of LED 1: 0 -- off, 1 -- on
byte s1pos[2]={0,180}; // angles of servo1 in {opened,closed} positions
byte s2pos[2]={0,180}; // angles of servo2 in {opened,closed} positions
byte command=0; //number of command from computer:
//1 - open channel 1, 2 - close channel 1
//3 - open channel 2, 4 - close channel 2
void setup() {
Serial.begin(9600);
pinMode(PINbut1,INPUT_PULLUP); //set pinMode for button 1
pinMode(PINbut2,INPUT_PULLUP); //set pinMode for button 2
pinMode(PINled1,OUTPUT); //set pinMode for LED 1
pinMode(PINled1,OUTPUT); //set pinMode for LED 2
servo1.attach(PINservo1); // attaches the servo1 to the servo object
servo1.write(s1pos[0]); //initially opens channel 1
for (byte i=1; i<8; i++) { //blink both leds while mooving servo 1
STATUSled1=!STATUSled1;
STATUSled2=!STATUSled2;
analogWrite(PINled1,STATUSled1*brightness);
analogWrite(PINled2,STATUSled2*brightness);
delay(blink); //to prevent simultaneously mooving of two servos
}
servo2.attach(PINservo2); // attaches the servo2 to the servo object
servo2.write(s2pos[0]); //initially opens channel 2
for (byte i=1; i<8; i++) { //blink both leds while mooving servo 2
STATUSled1=!STATUSled1;
STATUSled2=!STATUSled2;
analogWrite(PINled1,STATUSled1*brightness);
analogWrite(PINled2,STATUSled2*brightness);
delay(blink); //to prevent simultaneously mooving of two servos
}
STATUSled1=0;
STATUSled2=0;
analogWrite(PINled1,STATUSled1*brightness); // turn off led1
analogWrite(PINled2,STATUSled2*brightness); // turn off led2
Serial.println("Ready!");
}
void loop() {
if (!digitalRead(PINbut1)) { //button 1 is pushed
rotateServo1(STATUSservo1); //rotate servo 1
STATUSservo1=!STATUSservo1; //update status of servo 1
};
if (!digitalRead(PINbut2)) { //button 2 is pushed
rotateServo2(STATUSservo2);
STATUSservo2=!STATUSservo2;
};
if (Serial.available()>0) {command=Serial.read()-'0'; //reading data from the computer
//Serial.print(command); Serial.print("\n");
if (command==1&&STATUSservo1==1) { //need to open channel 1 and it is closed
rotateServo1(STATUSservo1);
STATUSservo1=!STATUSservo1;}
if (command==2&&STATUSservo1==0) { //need to close channel 1 and it is opened
rotateServo1(STATUSservo1);
STATUSservo1=!STATUSservo1;}
if (command==3&&STATUSservo2==1) { //need to open channel 2 and it is closed
rotateServo2(STATUSservo2);
STATUSservo2=!STATUSservo2;}
if (command==4&&STATUSservo2==0) { //need to close channel 2 and it is opened
rotateServo2(STATUSservo2);
STATUSservo2=!STATUSservo2;}
}
}
void rotateServo1(bool status) { //changing the position of servo 1
servo1.write(s1pos[!status]); //moving the servo
bool STATUSled=status;
for (byte i=1; i<8; i++) { //blinking with LED 1
STATUSled=!STATUSled;
analogWrite(PINled1,STATUSled*brightness);
delay(blink);
}
analogWrite(PINled1,(!status)*brightness); //switch LED to new position
}
void rotateServo2(bool status) { //changing the position of servo 2
servo2.write(s2pos[!status]); //mooving the servo
bool STATUSled=status;
for (byte i=1; i<8; i++) { //blinking with LED 2
STATUSled=!STATUSled;
analogWrite(PINled2,STATUSled*brightness);
delay(blink);
}
analogWrite(PINled2,(!status)*brightness); //switch LED to new position
}