// https://forum.arduino.cc/t/need-help-to-l298n/1265112/
const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;
const int startButton = 7;
const int offLED = 8;
void setup() {
pinMode(motor1ForwardPin, OUTPUT);
pinMode(motor1BackwardPin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(contact1Pin, INPUT_PULLUP);
pinMode(contact2Pin, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
pinMode(offLED, OUTPUT);
digitalWrite(offLED, HIGH); // show OFF state
while (digitalRead(startButton)) {} // wait for BUTTON 7 (start)
digitalWrite(offLED, LOW); // stop OFF state
// MOTOR 1 FORWARD
while (digitalRead(contact1Pin) == HIGH) { // wait for BUTTON 5 (limit 1)
digitalWrite(motor1ForwardPin, HIGH);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, HIGH);
}
// MOTOR 1 REVERSE
while (digitalRead(contact2Pin) == HIGH) { // wait for BUTTON 6 (limit 2)
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, HIGH);
digitalWrite(motor2Pin, HIGH);
}
// STOP MOTOR 1
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, LOW); // disable motor
digitalWrite(offLED, HIGH); // show OFF state
}
void loop() {
// nothing
}FORWARD
REVERSE
LIMIT_1
LIMIT_2
ON
START
OFF