Running stepper motor on a time interval with optional button override

I am working on a project where I need to run stepper motor on a set time interval. I also need to have the option to activate the rotation with a button. I got the time interval, but I can't figure out the button part of the code. Can I get some guidance on how to make this work? Thanks!

Here is my code that I have so far.

#define A 2
#define B 3
#define C 4
#define D 5
const int buttonPin = 7;
int buttonState = 0;
#define NUMBER_OF_STEPS_PER_REV 512

void setup(){
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(C,OUTPUT);
pinMode(D,OUTPUT);
}

void write(int a,int b,int c,int d){
digitalWrite(A,a);
digitalWrite(B,b);
digitalWrite(C,c);
digitalWrite(D,d);
}

void onestep(){
write(1,0,0,0);
delay(1);
write(1,1,0,0);
delay(1);
write(0,1,0,0);
delay(1);
write(0,1,1,0);
delay(1);
write(0,0,1,0);
delay(1);
write(0,0,1,1);
delay(1);
write(0,0,0,1);
delay(1);
write(1,0,0,1);
delay(1);
}

void loop (){



if (buttonState == HIGH) {
  int i;
  i=0;
  while(i<NUMBER_OF_STEPS_PER_REV){
  onestep();
  i++; }

}  else {
    int i;
    i=0;
    while(i<NUMBER_OF_STEPS_PER_REV){
    onestep();
    i++;
    delay(15000);
}
    
}
}

A program that uses delay() can not be very responsive. Use millis() (or micros()) timers instead.

Blink without delay().
Beginner's guide to millis().
Several things at a time.

Does all the individual parts work being exercised alone?
Real schematics are interesting to see, not toy pictures like Fritizings.
How is the button wired?
buttoninput needs to be declared in setup.
1 mS delay for any stepper is way too little.

I've decided to use delay() for now. Thank you for the advice though.

I can run the servo with code before I begin trying to incorporate a button. I don't have any schematics unfortunately. The button is connected to a ground pin and digital pin 7.

I can't see any declaration of pin 7 as input, INPUT_PULLUP. I see no reading of the button pin...

Servo? The title tells about a stepper?

Stepper. Sorry...

Thanks. Better safe than sorry so I checked it up.

What about the reply in #7?

I have updated that part of the code and will upload in a few minutes. I can now run the motor with the button or on the timer, but when I try to incorporate both, nothing works.

I urge You to post the schematics. Often pen, paper, a foto and posting it helps us.
Links or data about the powering is highly wanted. Don't tell You use a PP3 battery.... The symptoms points to a possible powering issue.

I have got my code to work! The motor turns one revolution every 15 seconds. The button also turns the motor one revolution when pressed. Now the only question I have is how to make the motor turn more than one revolution at a time? Thank you all for the help!

#define A 2
#define B 3
#define C 4
#define D 5
const int buttonPin = 7;
int buttonState;
#define NUMBER_OF_STEPS_PER_REV 512
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 15000;

void setup(){
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(C,OUTPUT);
pinMode(D,OUTPUT);
pinMode(7,INPUT_PULLUP);
startMillis = millis(); 
}

void write(int a,int b,int c,int d){
digitalWrite(A,a);
digitalWrite(B,b);
digitalWrite(C,c);
digitalWrite(D,d);
}

void onestep(){
write(1,0,0,0);
delay(1);
write(1,1,0,0);
delay(1);
write(0,1,0,0);
delay(1);
write(0,1,1,0);
delay(1);
write(0,0,1,0);
delay(1);
write(0,0,1,1);
delay(1);
write(0,0,0,1);
delay(1);
write(1,0,0,1);
delay(1);
}

void loop (){

currentMillis = millis();
buttonState = digitalRead(buttonPin);

if (currentMillis - startMillis >= period)  
{
int i;
i=0;
while(i<NUMBER_OF_STEPS_PER_REV){
onestep();
i++;
startMillis = currentMillis;
}
}

if (buttonState == LOW) {
  int i;
  i=0;
  while(i<NUMBER_OF_STEPS_PER_REV){
  onestep();
  i++; 
  }
    
}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.