I am new to programming my arduino and am looking for a little code help. I have my program currently setup to turn on a relay if my IR distance sensor is reading lower then 200 and then turn off if the reading is higher then 600. I need to add code that will turn off my relay pin if the pin has been high for longer then 5 min.
I am building a auger system that will move material from one bin to another. I want to shut everything off if the first bin runs out of material so it doesn’t just sit there and run for no reason. If I turn the system on an off again The program should reset. I could also put a momentary switch on my bin lid so that if you lift the lid it resets the program. Thank you for any suggestions you can give me.
My first suggestion is that you post your code so we can see what you’ve done and what’s the easiest way to modify that.
Speaking in general terms you can “memorize” the time that something happened by calling the millis() function and storing away the value returned in a variable. Then you can periodically call millis() again and subtract the 2 to see how much time has passed since that event happened. Branch to whatever action is needed based on that time difference. Be aware of the data type and max value possible when doing this. Evaluate your systems potential for any “rollover” (aka overflow) problem.
Thank you for your help. The code I am currently running is as follows
int sensorPin = 1; //analog pin 1
int Pin12 = 12;
void setup(){
Serial.begin(9600);
pinMode(Pin12, OUTPUT); //set pin11 to output
pinMode (sensorPin, INPUT); //set sensorPin to input
}
void loop()
{
int val = analogRead(sensorPin);
Serial.println(val);
if (analogRead(sensorPin)< 100)
{
digitalWrite(Pin12, HIGH);
}
if (analogRead(sensorPin)> 244)
{
digitalWrite(Pin12, LOW);
}
delay(1000);
}
I would like to turn pin12 off if it has been on longer then 5 min and stay off until pin14 reads high. I would also like to read pin15 high if sensorPin >244 for longer then 6 min.
OK so let me suggest some things to you and see what you can come up with. First let me rename Pin12 to be relayPin as that better describes it’s function. It just happens that pin 12 is used to control the relay … because you picked up a pair of dice this AM and rolled boxcars. :mrgreen: Also create a new variable called relayPinState which can be either HIGH or LOW.
So you need a way to tell when the relayPin gets turned on. It might be something like the following code. See if you can follow the thinking and then use it to do the other functions you want, setting pin15 and using pin14.
void loop()
{
int val = analogRead(sensorPin);
Serial.println(val);
if (analogRead(sensorPin)< 100)
{
relayPinState = LOW;
}
if (analogRead(sensorPin)> 244)
{
if(relayPinState == LOW)
{
relayPinState = HIGH;
relayPinTime = millis(); //this only happens on the transition from low to high states
}
}
if(millis() - relayPinTime >= TimeOut) //if relay has been on too long, turn it off
{
relayPinState = LOW;
}
digitalWrite(relayPin, relayPinState); //output the state desired
delay(1000);
}
TimeOut is a new constant (unsigned long) and set to the equivalent of 5 mins of msecs (5mins * 60secs/min * 1000msecs/sec).
I finally have a program that works. Thank you guys for your help. After a lot of trial and error I stumbled across the correct code and now I believe I understand why it works. Does the Arduino read the whole loop and then apply the output signals or does it do it each statement at a time?
int sensorPin = 1;
int relayPin = 12;
int ledPin = 7;
int skip;
int relayPinAlarm;
unsigned long relayPinTime;
void setup(){
Serial.begin(9600);
pinMode(relayPin,OUTPUT);
pinMode(ledPin,OUTPUT);
}
void loop(){
Serial.println(millis()-relayPinTime);
//-> if sensor is less then 200 and currently off turn on relay
if(analogRead(sensorPin)<200&&(relayPinAlarm==0)){
digitalWrite(relayPin,HIGH);
}
//-> if sensorpin is greater then 300 turn off relay and reset relayPinTime to millis time
if(analogRead(sensorPin)>300){
digitalWrite(relayPin,LOW);
relayPinTime = millis();
}
//-> if relay has been on for 210 seconds or longer turn relay off and call relayPinState HIGH then goto skip:
if((millis()-relayPinTime)>=210000){
digitalWrite(relayPin,LOW);
digitalWrite(ledPin,HIGH);
relayPinAlarm = 1;
goto skip;
}
//-> Turn ledPin off
digitalWrite(ledPin,LOW);
relayPinAlarm = 0;
skip:
delay(1000);
}