I need to pause my input sensor for 5 minutes.

My sketch is for a laser triggered spray device. The code works except for one thing. If someone is to stand at the beam, or go back and forth, the motor will keep spraying quickly running the fluid dry. what I need is a 5 minute pause or delay,so that there will be at least 5 minutes between sprays, even when the beam is tripped. The code below is what I have so far. I have a delay(ledTime) set at 1000. If I try to make it longer using something like 1000 * 60 * 5 everything basically comes to a halt. What am I doing wrong?

/*
 ** This sketch is for a
 ** laser light controlled valve system
 */

int ledPin = 2;                           // pin selected or the LED
int sensorPin = A0;                    // pin selected for the photocell
unsigned int sensorValue = 0;    // initial variable for the photocell

const long valveTime = 500;      // This is how long the valve will stay open
const long ledTime = 1000;       // hang time between sprays

void setup()
{
  pinMode(ledPin, OUTPUT);     // the pin the LED is in will used for output

  Serial.begin(9600);                  // start serial for output - for testing @ 9600 baud
}

/* in this loop, the LED and the valve work in opposite directions
 ** when the LED in on, the valve is closed, and visa versa
 */

void loop()
{
  sensorValue = analogRead(sensorPin);   // read the value from the photocell  
  if(sensorValue<400)

    digitalWrite(ledPin, HIGH);          // when beam is solid, turn the LED on, valve off

  else 

    digitalWrite(ledPin, LOW);      // or else when beam is broke
    delay(valveTime);                    // open the valve
    digitalWrite(ledPin, HIGH);     // when beam is solid, turn the LED on, valve off
    delay(ledTime);                      // wait time before next valve actuation


  /*
  ** below is a test of a more detailed serial monitor output
  ** will show light levels and if the valve and led should be open or closed.
  */

  Serial.print("Current Level = ");
  Serial.print(sensorValue);                // the raw analog reading

  if (sensorValue < 400)
  {
    Serial.println(" - LED on, Valve OFF");
  }
  else if (sensorValue >400)
  {
    Serial.println(" - LED off, Valve ON");
  } 

  delay(500);
}

What am I doing wrong?

const long ledTime = 1000; // hang time between sprays

try:-
long int ledTime = 300000UL; // hang time between sprays

Delay() stops all your code basicaly, you need to have a delay without the delay() function which, means that it delays the spraying action not the rest of the code
you would have a timer variable that only allows it to spray after 5 minutes have elapsed, and that timer is only reset by a spray

Something like
unsigned long spraydelay = 0; // var for timer

Then in loop

If(millis() - spraydelay > 300000 && lasertripped() == true){
spraydelay = millis();
spray();
}

Something like that should work and allow you to continue with other processs or debugging or watever,

Thanks greatly for the reply, I will play around with the millis() and see what I come up with. Once figured out I will post the results.

Thanks again

millis is definitely the way to go, particularly if your sketch isn't complete yet and you'll want to do other things that a 5 minute delay will interfere with. Just make sure any large constants have UL on the end of them i.e. 300000UL, not 300000 or the compiler will try to interpret them as sixteen bit signed integers, which, since they can't exceed 32767 will cause problems as you have seen.

However, before you go there, don't forget Grumpy Mike's suggestion - it'll get you right to the solution you're asking for. You may well need millis imediately afterwards of course, as you refine things for the mark two version.

awesome! Thanks for the UL tip, I would have missed that since I didn't what it was. Much to learn I have, I wil hack at it tonight. Every bit of new knowledge helps...

Ok, I've attempted at adding the new code to create the 5 minute wait period for the valve control. I wasn't entirely successful. There is a 5 minute pause between sprays, but that is with or without the use of the triggering mechanism. Thats not bad if I want my spray valve to automaticaly spray periodicaly, so i think I will keep that part of the code. But what I really need is to deactivate the photosensor (input) for 5 minutes, in case someone is standing in the beam, or walking back and forth. I would like the input to be turned off for awhile. I think we are close, what am I missing?

/*
 ** This sketch is for a
 ** laser light controled valve system
 */

// pin assignments
const byte ledPin = 2;                        // pin selected or the LED
int sensorPin = A0;                            // pin selected for the photocell

// variables and settings
const long valveActuate = 500;           // This is how long the valve will stay open
const unsigned long valveInterval = 300000UL;  // Interval between valve actuation
unsigned long valveTimer = 0;              // variable holding the timer   
int valveState = LOW;                          // Setting the current state of the valve
unsigned int sensorValue = 0;              // initial variable for the photocell

void setup()

{
  pinMode(ledPin, OUTPUT);   // the pin the LED is in will used for output
  valveTimer = millis();            // timer controlled by the millis function

  Serial.begin(9600);               // start serial for output - for testing @ 9600 baud
}

/*  
 ** in this loop, the LED and the valve work in opposite directions
 ** when the LED in on, the valve is closed, and visa versa
 */

void loop()
{
  sensorValue = analogRead(sensorPin);   // read the value from the photocell  
  if(sensorValue<400)

     digitalWrite(ledPin, HIGH);       // when beam is solid, turn the LED on, valve off

  else 

     digitalWrite(ledPin, LOW);        // or else when beam is broke
     delay(valveActuate);                 // open the valve
     digitalWrite(ledPin, HIGH);       // when beam is solid, turn the LED on, valve off

  /*
   ** This code actuates  the valve after a period of 
   ** inactivity. 
   */

  if( (millis () - valveTimer) >= valveInterval)  // Check the valve state
  {
    if( valveState == LOW)
      valveState = HIGH;
    else
      valveState = LOW;
      digitalWrite (ledPin, valveState);   // Write a new valve state
      valveTimer = millis();                      // Reset timer
  }

  /*
  ** below is a test of a more detailed serial monitor output
   ** will show light levels and if the valve and led should be open or closed.
   */

  Serial.print("Current Level = ");
  Serial.print(sensorValue);                   // the raw analog reading

  if (sensorValue < 400)
  {
    Serial.println(" - LED on, Valve OFF");
  }
  else if (sensorValue >400)
  {
    Serial.println(" - LED off, Valve ON");
  } 

  delay(500);
}

But what I really need is to deactivate the photosensor (input) for 5 minutes, in case someone is standing in the beam, or walking back and forth.

This code:

  else 

     digitalWrite(ledPin, LOW);        // or else when beam is broke
     delay(valveActuate);                 // open the valve
     digitalWrite(ledPin, HIGH);       // when beam is solid, turn the LED on, valve off

is most likely where your problem is. Fancy indentation notwithstanding, the three lines after the else are independent. The only thing that happens only if the if test was not true is to turn the pin off. The other two statements are executed on every pass through loop. I suspect that you intended to have them executed as a block. If that is the case, you need curly braces around them.

right on, I see what you are saying. I will take a look into that and see what becomes of it.

Thanks!

I fixed the code, but it still doesn't have any effect on the input sensor. I need to keep hacking at it. I'm sure its probably something simple...

I'm confused about what the " if( (millis () - valveTimer) >= valveInterval) // Check the valve state" stuff is doing. The comment is incorrect, where it is placed. Nothing in that code is reading a valve state.

You've already waited 5 minutes after turning the valve on, and turned it off. Why are you messing with it again?

You want to use either millis() or delay(), not both.