Go Down

Topic: timer function (Read 592 times) previous topic - next topic

Zooey

Oct 02, 2007, 11:37 pm Last Edit: Oct 02, 2007, 11:41 pm by Zooey Reason: 1
Hi! we need your help again... we have three servomotors turning of 90° after having pressed the corresponding button. We need to make these servos move cyclically in a lapse of time. (i.e. for 1 to 30 seconds I can open ONLY the first servo, 31 to 60 seconds i can open only the second one and 61 to 90 secs only the third one.)

The code we wrote so far doesn't include the time function: all the buttons can work almost together (but we have to consider the time taken by servos to move).  
We have three cases:

1) botton A is pressed --> servo A moves and LED A turns on
2) button B is pressed --> servo B moves and LED B turns on
3) button C is pressed --> servo C moves and LED C turns on

we tried to put another condition, something like that:

void loop() {
  for (time = 0; time < 90000; time++) {
     if (time == 90000) time = 0;
     if (time >= 0 && time < 30000) lapse = 1;
     if (time >= 30000 && time < 60000) lapse = 2;
     if (time >= 60000 && time < 90000) lapse = 3;

    if (buttonA == LOW && lapse == 1) move servo A...

    if (button B == LOW && lapse == 2) move servo B...

    if (button C == LOW && lapse == 3) move servo C...

....
}

But it doesn't seem to work... :( Maybe it's wrong...
   

Does anyone of you know how we can write it with code?

Thank you!!!

Cheater

If you want seconds then use millis();

Zooey

Hello, Cheater! thank you for your reply!
We're newbies, so... how can we use this function, millis(); ? I mean, in the code...

Thxxxx!!!!

Cheater

millis() simply returns the number of milliseconds since the Arduino was turned on.

Here is a rough idea of what you want.

[code]
long offset = 0;
void setup()
{
offset = millis();
}

void loop()
{
 if (millis() - offset > 90000)
 {
   offset = millis();
 } elseif (millis() - offset > 60000 && button C == LOW) {
   move servo
 } elseif (millis() - offset > 30000 && button B == LOW) {
etc....
}

Zooey

#4
Oct 04, 2007, 01:23 pm Last Edit: Oct 04, 2007, 01:30 pm by Zooey Reason: 1
Thank you!!!
We succeeded in writing it correctly and now it works perfectly!!!

We used a structure like that:


Code: [Select]

void ServoA () {
...move servoA...
}

void ServoB() {
...move servoA...
}

void ServoC() {
...move servoC...
}

void loop() {

 if (millis() - offset >= 0 && millis() - offset < 30000) {
   ServoA();
   }
 if (millis() - offset >= 30000 && millis() - offset < 60000) {
   ServoB();
   }
 if (millis() - offset >= 60000 && millis() - offset < 90000) {
   ServoC();
   }
 if (millis() - offset == 90000) offset = millis();
   }



Bye!!

Go Up