#include <SoftwareSerial.h>

SoftwareSerial mySerial (6,5); // pins 6 and 5 used as serial communication pins and called mySerial

int servo = 2;// connected to pin2
int led = 3;// LED connectedd to pin 3

int minPos = 500; // positions of eye movement
int homePos = 1500;
int maxPos = 2400;

int changeRate = 20;


char inputChar; //variable where incoming communication is stored

char ID = '3';// right eye ID


void servoSet(int pos){
  digitalWrite(servo, HIGH);// turning servo on and moving to position
  delayMicroseconds(pos);
  digitalWrite(servo, LOW);
  delayMicroseconds(20000-pos);
}

void servoHome(){
  digitalWrite(led,HIGH); //LED on
  for(int i=0;i<10;i++){
    servoSet(homePos);
  }
  digitalWrite(led,LOW); //turning the LED off
}

void leftEye(){
  digitalWrite(led,HIGH);
  for(int i=homePos;i<=maxPos;i=i+changeRate){
    servoSet(i);
  };
  for(int i=maxPos;i>=minPos;i=i-changeRate){
    servoSet(i);
  }
  for(int i=minPos;i<=homePos;i=i+changeRate){
    servoSet(i);
  };
  servoHome();
  digitalWrite(led,LOW);
}

void setup(){
  pinMode(servo,OUTPUT);
  pinMode(led,OUTPUT);
  servoHome();
  
  mySerial.begin(9600); //board rate, start serial communications at 9600
  
  pinMode(5,INPUT_PULLUP);// changed pin 5 to input to prevent inerference with other transmitting boards

}


void loop(){
  if (mySerial.available () ) { // if there is a message waiting
    inputChar = mySerial.read();// then store in inputChar
    
    if(inputChar == ID){
      leftEye();
    }
  }
      
 
}
  
