Help with couple of loop's and leds

helo, its my first post so I hope it is in correct place. I am totally noob at programming. Im trying to solve one problem. Probably it is very easy for expirienced proggramers.

Basic functionality is flashing some leds in given combination. It works. Now I would like to make a several loops (for example 7). I would like to start every loop with diffrent button. For example:

Button no 1. -> loop no. 1 -> given combination
Button no 2. -> loop no. 2 -> given combination
Button no 3. -> loop no. 2 -> given combination

etc. I would like to use this same leds but in diffrent combinations.

So now I have to add code for buttons and name diffrent loops. Can anyone help me?

I would be very, very grateful.

Actual code

int pinled = 8;
  
	int pinled2 = 9;
  
	int pinled3 = 10;
  
	int pinled4 = 11;
  
  int pinled5 = 7;
  
	int pinled6 = 6;
  
	int pinled7 = 5;
  
	int pinled8 = 4;

	void setup() {                
		// initialize the digital pin as an output.
		pinMode(pinled, OUTPUT);   
    pinMode(pinled2, OUTPUT);   
    pinMode(pinled3, OUTPUT); 
    pinMode(pinled4, OUTPUT); 
    pinMode(pinled5, OUTPUT);   
    pinMode(pinled6, OUTPUT);   
    pinMode(pinled7, OUTPUT); 
    pinMode(pinled8, OUTPUT);
	}

	// the loop routine runs over and over again forever:
	void loop() {
		digitalWrite(pinled, HIGH);   // turn the LED on 
		delay(1000);               // wait for a second
		digitalWrite(pinled, LOW);    // turn the LED off 
		delay(700);               // wait for a second
    digitalWrite(pinled2, HIGH);   // turn the LED on 
		delay(1200);               // wait for a second
		digitalWrite(pinled2, LOW);    // turn the LED off 
		delay(1100);               // wait for a second
    digitalWrite(pinled3, HIGH);   // turn the LED on 
		delay(800);               // wait for a second
		digitalWrite(pinled3, LOW);    // turn the LED off 
		delay(900);               // wait for a second
    digitalWrite(pinled5, HIGH);   // turn the LED on 
		delay(500);               // wait for a second
		digitalWrite(pinled5, LOW);    // turn the LED off 
		delay(900);               // wait for a second
    digitalWrite(pinled8, HIGH);   // turn the LED on 
		delay(1000);               // wait for a second
		digitalWrite(pinled8, LOW);    // turn the LED off 
		delay(1000);               // wait for a second
    digitalWrite(pinled2, HIGH);   // turn the LED on              // wait for a second
		digitalWrite(pinled7, HIGH);    // turn the LED off 
		delay(1000);               // wait for a second
    digitalWrite(pinled7, LOW);   // turn the LED on 
		digitalWrite(pinled2, LOW);    // turn the LED off 
		delay(700);               // wait for a second
    digitalWrite(pinled, HIGH);   // turn the LED on 
		delay(1000);               // wait for a second
		digitalWrite(pinled, LOW);    // turn the LED off 
		delay(1200);               // wait for a secon
    digitalWrite(pinled3, HIGH);   // turn the LED on 
		delay(1000);               // wait for a second
		digitalWrite(pinled3, LOW);    // turn the LED off 
		delay(700);          
 
    

What have you tried?

If you want different patterns, duplicate your loop() function and give it a different name. Do that 3 times so you have pattern1(), pattern2() and pattern3()

Now put your code inside loop() that checks your buttons and calls the appropriate function

Thanks for trying to use code tags. The three back ticks (```) need to be at the beginning of a line; you missed that for the opening one. It's now fixed.

1 Like

Try the example code "Do several things at the same time".
Also study "Blink without delay".

Hello phoenix_phx

Welcome to the best Arduino forum ever :slight_smile:

What´s the task of the sketch in real life?

Thank you all for answers. I'll try to write code with your tips.

Device will be for kids with autism to improve reflex. My idea is to make some programs for training. The task for kid is to clap light as soon as it appears. In first program (or function) time between flashes will be long (long flash with long gap for example 5-7 seconds). In next programs it will be faster, and the last one will be the fastest (for example 0,2-0,5 s. ). To avoid reprogramming arduino I would like to use buttons with diffrent functions.

Hi! Welcome to the forum.

The time between flashes can also be controlled by a potentiometer instead of buttons. This would simplify the sketch and allow more "time stages".

okay, but there is no place for repetitive continues 1-1-1 than 2-2-2 and 3-3-3 etc but to maintain the randomness of the lights with variable speed. Let's say we have 7 LEDs. In the first program, 1,4,2,6,4,5,7,3 are displayed (in a long loop) but with an average break of 5-7 seconds. In the second program (second button) the sequence is 7,1,6,2,3,4,5 but with an average of 3-5 seconds - faster. The third program has a different sequence and is faster...

Write this on a spreadsheet. Left column is "time" in seconds, right-side are the LEDs.

1 Like

Depending on your programming skill, you could have an array for the sequence of LEDs and a second array filled with the delay times. If you push button 1, that function fills in the arrays how you want - sequence and time delay, and then you call one 'display()' function that steps through the array to flash the LED, delay, repeat for the entire array.

If you push button 2, you fill the array in with a different sequence and different delay times and call the same display() function.

Repeat for as many buttons as you have/want.

1 Like

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