Help to program

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

Please insert your code using a code tags.

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:

If you want it to run only one time and never again, the easiest way would be to put that code in setup() and leave loop() empty.

I will remember to do that with the code.

It’s a fault were it’s say if😉 that line is deleted

Thanks I will try

it work after putting it on setup instead for loop.
but motor 2 don't stop it keeps running after hitting stop concact

Show your revised code using code tags

I got it to stop now.
a new question:
when I put it all together and youse a l298n bord, in 1 in2 in3 nothing happen, do I need a code for l298n or?

Please use a Google translate

// 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() {
}

was that better?

An l298 also has enable pins (Enable A and EnableB) which you need to connect HIGH to allow that H Bridge to work (A and/or B)

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).

A wiring diagram would be helpful.

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.

of course thank you so much, I will change it.

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