Stepper Motor with Push button

hi there,

i'm trying to control a stepper motor using one push button, through several cases.

i'm using arduino uno with both stepper motor 28BYJ-48 and Servo motor PDI-1109MG

i've been trying for the last 3 days to understand how's the push button work to break the switch cases.

all what i need from this code, is
first press (case) of the button, the servo will close it's arm from 170 degrees into 70 degrees,
the second press, the stepper should rotate for only one revolution
the third time is stopping the servo for a bit to time as delay (5000);
the fourth case is rotate the stepper for one revolution as well
the fifth case is rotate the stepper for two revolutions
and the default, which is the last press of the button is stopping the stepper from rotating and open the arm of the servo into 170 degrees again.

i tried to edit my code several times,
i looked for tutorials about push buttons, servo and stepper. but till now i couldn't solve my problem.

can anyone help me with this code.

please find the attached code

#include <Pushbutton.h>
#include "Stepper.h"
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

#define pin1  9     
#define pin2  10 
#define pin3  11 
#define pin4  12  

const float stepper_rev=32;      // 4 step sequence : inner rotor step for 1 revolution
const float Gear_ratio= 64;     // Gear Ratio
const float step_rev= stepper_rev * Gear_ratio;   // outer shaft for 1 revolution

int State=0;
int old=0;
int reading=0;


int StepRequired;
//#define step_delay 8


Stepper mystepper (stepper_rev, pin1,pin3,pin2,pin4);


//int ledState = HIGH;         // the current state of the output pin
//int buttonState;             // the current reading from the input pin
//int lastButtonState = LOW;   // the previous reading from the input pin
//
//unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
//unsigned long debounceDelay = 25;    // the debounce time; increase if the output flickers





#include <Servo.h>
Servo BenServo;  

void setup() {
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(ledPin, OUTPUT);

//  digitalWrite(ledPin, ledState);

 BenServo.attach(8); // attaches the servo on pin 8 to the servo object
 Serial.begin(9600); // open a serial connection to your computer
//  myStepper.setSpeed(15); // set the speed to 600 rpm

 mystepper.step (0);
 
 BenServo.write(170);


}

void loop() {
 // read the state of the switch into a local variable:
 reading = digitalRead(buttonPin);
 StepRequired =  step_rev;
 mystepper.setSpeed (600);

if (reading ==1)
{
 delay (50);
 reading = digitalRead(buttonPin);
 if (reading == 0)
 {
   State = old+1;
 }
}
else 
{
//  delay (50);
}

switch (State)
{
 case 1:
 BenServo.write(77);
 Serial.println("case 1 closed");
 old = State;
 break;

 case 2:
 mystepper.step (StepRequired);
 delay (5000);
 Serial.println("case 2 finished 1st rev");
 old = State;
 break;


 case 3:
 mystepper.step (0);
 Serial.println ("case 3 stop");
 old = State;
 break;

 case 4:
 mystepper.step (StepRequired);
 delay(5000);
 Serial.println("case 4 one rev");
 old = State;
 break;
delay (2000);
 case 5:
 mystepper.step (StepRequired*2);
 Serial.println("case 5 run twice");
 old = State;
 break;

 default:
 mystepper.step (0);
 delay (100);
 BenServo.write(170);
 Serial.println("default-finished");
 
 old=0;
 break;
 
}
//  if (reading != lastButtonState) {
//    
//    lastDebounceTime = millis();
//
//   
//  }
////
//  if ((millis() - lastDebounceTime) > debounceDelay) {
//  
//
//  
//    if (reading != buttonState) {
//      buttonState = reading;
//
//    
//      if (buttonState == HIGH) {
//        ledState = !ledState;
//      }
//    }
//  }
}


 
 
//  digitalWrite(ledPin, ledState);
//
//if (ledState == 0) {
// BenServo.write(77);
//
//  myStepper.step(15);
//
//}
//else 
//   BenServo.write(170);
//    myStepper.step(0);
//  
////  delay(100);
//
//  lastButtonState = reading;
//}

StepperServoButtonedited.ino (3.11 KB)

You can NOT connect a servo to the smiley face pin.

You REALLY should start with code that is KNOWN to work, like the state change detection example.

  break;
delay (2000);
  case 5:

Putting anything after the break statement is pointless.

The code does something, no matter how poorly. You have NOT described what it actually does.

@anton123000000, to make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code consistently for easier reading.

...R

thanks mate,

i've edited

anton123000000:
thanks mate,

i've edited

Bad idea. You made some of my comments look silly, so I'm disinclined to help you any more.

anton123000000:
i've edited

Have you made changes other than adding code tags?

If so I agree with @PaulS as I can't remember the original text and have no idea what you have changed.

...R