Hi everyone,
Corona lockdown is undoubtedly a boring time, but I used the time to set about on a new DIY adventure. Since my bedroom curtains run behind my bed's headboard they are often tricky to open. To make life easy I would like to be able to open and close them using a remote.
Hence, I have been playing around with an Arduino setup in order to control the position of a stepper motor that will drive the curtains. I use a Nano, an EasyDriver v4.4, a NEMA 17 stepper motor, an IR sensor + remote and of course a power supply. The setup works; I can program the Nano so I can move the stepper motor using the IR remote. However, in this case the driver and motor are constantly powered on, which is unnecessary. For power consumption purposes and to prevent the motor from heating and making noise I would like to 'sleep' the EasyDriver.
The EasyDriver should 'sleep' by default and should wake up when a button on the IR-remote is pressed. Next it should stay awake and wait for a button to be pressed again, to either open or close the curtains. After it has moved the curtain I would like it to wait a few seconds before going into sleep-mode again.
I have tried to do so by making an If-loop that check if the IR sensor has received a signal. If so, wake the driver and proceed to move the motor accordingly using a switch...case command. Unfortunately it doesn't work this way. It behaves is such a way that I struggle to understand what is going on.
The driver doesn't wake immediately after it receives a signal / a button is pushed. When it does, the motor is held in position. When pressing a button again, say '5', it should run to its corresponding position. However, it doesn't. It stays stationary for 20 seconds (delay time) and then goes to sleep again. The loop seems to be blocking some of the functionalities.
Does anyone have a suggestion for solving this problem? Maybe using a function in order to move the stepper? Maybe a while loop (while the driver is awake...move to position when receiving a signal)?
Thanks in advance!
Ps. I must admit I'm not knowledgable when it comes to using a function command (yet).
Ps.Ps. I left out all other lines of code (define, constants and void setup lines) in order to fit it within the allowed 9000 characters.
void loop() {
if (irrecv.decode(&results)) // check if a IR-code is recieved
{
digitalWrite(sleepPin,HIGH); // Wake the EasyDriver
if (results.value == 0xFFFFFFFF) { // If key is repeated, the code becomes FFFFFFFF
results.value = lastKey; // IR-code is re-written to the code of the key being held
}
switch (results.value) { // Follows set of commands if IR-code equals case-code
case 0xFF6897: // When the '*' key is pressed
lastKey = results.value; // Store as last key (saved for later axecution by .run();)
stepper.moveTo(beginPos); // Move to the starting position
Pos = stepper.currentPosition(); // Update position
Serial.println(Pos); // Indicate position
break;
case 0xFF629D: // When the '2' key is pressed
lastKey = results.value; // Store as last key (saved for later axecution by .run();)
stepper.moveTo(pos1); // Move to position
Pos = stepper.currentPosition(); // Update position
Serial.println(Pos); // indicate position
break;
case 0xFF02FD: // When the '5' key is pressed
lastKey = results.value; // Store as last key (saved for later axecution by .run();)
stepper.moveTo(pos2); // Move to position 2
Pos = stepper.currentPosition(); // Update position
Serial.println(Pos); // Indicate position
break;
case 0xFFA857: // When the '8' key is pressed
lastKey = results.value; // Store as last key (saved for later axecution by .run();)
stepper.moveTo(pos3); // Move to position 3
Pos = stepper.currentPosition(); // Update position
Serial.println(Pos); // Indicate position
break;
case 0xFF9867: // When the '0' key is pressed
lastKey = results.value; // Store as last key (saved for later axecution by .run();)
stepper.moveTo(pos4); // Move to position 4
Pos = stepper.currentPosition(); // Update position
Serial.println(Pos); // Indicate position
break;
case 0xFFB04F: // When the '#' key is pressed
lastKey = results.value; // Store as last key (saved for later axecution by .run();)
stepper.moveTo(endPos); // Move to end position
Pos = stepper.currentPosition(); // Update position
Serial.println(Pos); // Indicate position
break;
}
delay(100); // Small delay to prevent misreadings
irrecv.resume(); // Resume recieving of codes
}
stepper.run(); // Let the stepper move to the desired position
delay(20000); // Add delay before turning into sleep-mode
digitalWrite(sleepPin,LOW); // Return to sleep mode
}