INPUT_PULLUP explanation

const int richtingPin = 8;
const int stappenPin = 7;
const int knoplinksdraaien = A0;
const int knoprechtsdraaien = A1;
const int knopuit = A2;

void setup(){
pinMode(richtingPin,OUTPUT);
pinMode(stappenPin,OUTPUT);
pinMode(knoplinksdraaien,INPUT_PULLUP);
pinMode(knoprechtsdraaien,INPUT_PULLUP);
pinMode(knopuit,INPUT_PULLUP);
Serial.begin(9600);

stepRechts(false,32000);

}

void loop (){

if(digitalRead(knoprechtsdraaien)==LOW){
Serial.println("rechter knop ingedrukt");
stepLinks(false,48);
}

if(digitalRead(knoplinksdraaien)==LOW){
Serial.println("linker knop ingedrukt");
stepRechts(true,48);
}
}

void stepLinks(boolean richting,int aantalstappen){
digitalWrite(richtingPin,richting);
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){
digitalWrite(richtingPin,richting);
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);

}

}

Do you have a question?

Code tags?

You have two virtually absolutely identical functions. Why?

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

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);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.