Cannot get stepper motor to turn in runState

I’m new to Arduino and I’m doing my best to learn.

Can someone help me correct this code? I cannot get the one stepper motor code to compile without errors.

I need 3 different movements & texts to display with three different buttons.

I have a Arduino UNO with an Adafruit MotorShield, one stepper motor, and three buttons.

If there is a better way to write the coding, please advise.

Again, I am new to Arduino, but I am doing my best to learn.

Thank you in advance.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;  //move stepper 33 BACKWARD MICROSTEP & display text
int buttonPin2 = 3 ;  //move stepper  8 FORWARD MICROSTEP & display text
int buttonPin3 = 4 ;  //move stepper 30 FORWARD MICROSTEP & display text

int myStepper ;

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
  Serial.begin(9600);           
  Serial.println("Stepper test!");
  pinMode (buttonPin1, INPUT_PULLUP);
  pinMode (buttonPin2, INPUT_PULLUP);
  pinMode (buttonPin3, INPUT_PULLUP);
  Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

  AFMS.begin(); 
  
  myMotor->setSpeed(10);  // 10 rpm   
}

void loop() 
{ 
  if (digitalRead(buttonPin1) == LOW)   //if button #1 is pressed for Dual Evap
    {
    runState=1;      //start running stepper 33 BACKWARD MICROSTEP
  }
  //Serial.println("Dual Evaporator (both)");   //Display "Dual Evaporator"
 //   myMotor->step(33, BACKWARD, MICROSTEP);    //advance 33 BACKWARD, MICROSTEP
//  {
  if (digitalRead(buttonPin2) == LOW)   //if button #2 is pressed for Freez Evap
  {
   runState=2;    //start running stepper 8 FORWARD MICROSTEP
  }
 // Serial.println("Freezer Evaporator (left)");  //Display "Freezer Evaporator"
 //   myMotor->step(8, FORWARD, MICROSTEP);      //advance 8 FORWARD, MICROSTEP
//  {  
    if (digitalRead(buttonPin3) == LOW)   //if button #3 is pressed for Fresh Evap
  {
    runState=3;    //start running stepper 30 FORWARD MICROSTEP
  }
 // Serial.println("Fresh Food Evaporator (right)");    //Display "Fresh Food Evaporator"
  //  myMotor->step(30, FORWARD, MICROSTEP);     //advance 30 FORWARD, MICROSTEP
 // {  
  if (runState=1)  //what should be displayed and how to run the stepper
  {
  Serial.println("Dual Evaporator (both)");   //Display "Dual Evaporator"
    myMotor->step(33, BACKWARD, MICROSTEP);    //advance 33 BACKWARD, MICROSTEP
  }
  if (runState=2)  //what should be displayed and how to run the stepper
  {  
  Serial.println("Freezer Evaporator (left)");  //Display "Freezer Evaporator"
    myMotor->step(8, FORWARD, MICROSTEP);      //advance 8 FORWARD, MICROSTEP
  } 
  if (runState=3)  //what should be displayed and how to run the stepper 
  {  
  Serial.println("Fresh Food Evaporator (right)");    //Display "Fresh Food Evaporator"
    myMotor->step(30, FORWARD, MICROSTEP);     //advance 30 FORWARD, MICROSTEP
  }
  
 // }
 // }
 // }
}

Cut and paste the compile errors here please.

Thank you in advance for your help.

Valve_buttons_5.ino: In function 'void loop()':
Valve_buttons_5.ino:39:5: error: 'runState' was not declared in this scope
Valve_buttons_5.ino:46:4: error: 'runState' was not declared in this scope
Valve_buttons_5.ino:53:5: error: 'runState' was not declared in this scope
Valve_buttons_5.ino:58:7: error: 'runState' was not declared in this scope
Valve_buttons_5.ino:63:7: error: 'runState' was not declared in this scope
Valve_buttons_5.ino:68:7: error: 'runState' was not declared in this scope
Error compiling

Well, you need to declare runState. That is what the error message is telling you.

Can you show me how that is done?

