Need stepper ON only while switch is pushed

Hi All, hope you all had a good 4th of July.
Now, I have this simple stepper motor sketch which I use I2C to run the show. ( I used I2C just to familiarize myself with I2C ).

As you can see in the code, the stepper motor spins till the stop switch is pushed.

Now, I want to change the code.

I want the motor to spin ONLY when switch1Pin or switch2Pin are pushed down. I want the motor to turn only while the switch is being pushed down and I want it to stop the moment I take the finger off the switch.

How do I do that?

Do I use "While" statements instead of an "if" statement?

I do NOT have the slave sketch. I downloaded it on a pro-mini and then deleted it from my laptop. All I remember is that case 0 is stop, case 1 is one directon and case 2 is the other direction.

Thank you for your replies!

#include <Wire.h>

const int stopSwitch = 9;
const int switch1Pin = 10;
const int switch2Pin = 11;

int switch1State = 0;     //Variable to store the state of switch 1.
int switch2State = 0;     //Variable to store the state of switch 2.
int stopSwitchState = 0;  //Variable to store the state of the stop switch.

void setup() {
  Serial.begin(9600);
  Wire.begin(); // Lack of an argument in the Parentheses indicates that it is a MASTER.
  // If it said Wire.begin(0x05), that would be used to set the device as
  // a slave with the address 0x05.
  pinMode(3, OUTPUT); // Sets pin 3 as output.
  pinMode(4, OUTPUT); // Sets pin 4 as output.

  pinMode(9, INPUT);  // Sets pin 9 as input.
  pinMode(10, INPUT); // Sets pin 10 as input.
  pinMode(11, INPUT); // Sets pin 11 as input.
}

void loop() {

  switch1State = digitalRead(10);
  switch2State = digitalRead(11);
  stopSwitchState = digitalRead(9);

  if (switch1State == LOW && switch2State == LOW)
  {
    Serial.println("Both the switches are LOW");
     }
  if (switch1State == HIGH && switch2State == HIGH)
  {
    Serial.println("Both the switches are HIGH");
    
  }
  if (switch1State == HIGH && switch2State == LOW)
  {
    digitalWrite(3, HIGH);
    delay(300);
    digitalWrite(3, LOW);
    delay(300);

    Serial.println("Switch 1 was just pushed. Switch 1 has gone HIGH!");
    Wire.beginTransmission(66);
    Wire.write(1);
    Wire.endTransmission();
    delay(10);
  }
  if (switch1State == LOW && switch2State == HIGH)
  {
    digitalWrite(4, HIGH);
    delay(300);
    digitalWrite(4, LOW);
    delay(300);

    Serial.println("Switch 2 was just pushed. Switch 2 has gone HIGH!");
    Wire.beginTransmission(66);
    Wire.write(2);
    Wire.endTransmission();
    delay(10);
  }
  if (stopSwitchState == HIGH)
  {
    Serial.println("The stop switch has gone HIGH. The stepper should stop rotating any moment! ");
    Wire.beginTransmission(66);
    Wire.write(0);
    Wire.endTransmission();
    delay(50);
  }
}

what's special about the 4th of July? waiting for the 14th here... :innocent: :grinning_face_with_smiling_eyes:

which switch are you talking about, you have 3 of them.

as this code is only transmitting commands, you should transmit a "run" command and then a "stop" command when it's time. The receiver should just do what it's told to do.

What's the difference between the 2 button combinations, LOW+HIGH and HIGH+LOW?
Fwd and bwd?

One button makes the motor spin CW and the other buttonmakes it spin CCW.

Fine.
Use a construction like:

if( forward) {
go_Forward ();
}
else if (backward) {
go_Backward)
}
else {
go_Stop();
}

Ty. railroader....I will go with the following. Ty

 if (switch1State == HIGH)
  {
    Serial.println("Switch 1 was just pushed. Switch 1 has gone HIGH!");
    Wire.beginTransmission(66);
    Wire.write(1);
    Wire.endTransmission();
    delay(10);
  }
  else if (switch2State == HIGH)
  {
    Serial.println("Switch 2 was just pushed. Switch 2 has gone HIGH!");
    Wire.beginTransmission(66);
    Wire.write(2);
    Wire.endTransmission();
    delay(10);
  }
 else (stopSwitchState == HIGH)
  {
    Serial.println("The stop switch has gone HIGH. The stepper should stop rotating any moment! ");
    Wire.beginTransmission(66);
    Wire.write(0);
    Wire.endTransmission();
    delay(50);
  }
}

1 Like

If You have that stop switch installed the stop switch will stop the running regardless of the buttons. However the motor will continue running when the fwd/bwd button is released.
One more else is needed.

I would like something this:

else if(stopSwitchState == HIGH)// You forgot the "if"!!!!!!!!
{
Serial.println("The stop switch has gone HIGH. The stepper should stop rotating any moment! ");
Wire.beginTransmission(66);
Wire.write(0);
Wire.endTransmission();
delay(50);
}
else { //No button pressed, no stop switch.
Wire.beginTransmission(66);
Wire.write(0);
Wire.endTransmission();

}