Go Down

Topic: Want to exit loop when I push a button, please help (Read 97 times) previous topic - next topic

MofoSchenked

Hi,

I'm new at programming, and i'm trying to figure out how to exit a for loop when pressing a button.

My scenario is this:

The setup is for a test rig i'm constructing for work.

I have two buttons that are configured to rotate a stepper motor for a certain amount of steps. After I start one of the two programs, I want to be able to push one of the two buttons to stop the test. I then have a LCD (20x4 pixles) that prints out how many cycles i've gone through and some other information.

I have a switch case that determines which button I press. So my thinking is to have a if statement in the end of the for loop that checks to see if the button has been pressed. If it has, then break, if it hasen't, continue through the loop.

This is what I have so far:
Code: [Select]
   
case 3:
  for (int j = 0; j < 10; j++){
    TIMER();
    b ++;
    c ++;     
    lcd.setCursor(14, 2);
    lcd.print(b);
    myStepper.setSpeed(255);
    myStepper.step(200);
    delay(50);
  }
  b = 0;
  lcd.setCursor(14,2);
  lcd.print("      ");
  lcd.setCursor(14,3);
  lcd.print(c);
break;



b counts how many cycles the current loop is doing. c counts the total of cycles since the program was started. The TIMER(); function is a timer that starts counting when I press either one of the buttons.


I have tried this, but without good results, it exits the loop after only 1 cycle.

Code: [Select]
   
case 3:
  for (int j = 0; j < 10; j++){
    TIMER();
    b ++;
    c ++;     
    lcd.setCursor(14, 2);
    lcd.print(b);
    myStepper.setSpeed(255);
    myStepper.step(200);
    delay(50);
      if(digitalRead(WhiteButton) == HIGH){
        break;
      }
  }
  b = 0;
  lcd.setCursor(14,2);
  lcd.print("      ");
  lcd.setCursor(14,3);
  lcd.print(c);
break;

/Mofo

johnwasser

If you want it to be able to exit before the first cycle, how about:
Code: [Select]
    
case 3:
  for (int j = 0; j < 10; j++){
      if(digitalRead(WhiteButton) == HIGH){
        break;
    TIMER();
    b ++;
    c ++;      
    lcd.setCursor(14, 2);
    lcd.print(b);
    myStepper.setSpeed(255);
    myStepper.step(200);
    delay(50);
      }
  }
  b = 0;
  lcd.setCursor(14,2);
  lcd.print("      ");
  lcd.setCursor(14,3);
  lcd.print(c);
break;
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

MofoSchenked

Thank you for the reply,

as you say, this code "only" exits the first cycle about 30% of the time (might be a problem with my hardware?). After it has done that, it restarts and counts from 0 again..


/Mofo

groundFungus

#3
Nov 09, 2018, 03:26 pm Last Edit: Nov 09, 2018, 03:29 pm by groundFungus
How is the switch wired? 

The accepted way to wire a switch is to wire the switch to ground and an input.  Set the pinMode of the input to INPUT_PULLUP.  The switch will read HIGH when not pushed and LOW when pushed.  Adjust the logic in the sketch as necessary.

Go Up