for loop

Hi,
I want my robot to take a turn for 8 seconds and stop. so I wrote the for loop as

const boolean H_EN1 = 2; //It's a PWM pin
const boolean H_EN2 = 5; //It's a PWM pin
const int M1_F = 3;       
const int M1_R = 4;        
const int M2_F = 6;      
const int M2_R = 7;


void setup() 
{
  pinMode(H_EN1,OUTPUT);
  pinMode(H_EN2,OUTPUT);
  pinMode(M1_F,OUTPUT);
  pinMode(M1_R,OUTPUT);
  pinMode(M2_F,OUTPUT);
  pinMode(M2_R,OUTPUT);
  Drive_Right();
}

void loop() 
{                     
  
}


void Drive_Right()
{
  for(int Time = 0; Time < 8000; Time += 1000)
  {
    digitalWrite(H_EN1, LOW); 
    digitalWrite(H_EN2, LOW);
    digitalWrite(M1_F, HIGH);  
    digitalWrite(M1_R, LOW);
    digitalWrite(M2_F, LOW);
    digitalWrite(M2_R, LOW);
    analogWrite(H_EN1, 255);
    digitalWrite(H_EN2, 0); 
  }
}

But the robot continuously keeps on rotating, actually it has to stop after 8 seconds as per the above code. Is my for loop wrong? The time will be in milliseconds, right?

That function will be done in the blink of an eye, how can it last more than 8 seconds?

Just because you call a loop variable Time doesn't give it any special property you can call it anything.

Look at the millis function, that returns a time in milliseconds you can use for timing things.

isn't this :

 for(int Time = 0; Time < 8000; Time += 1000)

the same as this :

 for(int Time = 0; Time < 8; Time += 1)

?????

If there is no other work being done in the sketch, and this is the only sketch, and you are not just clipping out this and really are doing a lot more....

then

 for(int Time = 0; Time < 8000; Time += 1)
delay(1)

is about 8 seconds, plus any other scan time overhead.

But at the end of the 8 seconds, you did not have code to stop!

I modified the code with delay

void Drive_Right()
{
  for(int Time = 0; Time < 8; Time ++)
  {
    digitalWrite(H_EN1, LOW); 
    digitalWrite(H_EN2, LOW);
    digitalWrite(M1_F, HIGH);  
    digitalWrite(M1_R, LOW);
    digitalWrite(M2_F, LOW);
    digitalWrite(M2_R, LOW);
    analogWrite(H_EN1, 255);
    digitalWrite(H_EN2, 0);
    delay(1000);
   }
}

But still cant get the desired output.

I made it :wink:

const boolean H_EN1 = 2; //It's a PWM pin
const boolean H_EN2 = 5; //It's a PWM pin
const int M1_F = 3;       
const int M1_R = 4;        
const int M2_F = 6;      
const int M2_R = 7;


void setup() 
{
  pinMode(H_EN1,OUTPUT);
  pinMode(H_EN2,OUTPUT);
  pinMode(M1_F,OUTPUT);
  pinMode(M1_R,OUTPUT);
  pinMode(M2_F,OUTPUT);
  pinMode(M2_R,OUTPUT);
  Drive_Right();
[b]  delay(8000);
[/b]  Stop();
}

void loop() 
{                     
  
}


void Drive_Right()
{
  
    digitalWrite(H_EN1, LOW); 
    digitalWrite(H_EN2, LOW);
    digitalWrite(M1_F, HIGH);  
    digitalWrite(M1_R, LOW);
    digitalWrite(M2_F, LOW);
    digitalWrite(M2_R, LOW);
    analogWrite(H_EN1, 255);
    digitalWrite(H_EN2, 0); 
}

void Stop() 
{
  digitalWrite(H_EN1, LOW); 
  digitalWrite(H_EN2, LOW);
  digitalWrite(M1_F, LOW); 
  digitalWrite(M1_R, LOW);
  digitalWrite(M2_F, LOW);
  digitalWrite(M2_R, LOW);
  digitalWrite(H_EN1, 0);
  digitalWrite(H_EN2, 0);
}

You can not use bold tags inside code tags :smiley:

And for a robot it might be advisable that you get rid of the delay function and start using millis() based timings. What if it's going forward for 8 seconds and you have no way to stop it when it approaches a wall because the delay will block any further operations.

To use millis(), every time I say go left it should start from 0 and run till 8s. Is it possible to reset millis()?

And in the code that i have posted above, after delay when I didn't include Stop() it kept on rotating. But its inside Setup(), so after delay having no code to execute the robot should have stopped itself. Why it didn't do so. Can anyone explain?

ramyar:
And in the code that i have posted above, after delay when I didn't include Stop() it kept on rotating. But its inside Setup(), so after delay having no code to execute the robot should have stopped itself. Why it didn't do so. Can anyone explain?

Well yeah the code will have stopped, but the pins will still be in the states that they were in from Drive_Right90 when the code stopped. Hence the need for Stop() to put them into the state that stops the motors.

ramyar:
Is it possible to reset millis()?

Not without resetting the board, but that's not actually what you want to do. You reset a timer instead, by assigning the current value of millis() to another variable say startTime and then later on you compare that startTime to the new, updated value of millis() to see how much time went by.

OK :slight_smile: . With millis I don't want to known the time the robot runs instead, for example the robot goes straight and when met with obstacle it turns, during only which the time has to start and count till 8s and then reset to zero because for the next obstacle it has to start from 0 again. That's why I am asking whether I can reset it?

You don't need to reset it. Just capture the time and set a flag to note that timer has started:
(if obtacle is met){ // however you determine that

newStartTime = millis();
timerRunning = 1;
}

then see how much time has elapsed.
(if (timerRunning ==1) && ((millis() - newStartTime) >= 8000UL){
// turn off timer
timerRunning = 0;
// do whatever happens after 8 seconds
}

CrossRoads:
You don't need to reset it. Just capture the time and set a flag to note that timer has started:
(if obtacle is met){ // however you determine that

newStartTime = millis();
timerRunning = 1;
}

then see how much time has elapsed.
(if (timerRunning ==1) && ((millis() - newStartTime) >= 8000UL){
// turn off timer
timerRunning = 0;
// do whatever happens after 8 seconds
}

Wat happened with your code tags. After 30000 plus posts you should know better :smiley:

sterretje:
Wat happened with your code tags. After 30000 plus posts you should know better :smiley:

..... to mention being a mod

:confused: