#include <Servo.h>  //We import the Servo library
Servo myservo1;  //  We declare Servo 1
#include <Servo.h>  //We import the Servo library
Servo myservo2;  //  We declare Servo 2

#include <Stepper.h>   //We import the Stepper library

const int stepsPerRevolution = 2038;  //This variable gives us the number of steps that the Stepper does when making 1 whole revolution. This number is given by the Stepper data sheet

Stepper myStepper (stepsPerRevolution, 8, 10, 9, 11); //We declare the Stepper

float X = 9.2; //This variable will work as the number of revolutions we want to make with our Stepper. In this case, we get this value (9.2) by calculating how many revolutions does the 

int sensorValue; //We use this variable as the one that contains the values read from the LDR voltage.


void setup() {
   
  Serial.begin(9600); //initialize serial communication at 9600 bits per second
  
  myservo1.attach(3); // We declare at what pin Servo 1 is
  myservo2.attach(5);  // We declare at what pin Servo 2 is
  myStepper.setSpeed(15);  // We declare the speed at which the Stepper rotates
  
  myservo1.write(0);  // We set the Servo 1 to start at angle 0
  myservo2.write(0);  //We set the Servo 2 to start at angle 0
}


void loop() {
  // read the input on analog pin 0:
  sensorValue = analogRead(0);  //We read the voltage before the LDR resistor by placing a cable that connects to pin A0 on the UNO board.
  Serial.println(sensorValue);  //We show the voltage values ​​for each time it reads the voltage values ​​in the Monitor Series.

  if (sensorValue <= 90) {  // We ask the question that will determine if the motors (Servos and Stepper) act or not and thus the whole mechanism of the project starts working.
                            // The question we ask is if the sensorValue is lower than 90, because that is the value that is shown when the hand is covering the LDR (trying to reach the candy)
                           //If so, we start all the action of the motors.
   
    myservo1.write(60); //Servo 1 moves rotates 60 degrees in order to open the first door.
    myservo2.write(-60); //Servo 2 moves rotates -60 degrees in order to open the second door. This value is negative because the second door rotates the opposite way the first door does.
                         // At this point, since it was until now supported at the doors, the knife drops because of its weight. It started in a position in which the cable was not tense. 
    
    delay(3000);  //Before we start picking up the cable, we wait for three seconds so that the scary moment the person just lived sinks in.

    myStepper.step(stepsPerRevolution * X);  //The Stepper rotates and picks up de cable, which pulls the knife until it stays completely upright

    myservo1.write(-60);  //Servo 1 moves rotates -60 degrees in order to close the first door.
    myservo2.write(60);  //Servo 2 moves rotates 60 degrees in order to close the second door. This value is negative because the second door rotates the opposite way the first door does.

    myStepper.step(-stepsPerRevolution * X);  //The Stepper rotates the same amount of revolutions that it did when it picked up the cable so that it 
                                              // lets the knife supported just by the inner faces of the doors. When the cycle restarts and the two doors open, the knife will immediately drop.
  }
  else {   //If the sensorValue is not lower than 90, whole mechanism stays still
    myservo1.write(0);
    myservo2.write(0);
  }

  delay(1);        // delay in between reads for stability

}
