How to set up a timer

Hi all. I'm working on a project with a servo and a light sensor. What I want is when the light sensor detects a value below the specified (400) for 30 seconds, the servo will move to position b, wait for 2 minutes, then move back to position a, then wait 5 minutes before starting the loop again.

This is what I've got so far and it works, I just don't know how to go about the first part. Right now when the value drops below 400 it instantly goes to position b. I want it to need 30 seconds of readings below 400 for it to do that.

#include <Servo.h>

int photocellPin = 0;
int photocellReading;
Servo servo;

void setup()
{
  Serial.begin(9600);
  servo.attach(9);
}

void loop()
{
  photocellReading = analogRead(photocellPin);
  Serial.print("Analog reading = ");
  Serial.println(photocellReading);
  servo.write(160);
  if(photocellReading <= 400){
    servo.write(20);
    delay(5000);
    servo.write(160);
  }
  delay(5000);
}

Basically what's going on is my phone line wiring in my house is crappy (200+ year old house) and every once in a while the internet connection drops. I know it's not the modem as Verizon has sent several different ones and they've all got this issue. When the internet LED on the modem is out for more than 30 seconds (internet connection dropped), I want the servo to move and turn the power switch off, wait 2 minutes, then turn it back on, then wait 5 minutes to be sure the modem is fully powered up before reading again. This just saves me from having to run down 2 flights of stairs to reset it myself, which sometimes can be a daily occurrence. I need it to wait 30 second because the internet light flashes when there's traffic, so instant would be a bad thing lol.

You need to read your sensor every second (let us say) for thirty seconds, and if any reading is above 400, then reset your thirty seconds timeout

How do I go about setting that up? :astonished: I did some searching in the Reference section, but couldn't find anything

Where in your code do you think you are checking for <300 for 40 seconds? There is not a trace of it anywhere.

OK, here are a few fragments

unsigned long thirtysec ;
  :
void loop() {
  :
  if(photocellReading >400 ) {
    thirtysec = millis() ; //reset clock - we've gone above 
  } else {
    if ( millis()-thirtysec > 30000 ) { if the clock has advanced 30 seconds....
     // bingo!!
    }
  }

Well it's not in the code because I've got no idea how to do it. I want it in with the "if(photocellReading <= 400);" part. I'm a noob to this kind of stuff with Arduino and have no idea how to set it up.

unsigned long thirtysec =0 ;
  :
void loop() {
  :
  if(photocellReading <=400 ) {
    if ( millis()-thirtysec > 30000 ) { if the clock has advanced 30 seconds....
      // bingo!!
     }
  } else {
    thirtysec = millis() ; //reset clock - we've gone above 
  }

got it

#include <Servo.h>

#define dropouttime  30000
#define offtime      120000
#define connecttime  180000

enum {
    waitingDrop='A',
    dropDetected='B',
    waitingConnection='C'
};

enum {
    turnOff=20,
    turnOn=160
};

int photocellPin = 0;
int photocellReading;
char state = waitingDrop;
char laststate;
unsigned char servoval = turnOff;
long looptimer, servotimer;
Servo servo;

void setup()
{
  Serial.begin(1200);
  servo.attach(9);
  servo.write(servoval);
  delay(2000);
  servoval = turnOn;
  servo.write(servoval);
  servotimer=millis();
  looptimer=servotimer;
}

void loop()
{
  if (((millis()-servotimer) > 50) || (state != laststate)) {
    servo.write(servoval);
    servotimer = millis();
  }
  laststate = state;
  if (state == waitingDrop) {
    photocellReading = analogRead(photocellPin);
    Serial.print("Analog reading = ");
    Serial.println(photocellReading);
    servoval = turnOn;
    if ((photocellReading <= 400) && ((millis()-looptimer) > dropouttime)) {
      state = dropDetected;
      Serial.println("Dropout detected");
      looptimer = millis();
    }
    else if ((photocellReading > 400) && (state == waitingDrop))  {
      looptimer = millis();
    }
  }
  else if (state == dropDetected) {
      servoval = turnOff;
      if ((millis()-looptimer) > offtime) {
        Serial.println("Turning Modem On and waiting for connection");
        state = waitingConnection;
        looptimer = millis();
      }
  }
  else if (state == waitingConnection) {
      servoval = turnOn;
      if ((millis()-looptimer) > connecttime) {
        state = waitingDrop;
        Serial.println("Waiting for Dropout");
        looptimer = millis();
      }
  }
  else {
    Serial.print("States: ");
    Serial.print(waitingDrop);
    Serial.print(dropDetected);
    Serial.println(waitingConnection);
    Serial.print("Invalid state: ");
    Serial.println(state);
    if ((millis()-looptimer) > dropouttime){
      state = waitingDrop;
      looptimer = millis();
    }
  }
}