Should I use the blink without delay?

Hello all,

I have created this code:

const int m1r=2;
const int m1l=3;
const int m2f=9;
const int m2b=11; 
const int threshold = 400;
const int sensorMin = 100;

void setup(){
   pinMode(m1r, OUTPUT);
    pinMode(m1l, OUTPUT);
     pinMode(m2f, OUTPUT);
      pinMode(m2b, OUTPUT);
}
void loop(){
  int analogValue = analogRead(0);
    
 if (analogValue > threshold) {
  
   
     digitalWrite(m2b,LOW);
  digitalWrite(m2f,HIGH);
  
   delay(1000);
   
   digitalWrite(m2b,LOW);
   digitalWrite(m2f,LOW);
   
   digitalWrite(m1r,HIGH);
   digitalWrite(m1l,HIGH);
   
   
   
  }
  
 
else{
  
  digitalWrite(m2f,LOW);
  digitalWrite(m2b,HIGH);
digitalWrite(m1r,HIGH);
   digitalWrite(m1l,HIGH);
}

}

If I upload the code when the if statement is used , the arduino only does the code before the delay. Why?

While where at it, can I put a default value to my pins.(example pin m2f==HIGH unless changed)

Thanks

the arduino only does the code before the delay

How can you tell? Have you tried putting Serial.println("whatever"); immediately after the delay?

if m1r, m1l, m2b, and m2f mean motor 1 right, motor 1 left, motor 2 forward, and motor 2 backwards, this code:

     digitalWrite(m2b,LOW);
  digitalWrite(m2f,HIGH);

looks like it sends the device forward.

After 1 second, this code is executed:

   digitalWrite(m2b,LOW);
   digitalWrite(m2f,LOW);
  
   digitalWrite(m1r,HIGH);
   digitalWrite(m1l,HIGH);

This appears to turn off the "go forward" motor, and turn on the "go left" AND "go right" motors.

Is your device capable of going right and left simultaneously?

Have you tried a simple sketch like:

void loop()
{
   digitalWrite(m1r, HIGH);
   digitalWrite(m1l, LOW);
   digitalWrite(m2f, LOW);
   digitalWrite(m2b, LOW);
   delay(1000);

   digitalWrite(m1r, LOW);
   digitalWrite(m1l, HIGH);
   digitalWrite(m2f, LOW);
   digitalWrite(m2b, LOW);
   delay(1000);

   digitalWrite(m1r, LOW);
   digitalWrite(m1l, LOW);
   digitalWrite(m2f, HIGH);
   digitalWrite(m2b, LOW);
   delay(1000);

   digitalWrite(m1r, LOW);
   digitalWrite(m1l, LOW);
   digitalWrite(m2f, LOW);
   digitalWrite(m2b, HIGH);
   delay(1000);

   digitalWrite(m1r, LOW);
   digitalWrite(m1l, LOW);
   digitalWrite(m2f, LOW);
   digitalWrite(m2b, LOW);
   delay(1000);
}

to verify that each motor works, and spins in the correct direction?

AND DOESN'T COMPLETE WITH:

How do you know that it doesn't? As drhex suggested, add a Serial.println() after the delay, and you will see that it DOES get there.

Then, you can figure out why the code after the delay doesn't appear to do anything.

[edit]By the way, if you want to move forward or backwards, left or right, smartly (without hitting things), then, yes, you WILL need to get rid of the delays.[/edit]

The motors work, I have tested them all.

This appears to turn off the "go forward" motor, and turn on the "go left" AND "go right" motors.

Is your device capable of going right and left simultaneously?

Actually the motors are activated when the pin is LOW( I am using an external circuit)

The motors work, I have tested them all.

With code on the Arduino? Or by applying external power and ground?

Put this subroutine at the end of your code...

void stop_program_here() {
  Serial.println("Program Stopped");
  for(;;);
}

And then go through your code bit by bit and tell us what state each output is in.

To do this just put this line in your code where you want it to stop...

stop_program_here();

It may be possible that you are using the wrong analogue pin, or don't have a common ground to the sensor.

You may want to print out to the serial monitor what value the analogue input is reading, see if it is what you expect.