How do I convert this to switch case?

Hi

I have this code, where I press button 1 and motor 1 runs x amount of steps. I press button 2 and motor 2 runs x amount of steps. The only problem is, if I hold button 1 down, it will continue to run motor 1. I tried to use switch case, but I just can’t get it too work. Can anyone help me out? TIA

// motor 1
const int dirPin  = 1;
const int stepPin = 2;
const int enPin   = 3;
const int switchOne = 12; // button 1
int p1buttonState = 0; // current state of the button

// motor 2
const int dirPin2 = 4;
const int stepPin2 = 5;
const int enPin2 = 6;
const int switchTwo = 9; //button 2
int p2buttonState = 0; // current state of the button



void setup() {
  Serial.begin(9600);
  
  //motor 1
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(enPin,OUTPUT);
  pinMode( switchOne, INPUT_PULLUP);
  
  //motor 2
  pinMode(dirPin2, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(enPin2, OUTPUT);
  pinMode( switchTwo, INPUT_PULLUP);
  

}

void motor1()
{
   p1buttonState = digitalRead(switchOne); 
  if (p1buttonState == LOW)// when button 1 is pushed, motor 1 will run
  {
    digitalWrite(dirPin,HIGH); //changes direction
    delay(5);
      for(int x = 0; x < 1000; x++) // 200 steps for 1 round
      {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      }
    }
    else (digitalRead(switchOne)); // if not pushed, motor 1 will not run
    (p1buttonState == HIGH);
    {
      digitalWrite(dirPin, LOW);
    }
}


void motor2()
{
   p2buttonState = digitalRead(switchTwo);
    if (p2buttonState == LOW) // when button 2 is pushed, motor 2 will run
    {
      digitalWrite(dirPin2,LOW); //changes direction
      delay(5);
      for(int x = 0; x < 5000; x++) //200 steps for 1 round
      {
        digitalWrite(stepPin2,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin2,LOW);
        delayMicroseconds(500);
      }
    }
    else (digitalRead(switchTwo)); // if not pushed, motor 2 wil not run
    (p2buttonState == HIGH);
    { 
      digitalWrite(dirPin2, LOW);
    }
}
    



void loop() 
{
  motor1();
  motor2();
}

The only problem is, if I hold button 1 down, it will continue to run motor 1.

I do not think that changing to switch case will help. You need to sense when the button becomes pressed (the HIGH to LOW transition) not when the button is pressed (the LOW state). The state change detection tutorial shows how.

Here is a one stepper example that show what I meant in reply #1. Note pins changed to match my test setup (Uno, CNC shield w/A4988 drivers, NEMA 17 bipolar stepper). Hold the button down, the motor runs only once. Will not run again till button released and pressed again. Second stepper just copy variables with different names.

// motor 1
const int dirPin  = 5;
const int stepPin = 2;
const int enPin   = 8;
const int switchOne = 9; // button 1
boolean p1buttonState = HIGH; // current state of the button
boolean p1buttonLastState = HIGH;

void setup()
{
   Serial.begin(9600);
   pinMode(stepPin, OUTPUT);
   pinMode(dirPin, OUTPUT);
   pinMode(enPin, OUTPUT);
   digitalWrite(enPin, LOW);
   pinMode( switchOne, INPUT_PULLUP);
   Serial.println("stepper run on button press");
}

void motor1()
{
   digitalWrite(dirPin, HIGH); //changes direction
   delay(5);
   for (int x = 0; x < 1000; x++) // 200 steps for 1 round
   {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
   }
}

void loop()
{
   p1buttonState = digitalRead(switchOne);
   if (p1buttonState != p1buttonLastState)
   {
      if (p1buttonState == LOW)// when button 1 is pushed, motor 1 will run
      {
         motor1();
      }
      else // if not pushed, motor 1 will not run
      {
         digitalWrite(dirPin, LOW);  // not sure what  this does
      }
      p1buttonLastState = p1buttonState;
   }
}

Robin2's simple stepper code may be of interest. It has an example using micros() (or millis()) for timing for non-blocking (no delay()) stepper code.

Thank you groundFungus, this helped a lot!

For anyone interested, this is now the code i use.

// motor 1
const int dirPin  = 1;
const int stepPin = 2;
const int enPin   = 3;
const int switchOne = 12; // button 1
boolean p1buttonState = HIGH; // current state of the button
boolean p1buttonLastState = HIGH;

// motor 2
const int dirPin2 = 4;
const int stepPin2 = 5;
const int enPin2 = 6;
const int switchTwo = 9; //button 2
boolean p2buttonState = HIGH; // current state of the button
boolean p2buttonLastState = HIGH;


void setup() {
  Serial.begin(9600);
  
  //motor 1
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(enPin,OUTPUT);
  pinMode( switchOne, INPUT_PULLUP);
  
  //motor 2
  pinMode(dirPin2, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(enPin2, OUTPUT);
  pinMode( switchTwo, INPUT_PULLUP);
  

}

void motor1()
{
   p1buttonState = digitalRead(switchOne); 
   if (p1buttonState != p1buttonLastState)
   {
  if (p1buttonState == LOW)// when button 1 is pushed, motor 1 will run
  {
    digitalWrite(dirPin,HIGH); //changes direction
    delay(5);
      for(int x = 0; x < 1000; x++) // 200 steps for 1 round
      {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      }
    }
    else // if not pushed, motor 1 will not run
    {
      digitalWrite(dirPin, LOW);
    }
    p1buttonLastState = p1buttonState;
}
}

void motor2()
{
   p2buttonState = digitalRead(switchTwo);
   if (p2buttonState != p2buttonLastState)
   {
    if (p2buttonState == LOW) // when button 2 is pushed, motor 2 will run
    {
      digitalWrite(dirPin2,LOW); //changes direction
      delay(5);
      for(int x = 0; x < 500; x++) //200 steps for 1 round
      {
        digitalWrite(stepPin2,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin2,LOW);
        delayMicroseconds(500);
      }
    }
    else
    { 
      digitalWrite(dirPin2, LOW);
    }
    p2buttonLastState = p2buttonState;
}
}
    



void loop() 
{
  motor1();
  motor2();
}
const int dirPin  = 1;

What Arduino board are you using? Pin 1 is usually the hardware serial (USB) TX pin. If you will want to use the serial port for troubleshooting or program output, you should not use pin 1 for anything else. Use of pin 1 may also effect code uploading.

Change the action to occur after you release the button and not when you press the button.

groundFungus:

const int dirPin  = 1;

What Arduino board are you using? Pin 1 is usually the hardware serial (USB) TX pin. If you will want to use the serial port for troubleshooting or program output, you should not use pin 1 for anything else. Use of pin 1 may also effect code uploading.

I am using arduino uno right now, but you are probably right and I will use another pin than pin 1. Thanks!