how to use delay

hello, I’m in a group project where we ve been working on this follow line car (i ll let the code down) but the robot doesn t work properly and doesnt go straight by the line and somehow we figgured out that we need to add delay but we tried so many positions and it still doesn t work. can someone please help us and tell where to add delay? or what should we change. thank you xx

/------ Arduino Line Follower Code----- /
/
-------definning Inputs------
/
#define LS A2 // left sensor
#define RS A1 // right sensor
#define CS A0 // center sensor
/-------definning Outputs------/
#define LM1 9 // left motor
#define LM2 6 // left motor
#define RM1 5 // right motor
#define RM2 3 // right motor
void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(CS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
}
void loop()
{
if(!(digitalRead(LS)) && !(digitalRead(RS)) && digitalRead(CS)) // Move Forward
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

if(!(digitalRead(LS)) && digitalRead(RS) && !(digitalRead(CS))) // Turn right
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

if(digitalRead(LS) && !(digitalRead(RS)) && !(digitalRead(CS))) // turn left
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}

if(!(digitalRead(LS)) && !(digitalRead(RS)) && !(digitalRead(CS))) // stop
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}
}

What kind of motor are you using? Stepper or regular DC?

In any case, "adding delay" is not the right approach. It might work in primitive contexts, but you will quickly run into its limitations. You have to learn how to "multitask" simultaneous control of multiple external devices (motors and such) by measuring time steps through millis() function.

Are you sure your logic's correct?

Does this:

 if(!(digitalRead(LS)) && digitalRead(RS) && !(digitalRead(CS)))    // Turn right
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  }

.... reverse the right motor for a right turn?

But it looks to me from the code for forward...

 if(!(digitalRead(LS)) && !(digitalRead(RS)) && digitalRead(CS))    // Move Forward
  {
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, HIGH); //high and low on rm1 and 2 means rm forward...
    digitalWrite(RM2, LOW);
  }

.... that those two lines make the right motor go forward, and you'll have a left turn when you mean to go right....

can someone please help us and tell where to add delay?

I could suggest where you should stick that delay(), but I'll be nice. This time.

Imagine how YOU would follow a line, with a blindfold on, only allowed to sneak a peak once a whateverTimeIntervalYouHadPlannedToDelay. While you are blindly sticking your head in the sand, while continuing to move in the same direction, the line could have wandered all over the place. The next time you sneak a peak, the line may not be anywhere in sight.

Clearly, YOU would not try to follow a line without looking ALL THE TIME. That means that your robot needs to do the same thing, which means NO delay() AT ALL.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

use delay in setup, while waiting for modules to get with the program

in the loop portion of the code, use delay without wasting time

please read the bottom of my signature line