INPUT_PULLUP holds the pin at Vcc so it is not in an unknown state between Vcc and GND. When your button is not pressed, it reads HIGH. When you press your button, the pin is then connected through the button to ground, where the pin then reads LOW.
const int richtingPin = 8; // DIRECTION PIN
const int stappenPin = 7; // STEP PIN
const int knoplinksdraaien = A0; // LEFT TURN BUTTON
const int knoprechtsdraaien = A1; // RIGHT TURN BUTTON
const int knopuit = A2; // STOP BUTTON
void setup() {
pinMode(richtingPin, OUTPUT);
pinMode(stappenPin, OUTPUT);
pinMode(knoplinksdraaien, INPUT_PULLUP); // NOTPRESSED = HIGH and PRESSED = LOW
pinMode(knoprechtsdraaien, INPUT_PULLUP); // NOTPRESSED = HIGH and PRESSED = LOW
pinMode(knopuit, INPUT_PULLUP); // NOTPRESSED = HIGH and PRESSED = LOW
Serial.begin(9600);
stepRechts(false, 32000);
}
void loop () {
if (digitalRead(knoprechtsdraaien) == LOW) { // IF PRESSED (IF LOW)
Serial.println("rechter knop ingedrukt"); // RIGHT TURN BUTTON PRESSED
stepLinks(false, 48);
}
if (digitalRead(knoplinksdraaien) == LOW) { // IF PRESSED (IF LOW)
Serial.println("linker knop ingedrukt"); // LEFT TURN BUTTON PRESSED
stepRechts(true, 48);
}
}
void stepLinks(boolean richting, int aantalstappen) { // STEP LEFT (DIRECTION, NUMBER OF STEPS)
digitalWrite(richtingPin, richting); // COUNTER-CLOCKWISE
delay(50);
for (int i = 0; i < aantalstappen; i++) {
Serial.println(i);
if (digitalRead(knopuit) == LOW) {
Serial.println("stop knop");
break;
}
digitalWrite(stappenPin, HIGH);
delayMicroseconds(1200);
digitalWrite(stappenPin, LOW);
delayMicroseconds(1200);
}
}
void stepRechts(boolean richting, int aantalstappen) { // STEP RIGHT (DIRECTION, NUMBER OF STEPS)
digitalWrite(richtingPin, richting); // CLOCKWISE
delay(50);
for (int i = 0; i < aantalstappen; i++) {
Serial.println(i);
if (digitalRead(knopuit) == LOW) {
Serial.println("stop knop");
break;
}
digitalWrite(stappenPin, HIGH);
delayMicroseconds(1200);
digitalWrite(stappenPin, LOW);
delayMicroseconds(1200);
}
}