push button problem

Hi, I want to start my loop until it reaches if statement of the push button then stop the loop until the button pressed . could anyone help me . Thanks.

What loop? What if statement? In what program? Rule #1 when asking for help with a program is to show the folks you want help from the program you're talking about.

Do you want something like this?

while(digitalRead(buttonPin) == HIGH){} // Do nothing while button not pressed

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.

simple state machine example (un-tested):

enum State {
  GO,
  STOP
};
const byte buttonPin = 5;

State state = STOP;
byte lastButtonPressed = 1;

void setup() 
{
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() 
{
  bool buttonPressed = digitalRead(buttonPin);
  if (buttonPressed != lastButtonPressed)
  {
    if (lastButtonPressed = 1)
    {
      if (state == GO)
      {
        state = STOP;
      }
      else
      {
        state = GO;
      }
    }
  }
  delay(25); //crude debounce
  lastButtonPressed = buttonPressed;
  if (state == GO)
  {
    // do "GO" stuff
  }
  else if (state == STOP)
  {
    // do nothing or do something else
  }
}

This is my code .

const int stepPin = 5; 
const int dirPin = 4; 
const int enable = 6;
int pushbutton = 3;
int vacum = 13;
char value;


void setup() {
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(enable,OUTPUT);
  pinMode(vacum,OUTPUT);
  pinMode(pushbutton, INPUT);
  Serial.begin(115200);
}
void loop() {
  if (Serial.available())
  {
    value = Serial.read();
    {
      if (value == '1')  
      {
        for (int i = 0; i != 10; i++)
        {
         
           counterclockwise ();
           delay(500);
           { 
              if (digitalRead(pushbutton) == HIGH) {
              digitalWrite(vacum,HIGH);
              delay(2000);
              clockwise ();
              }
           };
          Serial.print ( " Product number " ) ;
          Serial.println(i);
        }
      }
    }
  }
}

hsalame: This is my code .

you want to try to get it to compile?

I want to stop the loop before if statement of push button and wait until the button pressed .

What's with all the extra { and } in your code? Do you know what those do and where they are needed? If not, finding out would be a lot better than just randomly sprinkling them throughout your code.

hsalame: I want to stop the loop before if statement of push button and wait until the button pressed .

See reply #1, although it assumes an active LOW button whereas your seem to be active HIGH. An empty while loop like that will just keep checking the button until it is pressed.

Bulldoglowell could you compile your code with mine ? Thanks.

BulldogLowell: you want to try to get it to compile?

I don't know how to compile them together could you help me . Thanks.

hsalame: I don't know how to compile them together could you help me . Thanks.

The empty while loop would be a much simpler and easier to work with for you if all you want to do is block the program until a button is pressed. Using that state machine approach would require a rewrite of most of your code. If you want to use the state machine approach then don't try to combine the two codes but rather look at how that code works and try to use the strategy it employs to write your own version.

Ok ,thanks a lot .