//Include the servo library so the servomotor can be controlled
#include <Servo.h> 

//create a servo object, named 'laserServo'
Servo laserServo; 

//intitialise Global variables
//integers for laser angle variable and endpoint
int laserAngle = 0;
int laserPeak = 0;

//***STAGE #1 - define your variables here ***
int Potentiometer_pin = A0;
int LDR_pin = A5;
bool Button_pin = 7;
int LDR_LED_pin = 3;
int Potent_LED_pin = 5;
bool Button_LED_pin = 6;
int Motor = 9;

int LDR_val = 0;
int Potentiometer_val = 0;
int Button_val = 0;

void setup() {

  //initialise servo (this is done for you)
  laserServo.attach(9); //attach servo to pin 9

  //***STAGE #2 - setup your system here ***   

pinMode(LDR_pin, INPUT);
pinMode(Potentiometer_pin, INPUT);
pinMode(Button_pin, INPUT);

pinMode(LDR_LED_pin, OUTPUT);
pinMode(Potent_LED_pin, OUTPUT);
pinMode(Button_LED_pin, OUTPUT);

  //begin serial transmissions (for sending information back)
  Serial.begin(9600);

}

void loop() {
  //Call User Defined Function to move the laser (leave this function to keep the servo moving!)
  laserAlign();

  //***STAGE #3a - read sensors here *** 

LDR_val = analogRead(LDR_pin);
Potentiometer_val = analogRead(Potentiometer_pin);
Button_val = digitalRead(Button_pin);

  //***STAGE #3b - read sensors here *** 

Serial.print(LDR_val);
Serial.print(",");
Serial.print(Potentiometer_val);
Serial.print(",");
Serial.println(Button_val);

  delay(30); //delay which defines how fast you loop to read your sensors...

analogWrite(LDR_LED_pin, LDR_val/16);
analogWrite(Potent_LED_pin, Potentiometer_val/16);
digitalWrite(Button_val,HIGH);

//if (Button_val == LOW)
//digitalWrite(Button_val,HIGH);
//else digitalWrite(Button_val,LOW);
}

//User Defined Function to move the laser
void laserAlign(){
  
  // move laserServo to new angle
  laserServo.write(laserAngle);	

 /*
  * Logic for deciding next angle
  */
  if ((laserAngle >= 0) && (laserAngle <= 120) && (laserPeak ==0))
     {laserAngle++;}
  if ((laserAngle == 120) && (laserPeak ==0))
     {laserPeak = 120;}
  if ((laserAngle >= 0) && (laserAngle <= 120) && (laserPeak ==120))
     {laserAngle--;}
  if ((laserAngle == 0) && (laserPeak ==120))
     {laserPeak = 0;}

}