timer function

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… :frowning: Maybe it’s wrong…

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

Thank you!!!

If you want seconds then use millis();

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

Thxxxx!!!!

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

Here is a rough idea of what you want.

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

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

We used a structure like that:

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!!