Stepper motor switch control ALMOST THERE

Hey guys!

Alright guys! I think I almost got it!
I need to change the direction of a stepper motor with the help of an alternating switch (press it once, the do a number of steps, another and the same amount of steps in reverse)

I'm using these two lines of code, and I just need to mix them

#include <Stepper.h>
 
 const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                          // for your motor
 
 // initialize the stepper library on the motor shield
 Stepper myStepper(stepsPerRevolution, 12,13);     
 
 // give the motor control pins names:
 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;
 
 int x = 0;
void setup() {
 Serial.begin(9600);
 // set the PWM and brake pins so that the direction pins  // can be used to control the motor:
pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);
 
 // initialize the serial port:
 Serial.begin(9600);
 // set the motor speed (for multiple steps only):
 myStepper.setSpeed(100);
 }

 
 void loop() {
 
  myStepper.step(-10800);
  delay(500);
  myStepper.step(10800);




 }

This one has the amounts of steps I need, I just put it on a loop.

#include <Stepper.h>
 
 const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                          // for your motor
 
 // initialize the stepper library on the motor shield
 Stepper myStepper(stepsPerRevolution, 12,13);     
 
 // give the motor control pins names:
 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;
  int x = 0;
 

int inPin = 2;         // the number of the input pin
int outPin = 4;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);



 Serial.begin(9600);
 // set the PWM and brake pins so that the direction pins  // can be used to control the motor:
pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);
 
 // initialize the serial port:
 Serial.begin(9600);
 // set the motor speed (for multiple steps only):
 myStepper.setSpeed(100);
 }

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

and this one has a switch connected to a led and it works. one press and it on, another and it's off. It also already has all the intergers from the stepper loop.

Now, I just need a little help putting them together

Would you help a brother out?.

I don't understand why you need help . What exactly is the reason you don't know how to integrate them ? Our job here is to help you learn. You need to tell us what the problem is and we'll go from there. Asking us to do it for you is not how we roll here.

Oh no, I'm sorry, that's not what I mean at all.

The part that I don't understand how to do is how to link together the outPin, state part with the myStepper. step part

I I understand those two text, what each line does, but I cannot find a way to allow the change of state to also make the desires steps.

The part that I don't understand how to do is how to link together the outPin, state part with the myStepper. step part

I cannot find a way to allow the change of state to also make the desires steps.

pinMode(outPin, OUTPUT);

What is "Outpin" connected to ?
Can you please use the "Modify" button to edit your code and add comments that describe what everything is connected to ?
You did that with all your other I/O but what about these :

  int inPin = 2;         // the number of the input pin
int outPin = 4;       // the number of the output pin

If the input pin is connected to a switch, why is that not explained in that comment ?
If you where does the output pin connect to ?

These are self explanatory:

 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;

How are these explained : ?

  int inPin = 2;         // the number of the input pin
int outPin = 4;       // the number of the output pin

Input of what ?
Output of what ?

and this one has a switch connected to a led and it works. one press and it on, another and it's off.

Do you see anything about a led in the comments ? Did you mean the led is connected to pin-4 ?

What is it about this that you need help with ?

  digitalWrite(outPin, state);

If the STATE is working then why can't you just use and IF or CASE statement for this ?

  myStepper.step(-10800);
  delay(500);

or

   myStepper.step(10800);
  delay(500);

pinMode(outPin, OUTPUT);

What is "Outpin" connected to ?
Can you please use the "Modify" button to edit your code and add comments that describe what everything is connected to ?
You did that with all your other I/O but what about these :

  int inPin = 2;         // the number of the input pin
int outPin = 4;       // the number of the output pin

If the input pin is connected to a switch, why is that not explained in that comment ?
If you where does the output pin connect to ?

These are self explanatory:

 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;

How are these explained : ?

  int inPin = 2;         // the number of the input pin
int outPin = 4;       // the number of the output pin

Input of what ?
Output of what ?

and this one has a switch connected to a led and it works. one press and it on, another and it's off.

Do you see anything about a led in the comments ? Did you mean the led is connected to pin-4 ?

What is it about this that you need help with ?

  digitalWrite(outPin, state);

If the STATE is working then why can't you just use and IF or CASE statement for this ?

  myStepper.step(-10800);
  delay(500);

or

   myStepper.step(10800);
  delay(500);

[/quote]

Sorry. inPin is the LED that switches on and off when outPin is pressed. I've been frankensteining a lot of code and forgot to do that.

Now, I don't really understand the syntax of it very well. I can read but writing is extremely difficult at the moment.

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);


  previous = reading;
}

On this part I just get so confused. How I understand the flow of the loop it goes like this:

reading is equal to the value of the switch(inPin).

If reading is high and before it was low change the state to high.

If state is high the the led is high.

Now it gets confusing for me. for the stepper there not two but three possible states, FORWARD, REVERSE, and Off.

How can I with an if command and state make it switch between the forward or backward number of steps?

Your I/O labels are just too confusing to deal with. Please rename them with names that describe what they are physically connected to , which means it is not a name you choose , it is a physical description of where that pin is connected.

inPin is the LED that switches on and off when outPin is pressed.

