the blink w/o delay guy is back :-)

I have begun reading books on C and C++. Currently I am feeling very dumb and so have begun "C for Dummies". I mention this from the outset so no one thinks I'm trying to get the work done for me. I can't stand that (jeez I don't understand, will you do it for me...NOT).

I can make this work:

int ledPin = 13; long blinkTimer = 0;

void setup() { pinMode(ledPin, OUTPUT); }

void loop() {

if (millis() - blinkTimer >=500 ) //play with this... digitalWrite(ledPin, HIGH);

if (millis() - blinkTimer >=1000 ) //and this to change - //on and off times {

digitalWrite(ledPin, LOW); blinkTimer = millis();




I can make this work:

int switchPin = 2; //tactile button int greenLED = 3; //greenLED int redLED = 4; //redLED

void setup() {

pinMode(switchPin, INPUT);

pinMode(greenLED , OUTPUT); pinMode(redLED , OUTPUT);


void loop() {

if (digitalRead(switchPin) == HIGH) //if the button is pressed... { digitalWrite(greenLED, LOW); //turn off the green LED digitalWrite(redLED, HIGH); //turn on the red LED delay(5000); //do this for five seconds } else { digitalWrite(greenLED, HIGH); //then return LEDs to previous digitalWrite(redLED, LOW); } }

What I can not do is make the two work together. In other words I would like to stop using delay to make the light stay lit for the required five seconds. Delay(5000) works splendidly but for reasons that have recently been explained is not the best way to get an action to continue for a prescribed period of time.

If anyone could explain how this can be accomplished I would be very pleased. Four rather large text books and I am still none the wiser.

Thank you in advance for your kind assistance.

Can you make the Code/# key work, please?

You may want to initialise the states of your output pins in "digitalWrite".

You need to break down the problem into state. One state is the key is not pressed and the lights are out. Another state is the key is pressed, and another is the lights are on, and have to remain on.

Always bear in mind that "loop" gets called again as soon as it returns, so anything you do will have to keep the state outside of "loop". Also remember that your switch may bounce.

Thank you for getting back to me so quickly.

What is the Code/# key? I would be very glad to make it work (or at least give it a good try anyhow)

It seems that my initial thinking: ‘i can program this thing w/o a background in programming’ was a poor assumption. Reading the forum and trying to digest the advice all of you are so kind to offer it is clear to me I need background info. I simply do not understand.

I will continue to read and hopefully will understand sooner than later. Thank you for your assistance, I will continue to beat my head against this until it gets through.

When you edit posts, there’s a bar above with icons for stuff (links, mail, images and font size/style/colour).
On the top row, just right of centre is a “#” symbol.
Please post code between the brackets it produces.

You have a PM.

Just sent you a PM.

Take a step back and consider what happens when you blink an LED. At a certain time, the LED goes on. Later, it goes off again. Later it goes on again, and we continue repeating this forever...

The key question is how we decide WHEN to turn the LED on an off.

In the basic example, we turn on the LED and sit staring at our watch until the appropriate time has gone by, turn the LED off, sit and stare at the watch some more, etc.

In the "without delay" example, we turn on the LED, check the time, and go do other things. After we've done some of these "other things", we come back and look at our watch, and say "has enough time passed since we lit the LED to turn it off again?"

Thanks westfw,

That watch analogy works very well.

Thank you for taking the time to help me out! :D