photosenser to trigger a relay after 5 sec of continuous below threshold value..

Hey People!

I have a photo sensor, a LED, and a relay. I am wanting to trip the power relay when the light beam to the photosensor is blocked and drops the photo sensor value below 400 for a period >= 5sec.

Photosensor works great with output values of 700 full beam and 250 with a blocked beam...I don't think I've got the logic with the timers quite correct. As it is now the relay triggers after 5 sec regardless of the photo sensor value....

Any advice would be most welcome! I'm learning lots from this forum!

Thanks in advance

Eric

                                              //This will trip a relay to open from NC to after a case jam state breaks a beam threshold for > 5 sec

int casefeedsensor = 0;                       //analog pin 0 - case feeder photo resistor sensor-- has a led beam to charge sensor to ~ 700 (no break) to 350(beam break)

int caserelay = 5;                            //Pin 5 relay to cut power to case feeder in the event of a case feed jam state via light beam break

//int oldvaluecase = 0;                           //previous  state of case feed sensor

int newvaluecase = 0;                            //current state of case feed sensor

unsigned long casefeedertimer;                 //case feed timer < threshold
unsigned long oldvaluecase;                    //case feed timer > threshold



void setup(){

Serial.begin(9600);

  //oldvaluecase = 0;                             //initial state of case feed sensor 
  //casefeedertimer = 0;                                     

                                              //The relay pin as an output  - normally closed in wiring
  pinMode(caserelay, OUTPUT);

                                              //This is the default value, but we can set it anyways
  analogReference(DEFAULT);                   //5V Reference on UNO
  
}

void loop()
{
   Serial.println(analogRead(casefeedsensor)); 
  delay(500);

                                                        
    int newvaluecase = analogRead(casefeedsensor);       // read the photosensor
  
    if(newvaluecase < 400)                              // no beam break val ~ 700  with beam break val ~350
{
    casefeedertimer=millis();                          // a below threshold value begins a casefeeder timer
    
    
}
    else 
{    
    oldvaluecase=millis();     //above threshold timer updates
    
}    
  
    if (casefeedertimer - oldvaluecase  > 5000)      // if greater than 5 sec goes by then relay will trip
    digitalWrite(caserelay, HIGH);                   //if val is < 400 (a beam break state) the relay cuts power to the case feed motor 
                                                     
  
  
}

You have 2 different variables named newvaluecase. One is a global variable, the other is local to the loop() function and is declared each time through loop(). Which one is
    if(newvaluecase < 400) checking I wonder ? Logically it should be the one declared in loop(), but is it ?

Put some Serial.prints in your code to show the value of newvaluecase at various points and which sections of code are being executed.

I'm not sure if I have the logic of the timers correct in the code....anybody see what I don't see??

Much appreciated!

Spit

I changed the ">" to "<" in this line:

    if (casefeedertimer - oldvaluecase  < 8000)      // if greater than 5 sec goes by then relay will trip
{    digitalWrite(caserelay, HIGH);                   //if val is < 400 (a beam break state) the relay cuts power to the case feed motor 
}

and the relay will not trip as long as the beam is not broken....but if the beam is broken the relay trips after 1 sec (approx) no mater what the value is after the "<"

Not sure what is occuring??

Any suggestions??

Thanks,
Spit

It appears that each time you go through the loop you evaluate:

 if(newvaluecase < 400) 
{
    casefeedertimer=millis(); 
}

If the beam is broken you set casefeedertimer=millis(); .
Therefore casefeedtimer is always close to millis() as it is reset each time the loop executes.
You will never evaluate true for " if (casefeedertimer - oldvaluecase > 5000)" while the beam is broken as casefeedertimer and oldvalues will always remain almost identical.