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
}