Simple timer to end project

HI,

I hope I am posting on the right section and in the correct way. I am bit of a beginner in the wonderful realm of Arduino and have put together a few model railway signalling related projects that have been very succesful.

I am now attemping to put together a small project that controls two pumps in two large containers that hold rain water for a paiir of flower beds. I have got this all working - it is very simpe based around two float switches and a 2 gang relay module.

It would be really useful to put a timer onto the project so that everything goes LOW(off) after say 10 minutes. This is really a bit of a failsafe to prevent pump burnout in case of float switch failure.

I have searched the web for timer examples and millis() seems to be the answer but the examples all seem very complicated I just can't get the code to work. Adding a button to start the cycle would be excellent as well although simply switching off the power supply works just as well.

I have added my code below and I would be really grateful if someone wld be kind enough to point me in the right direction.

Many thanks,

Mike Andrew


// Dual Water Pump Float Valve Unit

//v1.0 - One pump working
//v1.1 - Two pumps
//v1.2 - Reorganise Pinouts - working Version

#define FLOAT1_SENSOR 3
#define RELAY1 4
#define LED1_R 5
#define LED1_G 6

#define FLOAT2_SENSOR 7
#define RELAY2 8
#define LED2_R 9
#define LED2_G 10

void setup()
{

// initialize the pins:
pinMode(LED1_R, OUTPUT);
pinMode(LED1_G, OUTPUT);
pinMode(RELAY1,OUTPUT);
digitalWrite(RELAY1, LOW);
pinMode(FLOAT1_SENSOR, INPUT_PULLUP);

pinMode(LED2_R, OUTPUT);
pinMode(LED2_G, OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(FLOAT2_SENSOR, INPUT_PULLUP);

}
void loop()
{
//Float Sensor 1
if(digitalRead(FLOAT1_SENSOR) == LOW)
{
digitalWrite(RELAY1, LOW);
digitalWrite(LED1_R, LOW);
digitalWrite(LED1_G, HIGH);
}
else
{
digitalWrite(RELAY1, HIGH);
digitalWrite(LED1_R, HIGH);
digitalWrite(LED1_G, LOW);
}
//Float Sensor 2
if(digitalRead(FLOAT2_SENSOR) == LOW)
{
digitalWrite(RELAY2, LOW);
digitalWrite(LED2_R, LOW);
digitalWrite(LED2_G, HIGH);
}
else
{
digitalWrite(RELAY2, HIGH);
digitalWrite(LED2_R, HIGH);
digitalWrite(LED2_G, LOW);
}
}

void loop()
{
  static unsigned long startTime = millis();

  if(millis() - startTime  >= 600000UL)
  {
    // hang forever
    for(;;);
  }


  your code here

}

You will to reset the Arduino if you want to run your program again.

Thank you - that works and I am really grateful

Hi Again Sterretje ,

I just wondered what the - for(;;); - is actually doing, I would not have come up with that in a million years.

Mike

while(1){
//hang out until condition becomes untrue - which it won't
}

would do the same.

sterretje:
for(;;); // hang forever

But the Arduino will continue to respond to interrupts, won't it?

Yes.

So, that's not a good way to really end a sketch... Timer actions will also continue! Strange that there is no "quit" statement in this language, that would stop all actions, destroy all objects, etc.

NB.: Yes, I'm used to writing Clipper, VB and so on. :wink:

You can you use that while statement , but within the brackets check a digital input and break out of the loop if say the input is HIGH and run the loop again .

Erik_Baas:
So, that's not a good way to really end a sketch... Timer actions will also continue! Strange that there is no "quit" statement in this language, that would stop all actions, destroy all objects, etc.

NB.: Yes, I'm used to writing Clipper, VB and so on. :wink:

Then:

nointerrupts();
for(;;); // hang forever