Pages: [1]   Go Down
Author Topic: Should I use the blink without delay?  (Read 738 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I have created this code:

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
Logged

Knivsta,Sweden
Offline Offline
Sr. Member
****
Karma: 0
Posts: 274
Low level's cool
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the arduino only does the code before the delay

How can you tell?  Have you tried putting Serial.println("whatever"); immediately after the delay?
« Last Edit: September 06, 2010, 08:33:55 am by drhex » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if m1r, m1l, m2b, and m2f mean motor 1 right, motor 1 left, motor 2 forward, and motor 2 backwards, this code:
Code:
    digitalWrite(m2b,LOW);
  digitalWrite(m2f,HIGH);
looks like it sends the device forward.

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: September 06, 2010, 08:52:02 am by PaulS » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The motors work, I have tested them all.

Code:
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)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The motors work, I have tested them all.
With code on the Arduino? Or by applying external power and ground?
Logged

Melbourne, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 219
Have you tried turning it off and on again?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put this subroutine at the end of your code...
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...
Code:
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.
Logged

Pages: [1]   Go Up
Jump to: