I have an application but very limited software abilities. The electronic component side is easy for me.
When a momentary push button is pressed, I need a single output to activate a relay for a period of 10 minutes. At the end of 10 minutes, the relay turns the load off.
If before the 10 minutes expires and the same button is pressed again, the relay turns off.
Pressing the button again starts the 10 minute countdown again.
Any guidance or direction appreciated. TIA
Countdown Timer Relay Sketch Help
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88154
- Joined: Sat Feb 07, 2009 10:11 am
Re: Countdown Timer Relay Sketch Help
Lady Ada's Arduino Tutorials are a great place to start. http://www.adafruit.com/tutorials
Lesson 5 goes in depth on button programming and software debouncing. Other lessons cover the basics of timing. Most of the examples are for controlling LEDs, but from the software side, that is the same as turning on a relay.
Lesson 5 goes in depth on button programming and software debouncing. Other lessons cover the basics of timing. Most of the examples are for controlling LEDs, but from the software side, that is the same as turning on a relay.
-
- Posts: 5
- Joined: Wed Jan 18, 2012 10:05 pm
Re: Countdown Timer Relay Sketch Help
@Prarietech. Did you manage to get your code to function well yet. I am currently working on something similar and would like to see if perhaps what you came up with so far might be of help to me. I would be happy to share my project with you as well. Right Now I am just getting started on it. I have a rough draft right now but it has potential. I am building a circuit that will Power a radiator cooling fan when the cars temp reaches the set coded temp. If the car is turned off and the fan is on, it will remain on for ten mIn and then turn off the fan and power down the arduino till the next time the car is started. This is a practical device but also is a great project to learn about voltages, timing, temp readings, and digital/analog inputs and outputs.
- prairietech
- Posts: 51
- Joined: Tue Apr 10, 2012 11:27 pm
Re: Countdown Timer Relay Sketch Help
I have not made much progress but am glad you replied. I have been trying to find some similar sketches but looks like several will need to be used together to achieve my goal.Hrd2RchMe wrote:@Prarietech. Did you manage to get your code to function well yet. I am currently working on something similar and would like to see if perhaps what you came up with so
The electronic side of this is easy for me but I have never been exposed to the code. It is amusing to see stick drawings of schematics because some code writers don't read schematics.
I'll have to keep trying to interrupt what I need to do.
-
- Posts: 5
- Joined: Wed Jan 18, 2012 10:05 pm
Re: Countdown Timer Relay Sketch Help
Here this sketch I made should help you on your way. I am using most of this in my project and I think it should speed you on your way. I wrote it in IDE Version 0022 to be more compatible as I was unsure what version your were working with. It should work well in most versions. Best of luck to you! Let me know if it helps.
/*
Button start timer controlled output
Turns on an OUTPUT (13 in this case) on for Set Time after pressing a button
or setting Digital i/o (pin 2 in this case) to high, then turns off when time
expires.
I modified the “button.ino” and "Blink.ino" examples to produce the below code.
The extra commented line were just to help me recall where I had made changes.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
Created: 4-14-2012
By: Hrd2RchMe [|:^)->~<
Modified 4-15-2012
By Cory Black - [email protected] Subject = Arduino Sketch
*/
//**************************************************
const int buttonPin = 2; // the number of the pushbutton pin
//**************************************************
//**************************************************
int buttonState = 0; // variable for reading the pushbutton status
//**************************************************
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//**************************************************
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//**************************************************
}
void loop() {
//*************************************************
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
delay(250); // debounce
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
//**************************************************
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second, adjust to your needs in milliseconds
//****************************************************
}
//****************************************************
//****************************************************
else {
//****************************************************
digitalWrite(13, LOW); // set the LED off
}
//****************************************************
delay(250); //Give the controller a quick break
}
//****************************************************
/*
Button start timer controlled output
Turns on an OUTPUT (13 in this case) on for Set Time after pressing a button
or setting Digital i/o (pin 2 in this case) to high, then turns off when time
expires.
I modified the “button.ino” and "Blink.ino" examples to produce the below code.
The extra commented line were just to help me recall where I had made changes.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
Created: 4-14-2012
By: Hrd2RchMe [|:^)->~<
Modified 4-15-2012
By Cory Black - [email protected] Subject = Arduino Sketch
*/
//**************************************************
const int buttonPin = 2; // the number of the pushbutton pin
//**************************************************
//**************************************************
int buttonState = 0; // variable for reading the pushbutton status
//**************************************************
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
//**************************************************
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//**************************************************
}
void loop() {
//*************************************************
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
delay(250); // debounce
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
//**************************************************
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second, adjust to your needs in milliseconds
//****************************************************
}
//****************************************************
//****************************************************
else {
//****************************************************
digitalWrite(13, LOW); // set the LED off
}
//****************************************************
delay(250); //Give the controller a quick break
}
//****************************************************
- prairietech
- Posts: 51
- Joined: Tue Apr 10, 2012 11:27 pm
Re: Countdown Timer Relay Sketch Help
Thanks!! I am anxious to give this a try!Hrd2RchMe wrote:Here this sketch I made should help you on your way. I am using most of this in my project and I think it should speed you on your way. I wrote it in IDE Version 0022 to be more compatible as I was unsure what version your were working with. It should work well in most versions. Best of luck to you! Let me know if it helps.
It is hard for me to create my own code since I don't have much of a clue how to (yet). But it is much easier to reverse engineer what someone else has done to fit my needs.
While searching for sketches, I noticed some criticism of the delay statements. Some examples were written where the entire board was shut down while waiting for the delay to expire. When really all that was needed was to delay a single output pin. I want my board to still be available while delaying one output pin.
I have a proto board with mini breadboard due to arrive from Adafruit any day. With it, I can get more LEDs tied to outputs and switches to inputs to test my ideas.
My first application for the Uno is as a light delay. Momentary push button causes an output to latch for say 10 minutes. I would like to have other inputs available to monitor door switches and possibly control other outputs for lighting. I also bought the Ethernet shield to add to the Uno for remote web controlonce I get my head above the clouds!
Trying to teach an old dog new tricks is a slow process!
-
- Posts: 98
- Joined: Sun Aug 21, 2011 11:01 pm
Re: Countdown Timer Relay Sketch Help
I don't think that the code posted by Hrd2RchMe is good for what prairietech asked for.
One of prairietech's requirements is:
If before the 10 minutes expires and the same button is pressed again, the relay turns off.
I would use two variables.
One variable would keep track of whether the relay is supposed to be on or off.
The other variable would keep track of what time the relay was turned on.
Here is my (untested) code:
(edited to fix some bugs I found upon inspection)
One of prairietech's requirements is:
If before the 10 minutes expires and the same button is pressed again, the relay turns off.
I would use two variables.
One variable would keep track of whether the relay is supposed to be on or off.
The other variable would keep track of what time the relay was turned on.
Here is my (untested) code:
(edited to fix some bugs I found upon inspection)
Code: Select all
int outputState = LOW; // variable for what we want to output
unsigned long timeOn = 0; // what time did we turn the output on?
const unsigned long timerSetting = 600000; // this is in milliseconds
// note: 1000 milliseconds = 1 second
// therefore, 60000 milliseconds = 1 minute
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(outputPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// remember previous button state
oldButtonState = buttonState;
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
delay(40); // debounce
// check if the pushbutton has just been pressed
// if it has, then the oldButtonState will be LOW
// (because a moment ago, the button was not pressed),
// and the buttonState now will be HIGH:
if ((oldButtonState == LOW) && (buttonState == HIGH)) {
if (outputState == LOW) { // If the LED is now off, ...
digitalWrite(outputPin, HIGH); // we turn the LED on, ...
outputState = HIGH; // remember that we turned it on, ...
timeOn = millis(); // and remember what time we turned it on, ...
}
else { // otherwise, ...
digitalWrite(outputPin, LOW); // we turn the LED off, ...
outputState = LOW; // and remember that we turned it off.
}
}
else { // If no button press:
// If the LED is on and has been on for such-and-such a length of time,
if ((outputState == HIGH) && ((millis() - timeOn) > timerSetting)) {
digitalWrite(outputPin, LOW); // we turn the LED off, ...
outputState = LOW; // and remember that we turned it off.
}
}
}
Last edited by odometer on Mon Apr 16, 2012 6:06 am, edited 1 time in total.
- philba
- Posts: 387
- Joined: Mon Dec 19, 2011 6:59 pm
Re: Countdown Timer Relay Sketch Help
The issue with delay is not that the board shuts down but rather that it stays in the delay routine while waiting for the time to expire. The alternate way of doing this is to use millis and look for the time yourself in your loop.
Instead of this:
do this:
Of course, what you do at the start and end of the time period is up to you. As is how you decide to start the delay.
Instead of this:
Code: Select all
delay(100);
// do what ever you want
Code: Select all
long delaytime;
...
void loop(){
if(start_a_delay) {
// do something you want to time
delaytime = millis()+100;
}
if(delaytime < millis()) {
// do what ever you want after 100 mS
}
// do lots of other stuff
}
-
- Posts: 5
- Joined: Wed Jan 18, 2012 10:05 pm
Re: Countdown Timer Relay Sketch Help
Wow that's great folks thanks for the input. I myself am kinda new as well, and just wanted to help get prairie started on what to look for. I myself will implement some of your ideas as well. Sharing and learnIng, I love it. Thanks again folks!
-
- Posts: 98
- Joined: Sun Aug 21, 2011 11:01 pm
Re: Countdown Timer Relay Sketch Help
My method has the advantage that one need not special-case millisecond rollover.
http://arduino.cc/en/Reference/millis
In general, it does not make sense to directly compare "millis" times. To use an analogy: suppose you are shopping for bread, and today's date is December 30. You see a loaf of bread marked "SELL BY JAN 3". Will you say, "Yuck, this bread is 11 months and 27 days past its sell-by date"? Or will you use common sense and deduce that the sell-by date has yet to arrive?
The Arduino has no common sense. As a programmer, your job is to provide it with enough "common sense" to avoid these slip-ups.
http://arduino.cc/en/Reference/millis
In general, it does not make sense to directly compare "millis" times. To use an analogy: suppose you are shopping for bread, and today's date is December 30. You see a loaf of bread marked "SELL BY JAN 3". Will you say, "Yuck, this bread is 11 months and 27 days past its sell-by date"? Or will you use common sense and deduce that the sell-by date has yet to arrive?
The Arduino has no common sense. As a programmer, your job is to provide it with enough "common sense" to avoid these slip-ups.
-
- Posts: 5
- Joined: Wed Jan 18, 2012 10:05 pm
Re: Countdown Timer Relay Sketch Help
Wow I sure have learned a lot in the past few days. Thanks so much everyone!
Here is the current state of my first attempt at my own project and sketch with a temp sensor. Any input here is welcome. I am sure I will have a few spots where improvements can be made. I will try and put up a schematic of my circuit as well. I know it can be improved on! It's the first time I have used Fritzing, I found it to be pretty easy to work with so far. I just need more practice with how I lay things out.
Here is the current state of my first attempt at my own project and sketch with a temp sensor. Any input here is welcome. I am sure I will have a few spots where improvements can be made. I will try and put up a schematic of my circuit as well. I know it can be improved on! It's the first time I have used Fritzing, I found it to be pretty easy to work with so far. I just need more practice with how I lay things out.
Code: Select all
/*
*****************************************************************************************************
********************************* Automatic Fan Control For Car *******************************
*****************************************************************************************************
12 volt external power source
Temp Sensor to activate relay to power cooling Fan
Temp var is set in code
If 12 volt switched current is present and temp >= threshold turn on fan
If 12 volt switched current is not present and temp >= threshold keep fan on for 10 min
If after 10 min temp >= threshold run for 10 more min
If after 10 min temp < threshold turn off fan
If 12 volt switched current is not present and temp < threshold turn off fan if on
One 12 volt relay powers a 9 volt regulator to power the arduino
One relay powers fan from 12 volt fused source
Finally relay output pin feeds the main 12 volt switched source relay with power from the fan realy
for when the 12 volt switched is not present and fan still needs to run
//This would allow ignition to be off and fan still run till temp goes below threshold or 20 min max
//When temp goes below threshold fan and arduino power off till next time 12 volt switched power returns
*********** ~ Credits ~ ************
Temp Sensor Code:
An open-source LM35DZ Temperature Sensor for Arduino. This project will be enhanced on a regular basis
(cc) by Daniel Spillere Andrade , http://www.danielandrade.net
http://creativecommons.org/license/cc-gpl
*/
int outputState = LOW; // variable for what we want to output
unsigned long timeOn = 0; // what time did we turn the output on?
const unsigned long timerSetting = 600000; // this is in milliseconds
const unsigned long maxTimerSetting = 12000000; //maximum time to run in milliseconds
// note: 1000 milliseconds = 1 second
// therefore, 600000 milliseconds = 10 minutes
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to or 5 volt ref in my case
const int ledPin = 13; // the pin that the LED is attached to or relay in my case
// Variables will change:
int buttonState = 0; // current state of the button or 5 volt ref in my case
int lastButtonState = 0; // previous state of the button or 5 volt ref in my case
//*************************************
// Temp Reading
//*************************************
int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
//*************************************
//*************************************
void setup() {
//Serial.begin(9600); // uncomment to start serial communication
// initialize the button pin (or 5 volt ref in my case) as a input:
// Pin 13 has an LED connected on most Arduino boards:
pinMode(buttonPin, INPUT);
// initialize the LED (or relay in my case) as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
//Serial.begin(9600); //uncomment to se serail data
}
void loop() {
//************************************
// Temp Reading
//************************************
for(i = 0;i<=7;i++){ // gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(1000);
}
tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature
Serial.print(tempc,DEC); // uncomment to start serial communication
Serial.print(" Celsius, "); // uncomment to start serial communication
Serial.print(tempf,DEC); // uncomment to start serial communication
Serial.print(" fahrenheit -> "); // uncomment to start serial communication
Serial.print(maxi,DEC); // uncomment to start serial communication
Serial.print(" Max, "); // uncomment to start serial communication
Serial.print(mini,DEC); // uncomment to start serial communication
Serial.println(" Min"); // uncomment to start serial communication
tempc = 0;
//************************************
//************************************
// read the state of the pushbutton (or 5 volt ref in my case) Input Pin value:
buttonState = digitalRead(buttonPin);
delay(40); // debounce
// check if the pushbutton has just been pressed
// if it has, then the oldButtonState will be LOW
// (because a moment ago, the button was not pressed),
// and the buttonState now will be HIGH:
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on
if (outputState == LOW) { // If the LED is now off, ...
//Enter code to check current temp threshhold below
if (tempf >= 160)
digitalWrite(outputState, HIGH); // we turn the LED on, ...
outputState == HIGH; // remember that we turned it on, ...
timeOn = millis(); // and remember what time we turned it on, ...
}
}
else { // otherwise, ...
if (tempf << 160)
digitalWrite(outputState, LOW); // we turn the LED off, ...
outputState == LOW; // and remember that we turned it off.
}
}
else { // If no button press:
// If the LED is on and has been on for such-and-such a length of time,
if ((outputState == HIGH) && ((millis() - timeOn) > timerSetting)) {
if (tempf >= 160)
digitalWrite(outputState, HIGH);
if ((outputState == HIGH) && ((millis() - timeOn) == maxTimerSetting)) {
digitalWrite(outputState, LOW); // we turn the LED off, ...
outputState == LOW; // and remember that we turned it off.
}
}
}
}
- Attachments
-
- AutoTemp_sensor_car.fzz.zip
- The schematic so far. BreadBoard and PCB are not complete.
- (15.19 KiB) Downloaded 80 times
-
- Posts: 98
- Joined: Sun Aug 21, 2011 11:01 pm
Re: Countdown Timer Relay Sketch Help
I just took a quick look at your code. I have a few comments to make.
I will put them in with the code. I will mark them with the keyword PITSTOP so you can just search for that word to find them.
I will put them in with the code. I will mark them with the keyword PITSTOP so you can just search for that word to find them.
Code: Select all
/*
*****************************************************************************************************
********************************* Automatic Fan Control For Car *******************************
*****************************************************************************************************
12 volt external power source
Temp Sensor to activate relay to power cooling Fan
Temp var is set in code
If 12 volt switched current is present and temp >= threshold turn on fan
If 12 volt switched current is not present and temp >= threshold keep fan on for 10 min
If after 10 min temp >= threshold run for 10 more min
If after 10 min temp < threshold turn off fan
If 12 volt switched current is not present and temp < threshold turn off fan if on
One 12 volt relay powers a 9 volt regulator to power the arduino
One relay powers fan from 12 volt fused source
Finally relay output pin feeds the main 12 volt switched source relay with power from the fan realy
for when the 12 volt switched is not present and fan still needs to run
//This would allow ignition to be off and fan still run till temp goes below threshold or 20 min max
//When temp goes below threshold fan and arduino power off till next time 12 volt switched power returns
*********** ~ Credits ~ ************
Temp Sensor Code:
An open-source LM35DZ Temperature Sensor for Arduino. This project will be enhanced on a regular basis
(cc) by Daniel Spillere Andrade , http://www.danielandrade.net
http://creativecommons.org/license/cc-gpl
*/
int outputState = LOW; // variable for what we want to output
unsigned long timeOn = 0; // what time did we turn the output on?
const unsigned long timerSetting = 600000; // this is in milliseconds
const unsigned long maxTimerSetting = 12000000; //maximum time to run in milliseconds
// PITSTOP: Twelve million milliseconds? That's more than three hours. Is that what you want?
// note: 1000 milliseconds = 1 second
// therefore, 600000 milliseconds = 10 minutes
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to or 5 volt ref in my case
const int ledPin = 13; // the pin that the LED is attached to or relay in my case
// Variables will change:
int buttonState = 0; // current state of the button or 5 volt ref in my case
int lastButtonState = 0; // previous state of the button or 5 volt ref in my case
//*************************************
// Temp Reading
//*************************************
int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
// PITSTOP: maybe you should use floating-point for temperature
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
//*************************************
//*************************************
void setup() {
//Serial.begin(9600); // uncomment to start serial communication
// initialize the button pin (or 5 volt ref in my case) as a input:
// Pin 13 has an LED connected on most Arduino boards:
pinMode(buttonPin, INPUT);
// initialize the LED (or relay in my case) as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
//Serial.begin(9600); //uncomment to se serail data
}
void loop() {
//************************************
// Temp Reading
//************************************
for(i = 0;i<=7;i++){ // gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
// PITSTOP: where did you get the above formula? Are you sure it is correct?
tempc = tempc + samples[i];
// PITSTOP: why are you storing these in an array?
delay(1000);
// PITSTOP: during "delay" statements, your Arduino is pretty much deaf, dumb, and blind,
// so you might want to rethink having such a long delay
}
tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature
Serial.print(tempc,DEC); // uncomment to start serial communication
Serial.print(" Celsius, "); // uncomment to start serial communication
Serial.print(tempf,DEC); // uncomment to start serial communication
Serial.print(" fahrenheit -> "); // uncomment to start serial communication
Serial.print(maxi,DEC); // uncomment to start serial communication
Serial.print(" Max, "); // uncomment to start serial communication
Serial.print(mini,DEC); // uncomment to start serial communication
Serial.println(" Min"); // uncomment to start serial communication
tempc = 0;
//************************************
//************************************
// read the state of the pushbutton (or 5 volt ref in my case) Input Pin value:
buttonState = digitalRead(buttonPin);
delay(40); // debounce
// check if the pushbutton has just been pressed
// if it has, then the oldButtonState will be LOW
// (because a moment ago, the button was not pressed),
// and the buttonState now will be HIGH:
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on
if (outputState == LOW) { // If the LED is now off, ...
//Enter code to check current temp threshhold below
if (tempf >= 160)
// PITSTOP: Check your braces here.
digitalWrite(outputState, HIGH); // we turn the LED on, ...
// PITSTOP: I think you have the wrong variable in the line above.
outputState = HIGH; // remember that we turned it on, ...
// PITSTOP: Oops, the line above should have the = symbol (meaning: make this equal that).
// Not to be confused with == (meaning: check if this equals that).
// My mistake. Sorry.
timeOn = millis(); // and remember what time we turned it on, ...
}
}
else { // otherwise, ...
if (tempf << 160)
// PITSTOP: this symbol << is the left-shift operator.
// Please read the reference on these symbols: < > == <= >= != =
// Yes, it is confusing. C++ was designed by geeks, for geeks, not for the rest of us.
// Also, you might want to check your braces.
digitalWrite(outputState, LOW); // we turn the LED off, ...
// PITSTOP: probably wrong variable in the above line
outputState = LOW; // and remember that we turned it off.
// PITSTOP: Oops, I misled you. I used == instead of =.
// I will go back and edit my earlier post, as well.
}
}
else { // If no button press:
// If the LED is on and has been on for such-and-such a length of time,
if ((outputState == HIGH) && ((millis() - timeOn) > timerSetting)) {
if (tempf >= 160)
digitalWrite(outputState, HIGH);
if ((outputState == HIGH) && ((millis() - timeOn) == maxTimerSetting)) {
// PITSTOP: Chances are, you won't hit maxTimerSetting exactly.
// Instead of using == (equals), try using >= (greater than or equal).
digitalWrite(outputState, LOW); // we turn the LED off, ...
outputState = LOW; // and remember that we turned it off.
// PITSTOP: fixing my own stupid mistake again
}
}
}
}
-
- Posts: 5
- Joined: Wed Jan 18, 2012 10:05 pm
Re: Countdown Timer Relay Sketch Help
Thank you for the comments. Helped a lot. I added my comments below yours and marked with ** I made most of the suggested changes and had a few questions on some of the others. Thank you so much for your input. I hope that Prairietech has been able to follow along here as well and learn from the code as I have. This is why I chose a project that is a practical device but also has many variables that I knew I would be able to learn from and be able to adapt into other projects quickly and efficiently. Thanks again for making me feel welcome and not afraid to asked questions.
Code: Select all
/*
*****************************************************************************************************
********************************* Automatic Fan Control For Car *******************************
*****************************************************************************************************
12 volt external power source
Temp Sensor to activate relay to power cooling Fan
Temp var is set in code
If 12 volt switched current is present and temp >= threshold turn on fan
If 12 volt switched current is not present and temp >= threshold keep fan on for 10 min
If after 10 min temp >= threshold run for 10 more min
If after 10 min temp < threshold turn off fan
If 12 volt switched current is not present and temp < threshold turn off fan if on
One 12 volt relay powers a 9 volt regulator to power the arduino
One relay powers fan from 12 volt fused source
Finally relay output pin feeds the main 12 volt switched source relay with power from the fan realy
for when the 12 volt switched is not present and fan still needs to run
//This would allow ignition to be off and fan still run till temp goes below threshold or 20 min max
//When temp goes below threshold fan and arduino power off till next time 12 volt switched power returns
*********** ~ Credits ~ ************
Temp Sensor Code:
An open-source LM35DZ Temperature Sensor for Arduino. This project will be enhanced on a regular basis
(cc) by Daniel Spillere Andrade , http://www.danielandrade.net
http://creativecommons.org/license/cc-gpl
Expert advice on the timing and variables
By: Odometer
At: http://forums.adafruit.com/viewtopic.php?f=25&t=27534#p141514
*/
int outputState = LOW; // variable for what we want to output
unsigned long timeOn = 0; // what time did we turn the output on?
const unsigned long timerSetting = 600000; // this is in milliseconds
const unsigned long maxTimerSetting = 1200000; //maximum time to run in milliseconds
// PITSTOP: Twelve million milliseconds? That's more than three hours. Is that what you want?
// ** Great catch, I fat fingered in a extra zero
// note: 1000 milliseconds = 1 second
// therefore, 600000 milliseconds = 10 minutes
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to or 5 volt ref in my case
const int ledPin = 13; // the pin that the LED is attached to or relay in my case
// Variables will change:
int buttonState = 0; // current state of the button or 5 volt ref in my case
int lastButtonState = 0; // previous state of the button or 5 volt ref in my case
//*************************************
// Temp Reading
//*************************************
int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
// PITSTOP: maybe you should use floating-point for temperature
//** Not familiar with floating-point yet but researching it now, Thanks
//** Just read http://arduino.cc/en/Reference/Fpconstants what did you have in mind here?
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
//*************************************
//*************************************
void setup() {
//Serial.begin(9600); // uncomment to start serial communication
// initialize the button pin (or 5 volt ref in my case) as a input:
// Pin 13 has an LED connected on most Arduino boards:
pinMode(buttonPin, INPUT);
// initialize the LED (or relay in my case) as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
//Serial.begin(9600); //uncomment to se serail data
}
void loop() {
//************************************
// Temp Reading
//************************************
for(i = 0;i<=7;i++){ // gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
// PITSTOP: where did you get the above formula? Are you sure it is correct?
//** not tested at all and was taken from
//http://www.danielandrade.net/2011/04/10/dotklok-lm35-temperature-sensor/#more-486
tempc = tempc + samples[i];
// PITSTOP: why are you storing these in an array?
//** Again not sure and sourced from the above link
delay(50);
// PITSTOP: during "delay" statements, your Arduino is pretty much deaf, dumb, and blind,
// so you might want to rethink having such a long delay
//** Not sure why it was so high or if it's needed at all, reduced from 1000 to 50
}
tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature
//Serial.print(tempc,DEC); // uncomment to start serial communication
//Serial.print(" Celsius, "); // uncomment to start serial communication
//Serial.print(tempf,DEC); // uncomment to start serial communication
//Serial.print(" fahrenheit -> "); // uncomment to start serial communication
//Serial.print(maxi,DEC); // uncomment to start serial communication
//Serial.print(" Max, "); // uncomment to start serial communication
//Serial.print(mini,DEC); // uncomment to start serial communication
//Serial.println(" Min"); // uncomment to start serial communication
tempc = 0;
//************************************
//************************************
// read the state of the pushbutton (or 5 volt ref in my case) Input Pin value:
buttonState = digitalRead(buttonPin);
delay(40); // debounce
// check if the pushbutton has just been pressed
// if it has, then the oldButtonState will be LOW
// (because a moment ago, the button was not pressed),
// and the buttonState now will be HIGH:
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed or not
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on or 5 volt went to 0 volt
if (outputState == LOW) { // If the LED is now off, ...
//Enter code to check current temp threshhold below
if (tempf >= 160) {
// PITSTOP: Check your braces here.
//** Fixed Thanks
digitalWrite(outputState, HIGH); // we turn the LED on, ...
// PITSTOP: I think you have the wrong variable in the line above.
//** HIGH should = on at temp of 160 with the above?
outputState = HIGH; // remember that we turned it on, ...
// PITSTOP: Oops, the line above should have the = symbol (meaning: make this equal that).
// Not to be confused with == (meaning: check if this equals that).
// My mistake. Sorry.
timeOn = millis(); // and remember what time we turned it on, ...
}
}
}
else { // otherwise, ...
if (tempf < 160) {
// PITSTOP: this symbol << is the left-shift operator.
// Please read the reference on these symbols: < > == <= >= != =
// Yes, it is confusing. C++ was designed by geeks, for geeks, not for the rest of us.
// Also, you might want to check your braces.
//** Fixed again here too, Thanks again.
digitalWrite(outputState, LOW); // we turn the LED off, ...
// PITSTOP: probably wrong variable in the above line
//** This would turn off the relay to the fan when temp falls below 160F ?
outputState = LOW; // and remember that we turned it off.
// PITSTOP: Oops, I misled you. I used == instead of =.
// I will go back and edit my earlier post, as well.
//** No problem like you said it was untested when you posted it ;-)
}
}
}
else { // If no button press:
// If the LED is on and has been on for such-and-such a length of time,
if ((outputState == HIGH) && ((millis() - timeOn) > timerSetting)) {
if (tempf >= 160)
digitalWrite(outputState, HIGH);
if ((outputState == HIGH) && ((millis() - timeOn) >= maxTimerSetting)) {
// PITSTOP: Chances are, you won't hit maxTimerSetting exactly.
// Instead of using == (equals), try using >= (greater than or equal).
//** Great idea, changed. Thanks
digitalWrite(outputState, LOW); // we turn the LED off, ...
outputState = LOW; // and remember that we turned it off.
// PITSTOP: fixing my own stupid mistake again
}
}
}
}
Please be positive and constructive with your questions and comments.