Issues running a DC motor after a series of button presses

Hello,

I am coding a DC motor to spin at a certain speed for a certain time. I am having trouble getting it to run. I want to first press the speed option (button 10) and then have it wait for me to press the time options (button 12 and 13). Below is my code, according to my print statements the code is running the while loop continuously but not running the if statements when I press buttons 12 or 13. Thanks for the help.

int buttonPin8 = 8;
int buttonPin9 = 9;
int buttonPin10 = 10;
int buttonPin12 = 12;
int buttonPin13 = 13;
int motorPin = 3;
bool pressed = false;
int B10_var = 0;



void setup(){
  pinMode(motorPin, OUTPUT);
  pinMode(buttonPin10, INPUT_PULLUP);
  pinMode(buttonPin12, INPUT_PULLUP);
  pinMode(buttonPin13, INPUT_PULLUP);
  Serial.begin(9600);
  while (! Serial);
}

void loop(){
bool currentState10 = digitalRead(buttonPin10);
bool currentState12 = digitalRead(buttonPin12);
bool currentState13 = digitalRead(buttonPin13);

if (currentState10 == pressed){
  B10_var = 1 ;
  
  do{
    Serial.println("step2") ;
    if (currentState12 == pressed){
      analogWrite(motorPin, 255);
      delay(100);
      analogWrite(motorPin, 25);
      delay(2000);
      analogWrite(motorPin, 0);
      B10_var = 0 ;
      Serial.println("step3") ;
          }
            
    else if (currentState13==pressed){
      analogWrite(motorPin, 255);
      delay(100);
      analogWrite(motorPin, 25);
      delay(2000);
      analogWrite(motorPin, 0);
      B10_var = 0;    
      Serial.println("step4") ;  
            }
  }
  while (B10_var == 1);
}

Welcome to the forum

while (B10_var == 1);

This while loop will never end once entered because nothing within it changes the value of B10_var

Inside the if statements there is a

B10_var = 0

because I want to break the loop only when one button 12 or 13 are pressed

Then you need to test for the button presses inside the while loop as that is the only code that is running

1 Like

Hello, @hastinat. As @UKHeliBob told you nothing changes to get out of the vhile loop.

You have written wait but your program does not wait for another button to be pressed and when the if condition passes you do not have button 12 or 13 pressed and you do not exit the vhile loop. You are missing a parenthesis at the end of the program to end a loop (). By the way, it seems to me that your program can work but in a slightly different way than you requested. Try pressing button 12 or 13 and WITHOUT letting go, press button 10. I'm pretty sure that's how your motor works.

currentState12 can not be re-assigned because the "do" loop is not exited, you should replace:

if (currentState12 == pressed){

with

if (digitalRead(buttonPin12) == pressed){

and also, replace:

else if (currentState13==pressed){

with

if (digitalRead(buttonPin12) == pressed){

The button sequence will be "b10"... "step2"..."b12"... "motor run"... "step 3"... "b10"..."step 2"... "b13"... "motor run"... "step 4"

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.