I'm not sure if it is different or the same for each runState, or the best way to write that into the sketch.

superprojb:
Can you show me how that is done?

I'm not sure if it is different or the same for each runState, or the best way to write that into the sketch.

You must already know how to declare variables, since you have one already:

int myStepper ;

You just forgot about runState.

... and since you asked for general advice, you don't need any variable like runState at all to perform your task. You can eliminate it and just move your button tests directly into the if statement tests.

Thank you! I will try your suggestions in the morning.

Again, thank you for your help. I do appreciate your advice very much.

superprojb:
I need 3 different movements & texts to display with three different buttons.

I have a Arduino UNO with an Adafruit MotorShield, one stepper motor, and three buttons.

I suspect that AdaFruit have a test program - have you managed to get that to work (without making any changes to it) ?

If the unchanged AdaFruit program won't work post the code here as well as a photo of a drawing of how everything is connected.

Post a link to the datasheet for your stepper motor.

How are you powering the motor?
DO NOT power the motor from the Arduino.

Have a look at stepper motor basics

...R

Also you have some "=" in if statements that should be "=="

The stepper motor does work in test sketches.

I tried to modify the code by removing the runState as suggested, but could not get the code right.

Can someone correct the code so I can learn what I'm doing wrong?

Thank you in advance for any help.

1 - you are not declaring runState as a variable. Add

int runState;

after line 9 (the declaration of myStepper) or inside the loop() function.

2 - the various lines

if (runState=1) { }

Will cause runState to be assigned a value of whatever, and will then check to see that this value is nonzero. Since it always is assigned a nonzero value, your code will execute each of the motor steps each time it loops.

Fix this by making those lines a double equals

if (runState==1)

Protip - use a switch() statement. This usually compiles down to a computed goto.

3 - you do not reset run state to zero. This means that if you press one of the buttons, the code will keep doing that button press function until some other button is pressed.

To fix this, stick a

runState=0;

before the very first if() in the loop() function.

Haven’t tried compiling this (not on the right machine ATM), but something like this:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;  //move stepper 33 BACKWARD MICROSTEP & display text
int buttonPin2 = 3 ;  //move stepper  8 FORWARD MICROSTEP & display text
int buttonPin3 = 4 ;  //move stepper 30 FORWARD MICROSTEP & display text

int myStepper ;

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
  Serial.begin(9600);           
  Serial.println("Stepper test!");
  pinMode (buttonPin1, INPUT_PULLUP);
  pinMode (buttonPin2, INPUT_PULLUP);
  pinMode (buttonPin3, INPUT_PULLUP);
  Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

  AFMS.begin(); 
  
  myMotor->setSpeed(10);  // 10 rpm   
}

void loop() 
{ 
  int runState = 0;

  if (digitalRead(buttonPin1) == LOW)   //if button #1 is pressed for Dual Evap
  {
    runState=1;      //start running stepper 33 BACKWARD MICROSTEP
  }

  if (digitalRead(buttonPin2) == LOW)   //if button #2 is pressed for Freez Evap
  {
   runState=2;    //start running stepper 8 FORWARD MICROSTEP
  }

  if (digitalRead(buttonPin3) == LOW)   //if button #3 is pressed for Fresh Evap
  {
    runState=3;    //start running stepper 30 FORWARD MICROSTEP
  }

  switch(runState) {
  case 1:
    Serial.println("Dual Evaporator (both)");   //Display "Dual Evaporator"
    myMotor->step(33, BACKWARD, MICROSTEP);    //advance 33 BACKWARD, MICROSTEP
    break;

  case 2:
    Serial.println("Freezer Evaporator (left)");  //Display "Freezer Evaporator"
    myMotor->step(8, FORWARD, MICROSTEP);      //advance 8 FORWARD, MICROSTEP
    break;

  case 3:
    Serial.println("Fresh Food Evaporator (right)");    //Display "Fresh Food Evaporator"
    myMotor->step(30, FORWARD, MICROSTEP);     //advance 30 FORWARD, MICROSTEP
    break;

    // default: do nothing.
  }
  
}