#include //Variables const int analogInPinLeft = A0; //Pin on the Arduino board float sensorValueLeft = 0; //Value obtained on the receiver int isOpening = 0; //If the curtain is opening int isClosing = 0; //If the curtain is closing int lastMovement = 1; //To keep the last action executed : Opening = 1 || Closing = 2 int nbOfRotation = 0; //To keep the number of rotation done by the stepper motor //Instantiate the stepper motor const int stepsPerRevolution = 200; //Number of steps per revolution done by the stepper motor Stepper stepperLeft(stepsPerRevolution, 8, 10, 9, 11); //Associates the pins on the Arduino board to stepperLeft //Actions to be executed when the program is executed for the first time void setup() { Serial.begin(9600); //In order to see the values read by the receiver in the serial monitor //Sets the speed of the stepper in rpm (revolution per minute) stepperLeft.setSpeed(120); } //Actions to be executed when the program is running and the setup has been completed void loop() { //Read the sensor value for the receiver sensorValueLeft = analogRead(analogInPinLeft); float voltageLeft = sensorValueLeft*5/1023; //In order to read the sensor value as a voltage //To activate the motor when the button is pressed Serial.print("Voltage Output = \n"); //In order to properly read the values in the serial monitor Serial.print(voltageLeft); //Prints the voltage after one read //If the voltage is bigger than 3 (value obtained if the button is pressed) if (voltageLeft > 3) { //If no action required if(isOpening == 1 || isClosing == 1){ //Do nothing } else if(lastMovement == 1){ //if the curtain was closed, the variables will be changed in order to open lastMovement = 2; isOpening = 1; } else if(lastMovement == 2) { //if the curtain was opened, the variables will be changed in order to close lastMovement = 1; isClosing = 1; } } //if we want to open, we do a certain number of steps clockwise and we increment the number of rotation if(isOpening == 1){ stepperLeft.step(2000); nbOfRotation++; } else if(isClosing == 1){ //if we want to close, we do a certain number of steps counterclockwise and we stepperLeft.step(-2000); //increment the number of rotation nbOfRotation++; } //If the number of rotation needed to completely open or close the curtain is obtained, stop the current movement if(nbOfRotation > 5){ isOpening = 0; isClosing = 0; nbOfRotation = 0; } //Delay between each read on the receiver delay(300); }