I'm sorry, I don't understand how this statement could be true. You must have it reversed.
Let me explain something , if something is an INPUT (it is connected to a switch), when you press the switch , you don't say
"when Inpin is pressed ", because "inpin" is a label. You say "when the switch is pressed, (and then you have to explain how the switch is setup. ie; pullups or pulldown resistors, and what happens to the signal when it is pressed , ie: switch is Normally HIGH,
pressing switch pulls inpin LOW.)

  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);

Yes, youre right, my bad.

corrected!

Now, the question is the stepper direction conundrum I have.

Your I/O labels are just too confusing to deal with. Please rename them with names that describe what they are physically connected to , which means it is not a name you choose , it is a physical description of where that pin is connected.

Can you rename these

int inPin = 2;         // the number of the input pin
int outPin = 4;       // the number of the output pin

to switchpin // the name of the input pin
and Statepin // the name of the output pin

?

Done!

I also made it a little clearer

#include <Stepper.h>
 
 const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                      
 Stepper myStepper(stepsPerRevolution, 12,13);     // initialize the stepper library on the motor shield  
 
 // give the motor control pins names:
 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;
  int x = 0;
 

int switchPin = 2;         // the number of the input pin
int statePin = 4;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(statePin, OUTPUT);



 Serial.begin(9600);
 // set the PWM and brake pins so that the direction pins can be used to control the motor:
pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);
 
 // initialize the serial port:
 Serial.begin(9600);
 // set the motor speed (for multiple steps only):
 myStepper.setSpeed(100);
 }

void loop()
{
  reading = digitalRead(switchPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(statePin , state);

  previous = reading;
}

You didn't answer the question about the IF or CASE statements .
Why haven't you inserted that after the

digitalWrite(statePin , state);

?

I'm trying to figure it out at the moment.

To what I understand the Switch_case command work as if the Var one is met, it performs this function, else it performs the second variable, right?

I think thatif i put the first var as the state being high then do forward, and then I put the second variable as the state being low then do reverse it should work. right?

Also, I just need this function to be performed once, not that if the var is high then do a certain number of steps gain and again.

Ok now I think we are finally getting somewhere. "I am trying to figure it out.." is too vague.
Rephrase your question in the standard forum format for all questions :
"How do I use an IF statement ?"
or
"How do I use a CASE statement ?"
or , more specifically
"What is the SYNTAX for an IF or CASE statement ?"
"I am trying to figure it out" doesn't tell us anything about what you are trying to do ,
Part of learning how to use the forum , is not asking us to write your code but asking us how you can write some specific code or use some kind of statement.

I meant I'm reading on the subject. I'm really new so I don't really know much. But yes, Learning!

How can I use the SwitchCase command in this situation? Specially if I only need the stepper to do that amount of steps ONCE per click?

This is an example

switch (state) {
    case 1:   //state high
   myStepper.step(10800);
  delay(500);
      break;
    case 2: //state low
      myStepper.step(-10800);
  delay(500);
      break;
 delay (10)
  }

On this case my variable will be my state, right?.

Hey man, thanks a lot for helping me out. You're amazing.

I really feel a little lost right now, but I think I'm getting somewhere.

You declared it as a variable here didn't you ?

 int state = HIGH;      // the current state of the output pin

Doesn't that make it a variable ?
Is there anything else that is changing or determining the program flow ?

then i guess I can use the statePin? no?

I can use the statePin as a variable because it changes from LOW to HIGH with the state int.

The LED starts on HIGH at the beginning of the program so then I get my x number of steps and then when it changes to low then i get -x steps.

Now wouldnt this switch case function make the stepper go on an x amount of steps and then loop again and make it take the x amount of steps again and again? I only need it once.

You need to declare a boolean flag that is initialized to "false" and set to "true" inside the case statement right after the stepper move command. Then you need to do a boolean AND with the flag so if flag ==false, then move . If flag ==true then the move is already complete (you could name if "moveComplete") You may need another flag but not sure yet. Try it and see what happens.

Make a Truth table that includes the Initial state on power up.

As a training exercise, add led I/O outputs to serve as visual flag indicators. You can also use Serial.print commands.
You can have the following Led indicators:
1- Motor at "HOME" position
2- Motor at "TARGET" position
3- Move Complete
4- STATE (already implemented)

I'm sorry dude. I feel like an idiot right now but I "get" what you want me to do.

Set up a flag as false (STOP flag)
then after the code
if flag is stop then go Forward
then flag is false.

I think thats the first part on how to do it.

But I just can't get it on my small head. any more hints you can give me? I'm really want to understand this.

The reason you can't get it is because you haven't made a Truth Table (or you would have posted it) which means you think it is too much trouble, so now you have to stay stuck in the mud until you finally realize that Truth Tables wouldn't exist if they weren't useful. I'm not going to give you any shortcut because if you haven't made a truth table it is because you are too lazy.
If you are too lazy then you have made your own bed. Sleep in it. When you post a truth table that include initial power up conditions then maybe we can make some progress. Sometimes there aren't any shortcuts.
I'll give you a hint. Instead of STOP /START, think SET /RESET. (SOUND FAMILIAR? (FLIP-FLOP))

be nice he only a beginner point to info he will need