Stepper Motor sleeping timer?

I am building a project with a stepper motor driven by the easydriver board. While i have managed to drive the motor in both directions using pushbuttons i want to enter a process that does the following:

If no button is pressed for eg 30 seconds then the enable pin of the easydriver board will go to LOW so that the battery for the motor will not drain. While the motor is “sleeping” if i press a button i want the motor to be enabled and start moving. How can i achieve this?

This is part of a test code i made using an LED and the serial monitor

if (digitalRead(IN) == LOW && digitalRead(OUT) == LOW){
timer = millis();
while (timer>=10000){
Serial.println(“OFF”);
if (digitalRead(IN) == HIGH || digitalRead(OUT) == HIGH){
Serial.println(“ON”);
timer = 0;
break;}}}}

timer = millis();
while (timer>=10000){

Ten seconds after your sketch starts, that condition will always be true.
You need to mark a time and use a difference from the current time, not an absolute like that.

Please post all your code, use code tags, and don't use the "copy for forum" option.

this is the code i am using for the movement of the stepper motor. I have not included any debounce operations yet since the code works for the time being but i want to have the enable option resolved first.

const int DIR = 8;    //direction pin of easydriver
const int STEP = 9;    //stepping pin of easydriver
const int IN = 2;    //IN switch pin
const int OUT = 3;   //OUT switch pin
const int MS1 = 4;   //Microstepping 1 pin of easydriver
const int MS2 = 5;   //Microstepping 2 pin of easydriver
const int Enable = 6;    //Enable pin of easydriver
int POT = 1;    //starting value of potentiometer
long timer = 0; //timer for the sleep operation

void setup() {                
Serial.begin(9600);

//set up of the pins 
      pinMode(DIR, OUTPUT);     
      pinMode(STEP, OUTPUT);
      pinMode(IN, INPUT);
      pinMode(OUT, INPUT);
      pinMode(MS1, OUTPUT);     
      pinMode(MS2, OUTPUT);
      pinMode(Enable, INPUT);
 
      digitalWrite(DIR, LOW);     
      digitalWrite(STEP, LOW);
      digitalWrite(IN, LOW);
      digitalWrite(OUT, LOW); 
      digitalWrite(MS1, LOW);     
      digitalWrite(MS2, LOW);
      digitalWrite(Enable, HIGH); //set enable to high so the motor has power
}


void loop() {
  POT = constrain(POT, 6, 1023); //constrain the values of pot 
  POT = map(analogRead(0), 6, 1023, 1, 170);  //map values of pot for later use at the step delay

//in movement of motor
       if (digitalRead(IN) == HIGH){
             digitalWrite(DIR, LOW);
             digitalWrite(STEP, LOW);
             delay(POT);     //use the mapped value of the pot
             digitalWrite(STEP, HIGH);
             delay(POT);     //use the mapped value of the pot
    }

 
 //out movement of motor
       if (digitalRead(OUT) == HIGH){
             digitalWrite(DIR, HIGH);
             digitalWrite(STEP, LOW);
             delay(POT);     //use the mapped value of the pot
              digitalWrite(STEP, HIGH);
              delay(POT);     //use the mapped value of the pot
    }
  
if (digitalRead(IN) == LOW && digitalRead(OUT) == LOW){     //if both switches are LOW then start counting millis
      timer = millis();
while (timer>=10000){                                      //While the timer is above 10 secs
             digitalWrite(Enable, LOW);                    //set the power to the stepper coils to LOW
               if (digitalRead(IN) == HIGH || digitalRead(OUT) == HIGH){      //if there is a reading on any of the switches then set the
                    digitalWrite(Enable,HIGH);                                // timer to 0 and break the while loop
                     timer = 0;
                     break;
            }
        }
    }
}
break;}}}}

If you can't be bothered writing proper code, or following any coding standards, and you can't be bothered to read the "How to post in this forum" sticky, and you can't be bothered to post your code correctly, I can't be bothered to read it.

Please post all your code, use code tags, and don't use the "copy for forum" option

I suppose, in the timeless words of Meatloaf, two out of three ain't bad.

You need to save the time you want your timeout to start in an unsigned long, then subtract that from current time until 10000 milliseconds have elapsed.

PaulS:

break;}}}}

If you can't be bothered writing proper code, or following any coding standards, and you can't be bothered to read the "How to post in this forum" sticky, and you can't be bothered to post your code correctly, I can't be bothered to read it.

i believe that now the code is per the rules of the forum and to your liking (no disrespect intended).

Why are you using a potentiometer to determine the length of the pulses needed for the stepper motor pulses?

 digitalWrite(STEP, LOW);
             delay(POT);     //use the mapped value of the pot
             digitalWrite(STEP, HIGH);
             delay(POT);     //use the mapped value of the pot

As far as I know the pulses should be the same length no matter how the motor is moved.

...R

With the pot i control the speed of the motor, and i want to have speed control. Small duration=fast movement. My code is based on the examples given at this page Easy Driver Examples At the code i start the mapping and constrain the pot from the value 6 so i can use a debounce operation later.

You didn't answer my question ...

I think you need to study the examples you linked to much more carefully.

...R

I include a pic of the circuit i have made. With the pushbuttons i control the direction of the stepper motor. With the rotary pot i control the speed of the stepper motor. The code i used and modified is the following:

void setup() {                
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

void loop() {
  digitalWrite(9, HIGH);
  delay(1);          
  digitalWrite(9, LOW); 
  delay(1);          
}

according to the explanation of the author: "What if we wanted the motor to go slower? We change the delay(); lines to have longer delays. If you use delay(10); for both, the you’ll move at 50 microsteps/second.

What if you wanted the motor to go faster? We can’t really delay for less than 1 ms, can we? Yes, of course we can! We can change the delay() calls to delayMicroseconds(100); calls and then each delay would be 100 microseconds (or us), so the motor would be driven at 5000 microsteps/second." Similar piece of code and explanation is given by these pages http://danthompsonsblog.blogspot.gr/2010/05/easydriver-42-tutorial.html bildr Use The EasyDriver Stepper Motor Driver + Arduino - bildr Stepper Motor Quickstart Guide - SparkFun Electronics
So i used a pot to change the delay value from 6ms to 100ms in order for the motor to move slower or faster.

The original sketch i posted (excluding the code if (digitalRead(IN) == LOW && digitalRead(OUT) == LOW) until the end) works for the movement and speed of the motor. My concern right now is how to make the code to put the motor to sleep by setting the Enable pin to LOW after a time eg 10 secs and setting it to HIGH if i push a button.

OK, I see why you were using that approach. I don't think it's good advice. as far as I know the pulses to the stepper driver board should be a more-or-less fixed length. Of course there can be a very long delay between pulses - days or weeks if that's what's needed. Just use the pot to change the delay between pulses, not the length of the pulse.

To get the enable pin to switch on and off you need to have a global variable to store its "state" - let's call it motorState. The code in your loop should switch on the enable pin if motorState is HIGH and switch it off if motorState is LOW.

Then you need a piece of code to read the button. The first button press will set motorState to HIGH and the next button press will set motorState to LOW etc. You should probably include some code to "debounce" the button press so you don't get a whole stream of HIGHs and LOWs.

...R