I've got a photoresistor , a led, and a relay wired up for a beam break with the photoresistor and led. after 5 sec of the beam broken (photoresistor < 400 in a broken beam and 700 in a no break state) the relay cuts power to a motor.
I'm stuck on how to do a check if the photoresistor value < 400 for 5 or more sec...then trigger a relay.
not sure how to incorporate a time check for a threshold value....
If someone can give an example of some code for this I would be most appreciated.
Well here's what I got so far but I'm missing something in the coding and the 5 sec check
if you can give me any feedback from my loop that would be awesome
//This will trip a relay to open from NC to NO after a case jam state breaks a beam threshold
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) casefeedertimer = (millis() + casefeedertimer); // a below threshold value begins a casefeeder timer // no beam break val ~ 700 with beam break val ~350
else casefeedertimer = 0;
if (casefeedertimer - millis() > 5000) digitalWrite(caserelay, HIGH); // if greater than 5 sec goes by then relay will trip
}
// Backlight on - ten seconds - then off again
int blpinStatus = 0;
blpinStatus = digitalRead(blpin);
static unsigned long blTime;
static boolean blOn;
if (blpinStatus == LOW) {
delay(20); // Debounce
blOn = true;
blTime = millis();
lcd.backlight(); //<============= ( Backlight on -
}
if (blOn == true && millis() - blTime > 10000) { //<============ ten seconds -
lcd.noBacklight(); // <=================================== then off again
blOn = false;
}
pay attention to this:
blOn = true;
blTime = millis();
and this:
if (blOn == true && millis() - blTime > 10000) {
lcd.noBacklight(); // <========================= THIS IS THE CODE THAT IS EXECUTED WHEN ELAPSED TIME = LIMIT
blOn = false;
Thanks for the input....I solved by reading the Photo-resistor and setting a val=mills as long as photoresistor was above a threshold value...if the photoresistor drops below the threshold value val=mills would not be updated and the if (mills()-val > 5000) would set relay to high....Got it working Much thanks...this is part of an automatic bullet reloading machine that I am adding sensors to it...then an LCD (maybe...the LCD coding is a bit intimidating for me)
Here's the code
int photoread; //read photo resistor
int photo=0; //photo resistor on Pin 0
int relayPin =5; //coil of micro relay to cut power
unsigned long valuechangetime; //timer
void setup(){
pinMode(relayPin, OUTPUT);
pinMode(photo, INPUT);
Serial.begin(9600);
}
void loop()
{
photoread = analogRead(photo); //read current state of sensor
if (photoread >400)
{
valuechangetime=millis();
}
if (millis() - valuechangetime > 5000) digitalWrite(relayPin, HIGH);
{Serial.println(analogRead(photo));
delay(200);
//Serial.println(digitalRead(photoread));
//delay(300);
}
}