hallo I want to implement the Button in the loop, so that when it pressed the Motor stops. but it doesn't work as I want. could you please help with advice.
void loop() {
button.loop(); // MUST call the loop() function first
int btnState = button.getState();
for(int x = 0; x < 500 ; x++){
if (btnState==HIGH){
digitalWrite(dirPin,HIGH);
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
else { break;}}
}
hello ,
yes. that was the solution. I changed the code as following and it works now. many thanks for the great quick advice.
void loop() {
for(int x = 0; x < 500 ; x++){
button.loop(); // MUST call the loop() function first
int btnState = button.getState();
if (btnState==HIGH){
digitalWrite(dirPin,LOW);
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);}
else { break;}
}
}
You already have the other way. Your loop is inside the Arduino loop function. Every time your loop completes after 500 iterations, it just starts again when loop() resumes. So you can just remove the 'for' loop. It does nothing.
I'm not posting the code because I want you to learn from it.