If you want to do something when the pin the button is sewn onto changes state, then looking at the state change detection example seems like a reasonable thing to do.
It's just doing what you asked. When the step counter equals DISTANCE it sets Stepping false then goes to the top of the loop and immediately sets Stepping true again if the button is still pressed.
You need to be looking for the button changing to pressed not just being in the pressed state. Have a look at the Digital / StateChangeDetection example sketch in the IDE.
int StepCounter = 0;
int Stepping = false;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
if (Stepping == true)
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);
The code in Reply #6 won't work because there is a variable called buttonPin that has not been defined.
To make it easy for people to help you please use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum
#define DISTANCE 200
int StepCounter = 0;
int Stepping = false;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
pinMode(3,INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(3) == LOW && Stepping == false)
{
Stepping = true;
}
if (Stepping == true)
buttonState = digitalRead(3);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}
The code in lines 29 to 46 seems to have no relationship with any other part of the program
It would have been better to keep the variable buttonPin but define it to be 3. Code such as buttonState = digitalRead(buttonPin); makes more sense than buttonState = digitalRead(3);. And if the value is just defined in one place there is much less scope for silly typos that the compiler cannot detect.