How to make lights flash on digital output for a set time then stay on

Hi all, kinda new to all this. I have code to make led lights flash (string lights) on and off. However, I'd like it to flash for a set time, then stay on until power is removed. What code would I need to add to my script? Thanks in advance.
Code is:

void setup(){

    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
}

void loop(){
    //show colour 1;
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);

    delay(500); //wait half a second

    //show colour 2:
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);

    delay(500); //wait half a second

    //any more pattern or changes here...
}

A simple approach would be to use a for loop in your setup() function to turn the lights on and off a number of times and then turn them on.

And do nothing in the loop() function.

Simple but kinda brain dead, as there would be no way to say "do that again" other than to reset or power cycle the board.

The Hello World "blink" sketch blinks forever, but the same turn on, delay, turn off, delay pattern can be placed in a for loop, so all you'd have to know about if you don't already is a way to use a for loop to do something N times.

Do you know the Iced Tea guy? He can tell you all about it. I prefer to read, just can't sit still for 30 minutes to get 4 minutes of content, but ppl like him:

Or google

   arduino for loop

HTH

a7

Thanks for the reply. I'll look at that approach, it is a bit of a brain dead system, but it's what the customer wants lol.

The customer is always right.

I saw some mock-ups of a keypad where the customer had left off zero. It was to be used for entering zip codes.

The programmer (not me) did point it out, but I truly wished the he hadn't…

Your customer may come back at you for improvements…

a7

Kinda like this?
EDIT: Forgot something:

void setup(){

    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
}

void loop(){
    static unsigned long timer;
    unsigned long setTime = 30000; // 30 seconds
    static bool timing = true;
    timer = millis();

    while(timing){
    timing = (millis() - timer <= setTime);
    //show colour 1;
    digitalWrite(11, HIGH);
    digitalWrite(10, LOW);

    delay(500); //wait half a second

    //show colour 2:
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);

    delay(500); //wait half a second

    }
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
} // press reset button to repeat

Or a better way:

void setup(){

    pinMode(11, OUTPUT);
    pinMode(10, OUTPUT);
}

void loop(){
    static unsigned long timer;
    unsigned long setTime = 10000, // 10 seconds
                  elapsed;
    static bool timing = true;
    timer = millis();
    
    while(timing){
      elapsed = millis() - timer;  
      timing = elapsed <= setTime;
      //show colour 1;
      if(elapsed  % 1000 < 500){
        digitalWrite(11, HIGH);
        digitalWrite(10, LOW);
      }  
      else{
        //show colour 2:
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
      }
    
    }
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    
} // press reset button to repeat

If that's the one in the video, he's a right pain in the butt with his videos, not worth a cracker...............

The digital output may not provide enough power for "string lights"
Please provide a link to the ones you are using.

I've tested and it is working, I am running them throught a seperate motor IC board to power them.

Thanks everyone for the advice. I appreciate it fully!

Tell us how you really feel!

No matter, but if the OP had watched that video, or googled as I suggested, she might have been able to code this herself and she might have learned something.

Instead she goes off with a fish.

a7

Perhaps you could have told us straight away?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.