DigitalRead DigitalWrite

Hallo,

Ive one question because ive stuck in my project.
I have a driver for 24DC motor in which i can use 5V input to move it forward or backward.
I want to use arduino, when i use delay() then it moves for example 2 sec, but when i want to make a kind of 0 piont movement - 2 sec forward and then move till it reach the sensor it moves only forward but not back. What do i wrong?
here is me code:

int FWPin = 3; 
int BWPin = 4; 
int SpeedPin = 5;
int StopPin = 6;

void setup()
{
  pinMode(FWPin,OUTPUT);
  pinMode(BWPin,OUTPUT); 
  pinMode(SpeedPin,OUTPUT);
  pinMode(StopPin,OUTPUT);
  pinMode(2,INPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
}

void loop()
{
 if (digitalRead(9) == LOW)
 {
         digitalWrite(FWPin,HIGH); 
         delay(2000);
         digitalWrite(FWPin,LOW);
         digitalWrite(BWPin,HIGH);
         
   if (digitalRead(9) == HIGH)
      {
        digitalWrite(BWPin,LOW);
      }
         
 }

Can anybody help me?
thank you

Greatings
bart

As long as pin 9 is HIGH, set FWPin HIGH, keep it HIGH for 2 seconds, set it LOW, set BWPin HIGH, then start from beginning.

Both FWPin and BWPin are HIGH at this point

Is that your complete program ?
It does not compile.
How are the input pins wired ?

  if (digitalRead(9) == LOW)
  {
    digitalWrite(FWPin, HIGH);
    delay(2000);
    digitalWrite(FWPin, LOW);
    digitalWrite(BWPin, HIGH);
    if (digitalRead(9) == HIGH)

What is connected to pin 9 and how long does it stay LOW ? If it is more than 2 seconds then it will not be HIGH when tested for the second time.

This if:

   if (digitalRead(9) == HIGH)
      {
        digitalWrite(BWPin,LOW);
      }

Is inside the if that checks that pin 9 is LOW. So you only check to see if it's HIGH, if it was LOW two seconds ago. Is that right? It might be, depending on what you're trying to do, but I suspect not.

This is full code

int FWPin = 3; 
int BWPin = 4; 
int SpeedPin = 5;
int StopPin = 6;

void setup()
{
  pinMode(FWPin,OUTPUT);
  pinMode(BWPin,OUTPUT); 
  pinMode(SpeedPin,OUTPUT);
  pinMode(StopPin,OUTPUT);
  pinMode(2,INPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
}

void loop()
{
 if (digitalRead(9) == LOW)
 {
         digitalWrite(FWPin,HIGH); 
         delay(2000);
         digitalWrite(FWPin,LOW);
         digitalWrite(BWPin,HIGH);
         
   if (digitalRead(9) == HIGH)
      {
        digitalWrite(BWPin,LOW);
      }
         
 }
 if (digitalRead(2) == HIGH)
 {
               digitalWrite(FWPin,HIGH); 
               delay(5000);
               digitalWrite(FWPin,LOW);

               digitalWrite(FWPin,HIGH);
               digitalWrite(SpeedPin,HIGH);
               delay(2000);
               digitalWrite(FWPin,LOW);
               delay(3000);
               digitalWrite(BWPin,HIGH); 
               delay(2000);
               digitalWrite(BWPin,LOW);
               delay(3000);
               
               digitalWrite(FWPin,HIGH);
               delay(3000);
               digitalWrite(FWPin,LOW);
               delay(3000);
               digitalWrite(BWPin,HIGH); 
               delay(3000);
               digitalWrite(BWPin,LOW);
               delay(3000);

               digitalWrite(FWPin,HIGH);
               delay(4000);
               digitalWrite(FWPin,LOW);
               delay(3000);
               digitalWrite(BWPin,HIGH); 
               delay(4000);
               digitalWrite(BWPin,LOW);
               delay(3000);
               
               digitalWrite(BWPin,HIGH);
               delay(500);
               digitalWrite(BWPin,LOW);
               digitalWrite(SpeedPin,LOW);
               delay(1000);

               digitalWrite(BWPin,HIGH); 
               if (digitalRead(9) == HIGH);
               {
               digitalWrite(BWPin,LOW);
               digitalWrite(StopPin,HIGH);
               delay(100);
               digitalWrite(StopPin,LOW);
               delay(200);
               }
              
}
 if (digitalRead(8) == HIGH)
 {
               digitalWrite(FWPin,HIGH); 
               delay(5000);
               digitalWrite(FWPin,LOW);
               delay(100);
               digitalWrite(FWPin,HIGH);
               digitalWrite(SpeedPin,HIGH);
               delay(4000);
               digitalWrite(FWPin,LOW);
               delay(3000);
               digitalWrite(BWPin,HIGH); 
               delay(4000);
               digitalWrite(BWPin,LOW);
               digitalWrite(SpeedPin,LOW);
               delay(100);
               digitalWrite(BWPin,HIGH); 
               if (digitalRead(9) == HIGH);
               {
               digitalWrite(BWPin,LOW);
               digitalWrite(StopPin,HIGH);
               delay(100);
               digitalWrite(StopPin,LOW);
               delay(200);
               }
               
}

}

on begin of the program i wanted to set a 0 point so make a movement 2 sec forward and then bach til pin 9 is high - as a reference point to start making other movements.

then if 2 is pressed it have to make some movements en go back to the 0 point and the same when 8 is pressed

UKHeliBob:
Is that your complete program ?
It does not compile.
How are the input pins wired ?

  if (digitalRead(9) == LOW)

{
    digitalWrite(FWPin, HIGH);
    delay(2000);
    digitalWrite(FWPin, LOW);
    digitalWrite(BWPin, HIGH);
    if (digitalRead(9) == HIGH)




What is connected to pin 9 and how long does it stay LOW ? If it is more than 2 seconds then it will not be HIGH when tested for the second time.

The wiring is pin 2 8 and 9 they are connected like a buttons(with 330 ohm resistor) - they are accualy a buttons( 9 is endswitch, 2 and 8 are buttons to make an acction)
pins 3,4,5, and 6 are dirrectly conetcted to driver + GND to GND.

So the program have to check: Is 9 HIGH - yes -> ok, no -> have to move 2 sec forward and then back so long to make 9 HIGH, then the program waits for next command.

When i use delay() it goes back for time i set but in the back option i don`t want to use time, i want to make it going back to endswitch.