Hi all,
I have written some code but when i test it, it doen’t work as i would like it to work.
Here is the code:
int cutdownPin = 10; // Pin linked to Cutdown
int x = 0; // X is for Cutdown timer
void setup()
{
pinMode (cutdownPin, OUTPUT);
}
void loop()
{
while (x < 15)
{
delay(1000);
x++;
}
if (x >= 10 && x <= 15) // Time Zone for cutdown
{
digitalWrite (cutdownPin, HIGH);
}
}
I want the led to turn on at 10 secs but it turns on at 15?? Why is this?
Also when i implement an “else” statement after the last if for
else {
digitalWrite (cutdownPin, LOW)
}
it doesn’t turn the led off after 15 secs instead it stays off all the time?
Strange!
Help would be much appreciated,
Many Thanks
But, I also want to have another "loop" running at the same time sending gps data. For testing i set the time to 10secs and 15secs but in the real thing it will be 3 hours, so if i had a delay then that would delay sending data aswell?
Is there a way to have two loops running side by side?
Many thanks :)
If you have a "delay", yes, you stop other stuff running as well.
Think about how you yourself would perform a task like this.
Look at the clock ("millis()"), and note the time.
Has ten seconds gone by? ie. Is "millis" now > (the time I noted PLUS ten seconds' worth of milliseconds)?
No? Ok just carry on. Have another look at the clock, whatever.
Yes (tens seconds has elapsed) - do what you have to do after ten seconds.
so:
startTime = millis();
..
..
if (millis () > (startTime + (10* 1000))
{turn your LED on}
VERY IMPORTANT: Remember : as soon as "loop" has finished, it gets called again, so be careful about not over-writing your "startTime" AND "delay" blocks everything else from happening.