I am trying to blink an LED for 10 seconds then stop.
This code only blinks the LED.
Suggestion
int led = 8;
int timer;
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish
void setup() {
pinMode(led, OUTPUT); // initialize the digital pin as an output.
digitalWrite(led, LOW); // turn led on
void loop() {
// check if delay has timed out after 10sec == 10000mS
if (delayRunning && ((millis() - delayStart) >= 10000)) {
delayRunning = false; // // prevent this code being run more then once
digitalWrite(led, LOW); // turn led off
Serial.println("Turned LED Off");
}
timer++;
digitalWrite(8, led);
if (timer % 10000 == 0) {
led = !led;
}
Serial.println("Run Other Code");
}
DBRR_BrianK:
All I want is to have an LED go on/off for 10 seconds and stop.
I do not want to use a DELAY function
OK, now first thing is that you have been posting a lot and not getting the hint on how to post code.
You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output) from the IDE and click the icon. In fact, the IDE has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window.
And when posting code, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code", it can be quite garbled and is always more difficult to read.
Also tidy up your blank space. Use blank lines only between functional blocks.
So just for curiosity - why do you not want to use "delay()" - which could be used to do what you ask here, easily!
int led = 8;
//
unsigned long delayStart = 0; // the time the delay started
unsigned long timeBlink = 0; // blink timer
bool delayRunning; // true if still waiting for delay to finish
bool stateLED; //internal representation of LED 1=on, 0=off
void setup()
{
Serial.begin(9600);
//
pinMode(led, OUTPUT); // initialize the digital pin as an output.
digitalWrite(led, LOW); // turn led on
//
stateLED = false; //initial state equals led being off/LOW
delayRunning = true; // not finished yet
delayStart = millis(); //init delay timer
timeBlink = millis(); //init blinking timer
}//state
void loop()
{
if( delayRunning )
{
//has delay time run out?
if( (millis() - delayStart) >= 10000 )
{
//force LED off and clear delay flag
digitalWrite( led, LOW );
delayRunning = false;
}//if
else
{
//each on and off time lasts 250mS giving a period of 500mS
if( (millis() - timeBlink) >= 250 )
{
//get the time for the next blink time
timeBlink = millis();
//toggle the internal state flag and then drive that out to the LED
stateLED ^= 1;
digitalWrite(led, (stateLED)?HIGH:LOW);
}//if
}//else
}//if
//to prove things are running, print a live msg; indicate blinking or not
Serial.print("Led is " ); Serial.println( (delayRunning)?"blinking":"off" );
}//loop