Pages: [1]   Go Down
Author Topic: timer function  (Read 442 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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... smiley-sad Maybe it's wrong...
    

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

Thank you!!!
« Last Edit: October 02, 2007, 04:41:34 pm by Zooey » Logged

Brisbane, Australia
Offline Offline
God Member
*****
Karma: 1
Posts: 593
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want seconds then use millis();
Logged

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

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

Thxxxx!!!!
Logged

Brisbane, Australia
Offline Offline
God Member
*****
Karma: 1
Posts: 593
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
}
Logged

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

Thank you!!!
We succeeded in writing it correctly and now it works perfectly!!!

We used a structure like that:


Code:
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!!
« Last Edit: October 04, 2007, 06:30:15 am by Zooey » Logged

Pages: [1]   Go Up
Jump to: