Loop question

Hi there, I'm trying to create a LED strip controller using arduino, and as for now I've managed to get it up and running with basic tests.

I want to control my LED strip using a RC controller, which works fine for now, but now my problem comes; Lets say i want to have 10 modes of different blinking/colour patterns on the LEDS, how would i do that?

I figured i wanted to make a for loop to count how many times the switch on the RC controller turns on and of, and thats fine, but how do i engage a new loop or something to take care of the leds?

See my attached code:

int ch1;
int l1 = 8;
int l2 = 12;
int l3 = 13;

void setup() {

  pinMode(5, INPUT);
  pinMode(l1, OUTPUT);
  pinMode(l2, OUTPUT);
  pinMode(l3, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  
  ch1 = pulseIn(5, HIGH, 25000);
  if(ch1>1000){Serial.println("Left Switch: Engaged"); digitalWrite(l1, HIGH); } 
  if(ch1<1000){Serial.println("Left Switch: Disengaged"); digitalWrite(l1, LOW); }

  Serial.println();
}

but how do i engage a new loop or something to take care of the leds?

That "or something" is a function. Write a function for each mode, and call the appropriate function.

As easy as that? Can i write a function with a new loop with delays that blinks the LEDs? Or do i have to call the function every time in the existing loop?

jastrup:
As easy as that? Can i write a function with a new loop with delays that blinks the LEDs? Or do i have to call the function every time in the existing loop?

A function is like a grouping of commands. Instead of writing the same commands over and over again you just write them once in a function, then call that function whenever you want to run those commands.

When you call a function only that function happens until it's finished, then your main program carries on.

If you want more than one thing to appear to happen at the same time you should never ever use delay(). Instead, look at the BlinkWithoutDelay example, and also investigate the use of Finite State Machines.

Thanks alot!

I've experimented some now with millis() and getting the blinks to appear without delays, but still i encounter some issues which i seem to need delay for.

Lets say i have two strips, A and B.

If i want to Blink A twice with a delay of 50ms, and then wait 500ms before i blink B twice, i can't figure out how to do that without using delays, even tho i don't want to use
delays.

Any ideas .. or examples out there?

You could have an array of delay values and use that coupled with millis() to decide when the strip should turn on or off...

That could work, but would you or anyone be kind enough to write a small example for me? I'm a total newbie here... Sorry if its too much to ask. :slight_smile: