I'm new to arduino and have got a small project turning my radiator fan on at 80*c in my car.
I need it to delay the digital outpit by two seconds when it reached the value it should go high at... just to stop relay chatter as im using a NTC thermistor.
This is the code I have but everytime I enter a delay(2000) it slows the serial.println down.... how can I code it to stop it doing that?
Thanks for any help in advanced.
// pin 2 is led output
int led = 2;
//variable to store the value coming from A0
int sensorValue = (A0);
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(led,OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
//1023/5 = 204.6
// 4v = 818.
if (sensorValue >818)
{
delay(2000);
digitalWrite(led,HIGH);
}
else if (sensorValue <800)
{
digitalWrite(led,LOW);
}
Hi, please read How to use the forum and change your post to use code-tags.
float voltage = sensorValue * (5.0 / 1023.0);
If you want accurate you want to change that to 1024
And then have a look at blink without delay I still think the second chapter (after Blink) in every Arduino should be called "And now never use delay() again!".
I understand 1024 - I thought that at first but a lot of code says to do it 1023!?
I don't understand the "blink without delay" at all.... I just got used to using the delay and had no idea that it actually interrupts the process... useless!!
I'll just make a comparator circuit this time, I've spent too long on this project as it is!
I don't understand the "blink without delay" at all
Save the millis() value at the time that the start action happens, in this case the start of your "delay" period. Then, each time through loop(), check whether the required period (200 milliseconds) has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions such as sending or receiving via Serial, but don't block the free running of loop().
1023 is just wrong. Some people use that because it's the highest value out of the ADC. But the ADC has 1024 steps because 0 is also a step.
And yeah, delay() does exactly what it says on the box, it delays. And a micro can just do one thing at a time even if that one thing is waiting.
Then you have some homework to do Don't quite now, it's one of the most important things to learn when you make a program where you need to time stuff (that's like almost always...). Lot of tutorials and forum topics about it. Look at millis() as a clock. Although it doesn't give you the actual time like we know it but just the time since it started. But still, even if a clock is set "wrong" you can still use it to time an interval. And that's what blink without delay does. It saves (in a variable) the last time it did something (it remembered the time) and every pass though loop() it looks at the clock (millis() ) again to see is the time now - the time remembered >= the duration we want.
To make it real life, let's say you put your egg into the water at 15:05 and you want it to boil for 6 minutes. You remember the 15:05 and you watch the clock often. Now it's 15:08, you do 15:08 - 15:05 = 3 minutes. Nope, not done. Keep watching and comparing. Now it's 15:10, you do 15:10 - 15:05 = 5 minutes. Almost there, keep checking! Now it's 15:11, you do 15:11 - 15:05 = 6 minutes, yessss, the egg is done!
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.
It is pretty much essential to master the technique.
Awesome, Thank you for your help I really appreciate it! - Steptillion, that example was ideal, I understand how it works I just need to practis implementing it.
Robin2 - thanks for the link, that'll help me out alot.
I definitely won't give up... I put myself under pressure because I needed the car to drive from east Sussex to Yorkshire the same day and knew if I didn't have it up and running, as soon as I got suck in traffic the fan wouldn't come on and cool the engine down properly and I'd have broken down or blown the head gasket!
I just left it with Delay and it worked ok... not great, but it got me here.