two functions at once

I went through the ladyada’s lesson 5. but want to add a couple functions to the last code, but it’s starting to confuse the heck out of me. basically all I need to do is run a continuous function along with the if functions. so for example I have a constant blinking led set on pin 12, a button is set on pin 2, and another led on pin 11, when the button is pressed pin 11’s led changes rate but pin 12 led stays the same. when looking through Arduino Programming Notebook it seams the “while” statement is what I need basically I’m just having trouble with placement, would it be placed after “void loop” right before “val = digitalRead(switchPin);” , or would I place it before if (lightMode == 0) { // all-off". or am I completely incorrect here? any help is appreciated, Thanks

I don’t know these lessons, but I think this code is close from what you need :

bool buttonPressed = false;
long lastLed1Blink = 0, lastLed2Blink = 0
int led1Delay = 100, led2Delay =200;

void loop() {
    while(!buttonPressed) {
        if (millis()>(lastLed1Blink+led1Delay) { /* Make led1 blink */ }
        if (millis()>(lastLed2Blink+led2Delay) { /* Make led2 blink */ }
        /* Add code here to check if button is pressed, including debouncing it */
        }
    /* Add code here to change led2 blink rate */
}

There are certainly cleaner ways to do this, including timer-based interrupts (If they’re available on the Arduino, dunno). You might also want to add something like :

if (millis() < lastLed1Blink) { lastLed1Blink = 0; lastLed2Blink = 0; }

To prevent the overflow, then reset, of the returned value of millis() every 9 hours or something.

… I hope this is answering your question. If not, I’d try to give you another way… I have time to spend, just watching scripts run :slight_smile:

Thanks for the quick reply, that code seems to be what I need, I just need to free up some time to experiment with it and maybe go through some more tutorials to understand programming a little better, I’ll let you know how it goes once I have time. also is “bool” a in the first line correct?

Yes, Arduino added the boolean keyword as a synonym of bool, probably to be closer from Processing language (unsure of that), but both work. You can write boolean instead if you prefer, it will be exactly the same.

my code lacks something important, OMG I’m stupid ^^ :

        if (millis()>(lastLed1Blink+led1Delay) { 
            /* Add code to make led1 blink */ 
            lastLed1Blink=millis()
        }

        if (millis()>(lastLed2Blink+led2Delay) { 
            /* Add code to make led2 blink */ 
            lastLed2Blink=millis()
        }