hello
I need some help, I have set up a program that should work like this:
motor 1 and motor 2 driving forward until it hit a contact and then motor 1 drive back while motor 2 is running the same way " always the same derrection forward and back" until it hit contact 2 then both motors stop and do not run again.
but when it hits contact 2 then motor 1 start again forward, how do I make both motor stop when it hit contact 2?
// Definerer pin-numrene for motorerne og kontaktene
const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;
void loop() {
// Kører motor 2 fremad hele tiden
digitalWrite(motor2Pin, HIGH);
// Kører motor 1 fremad, indtil kontakt 1 rammes
while (digitalRead(contact1Pin) == LOW) {
digitalWrite(motor1ForwardPin, HIGH);
digitalWrite(motor1BackwardPin, LOW);
}
// Kører motor 1 baglæns, indtil kontakt 2 rammes
while (digitalRead(contact2Pin) == LOW) {
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, HIGH);
}
// Stopper motor 1 når kontakt 2 rammes
if
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, LOW);
// Vent lidt for at undgå at overbelaste Arduino'en med konstant at tjekke kontakter
delay(100);
}
When you paste your code, be sure to click the < CODE > button so your code looks like this:
// Definerer pin-numrene for motorerne og kontaktene
const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;
void setup() {
// Sætter pin-mode for motorer og kontakter
pinMode(motor1ForwardPin, OUTPUT);
pinMode(motor1BackwardPin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(contact1Pin, INPUT_PULLUP);
pinMode(contact2Pin, INPUT_PULLUP);
}
void loop() {
// Kører motor 2 fremad hele tiden
digitalWrite(motor2Pin, HIGH);
// Kører motor 1 fremad, indtil kontakt 1 rammes
while (digitalRead(contact1Pin) == LOW) {
digitalWrite(motor1ForwardPin, HIGH);
digitalWrite(motor1BackwardPin, LOW);
}
// Kører motor 1 baglæns, indtil kontakt 2 rammes
while (digitalRead(contact2Pin) == LOW) {
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, HIGH);
}
// Stopper motor 1 når kontakt 2 rammes
if
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, LOW);
// Vent lidt for at undgå at overbelaste Arduino'en med konstant at tjekke kontakter
delay(100);
}
Also, something happened here.... verify with your original code and correct this:
// Defined pin-numrene for motorerne and concact
const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;
void setup() {
// put pin-mode for motorer og contact
pinMode(motor1ForwardPin, OUTPUT);
pinMode(motor1BackwardPin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(contact1Pin, INPUT_PULLUP);
pinMode(contact2Pin, INPUT_PULLUP);
// motor 1 forward, until contact 1 hits
while (digitalRead(contact1Pin) == LOW) {
digitalWrite(motor1ForwardPin, HIGH);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, HIGH);
}
// motor 1 drivning bag until concact 2 har hit
while (digitalRead(contact2Pin) == LOW) {
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, HIGH);
}
// Stop motor 1 and 2 when contact 2 are push
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, LOW);
}
void loop() {
}
thank you, it works.
the only thing now is that motor 2 don't work, do you have any idea? the new code is:
// Defined pin-numrene for motorerne and concact
const int motor1ForwardPin = 2;
const int motor1BackwardPin = 3;
const int motor2Pin = 4;
const int contact1Pin = 5;
const int contact2Pin = 6;
const int enA = 7;
const int enB = 8;
void setup() {
// put pin-mode for motorer og contact
pinMode(motor1ForwardPin, OUTPUT);
pinMode(motor1BackwardPin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(contact1Pin, INPUT_PULLUP);
pinMode(contact2Pin, INPUT_PULLUP);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
// motor 1 forward, until contact 1 hits
while (digitalRead(contact1Pin) == LOW) {
digitalWrite(motor1ForwardPin, HIGH);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(enA, HIGH);
digitalWrite(enB, HIGH);
}
// motor 1 drivning bag until concact 2 har hit
while (digitalRead(contact2Pin) == LOW) {
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, HIGH);
}
// Stop motor 1 and 2 when contact 2 are push
digitalWrite(motor1ForwardPin, LOW);
digitalWrite(motor1BackwardPin, LOW);
digitalWrite(motor2Pin, LOW);
}
void loop() {
}```
Why is your motor2 different than motor1? For motor1, you have a forward and backward pin but not for motor2. Are they both connected to the same l298 driver? (channel A and channel B).
its because motor 1 have to drive forward and back, motor 2 have to drive the same way all the time. yes they are both connected to the same l298n driver
But did you notice that on motor1, when driving forward, you set one pin HIGH and the other LOW? That still holds true for motor2 as well. That is how an H-bridge operates. Think of it as tying both sides of the motor as either HIGH or LOW. That is what enables forward or backward motion. If you only tie one side of motor2 HIGH, the other side still is not connected so you haven't completed the circuit.