Go Down

Topic: Should I use the blink without delay? (Read 847 times) previous topic - next topic

thewine

Hello all,

I have created this code:

Code: [Select]
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

drhex

#1
Sep 06, 2010, 03:30 pm Last Edit: Sep 06, 2010, 03:33 pm by drhex Reason: 1
Quote
the arduino only does the code before the delay


How can you tell?  Have you tried putting[font=Courier] Serial.println("whatever"); [/font]immediately after the delay?

PaulS

if m1r, m1l, m2b, and m2f mean motor 1 right, motor 1 left, motor 2 forward, and motor 2 backwards, this code:
Code: [Select]
    digitalWrite(m2b,LOW);
 digitalWrite(m2f,HIGH);

looks like it sends the device forward.

After 1 second, this code is executed:
Code: [Select]
  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:
Code: [Select]
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?

PaulS

#3
Sep 06, 2010, 03:50 pm Last Edit: Sep 06, 2010, 03:52 pm by PaulS Reason: 1
Quote
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]

thewine

The motors work, I have tested them all.

Code: [Select]
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)

PaulS

Quote
The motors work, I have tested them all.

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

InvalidApple

Put this subroutine at the end of your code...
Code: [Select]

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...
Code: [Select]

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.

Go Up