How to time a loop then move to the next?

Hello, I’m programming some WS2812B LEDs using an Arduino Uno R3. I’ve figured out enough to make some neat LED sequences and now I want the to run one after the other. I want to have the 1st run for like 3min, then move to the next, then start over. Can anyone please tell me how to do this or point me to an example? I tried to look it up but I’m not sure what to look for.

How would I run this 1st code, then go to the 2nd, then to the top?
Thank you


void loop() {
// Turn the LED on, then pause

for(int i = 0;i < NUM_LEDS;i++){
leds = CRGB(50,150,60);

  • }*

  • FastLED.show();*

  • delay(500);*

  • // Now turn the LED off, then pause*

  • for(int i = 0;i < NUM_LEDS;i++){*
    _ leds = CRGB(150,50,60);_
    }
    * FastLED.show();*
    * delay(500);*
    }
    ---------------------------------------------------------------------
    Do that then move to this?
    ----------------------------------------------------------------------
    void loop() {
    * // First slide the led in one direction*
    * for(int i = 0; i > NUM_LEDS; i++) {
    _
    // Set the i’th led to red*_
    _ leds = CRGB::Crimson;
    * // Show the leds*
    * FastLED.show();
    // now that we’ve shown the leds, reset the i’th led to black*
    //leds = CRGB::Lime;_

* delay(30);*
* }*

Dare I say “blink without delay” might be of interest?
3 minutes is 180000 milli seconds.

That blink code was just example program I copied real quick. What I want to do is turn on my Led Strip then have it run a code for a certain amount of time, like blink or something else. This is where I have the problem. After "blink" has been running for a while, I want the Arduinio to automatically go to the next sequence, lets say its a "fade" but its not important. Then after a few minutes start at the top.

I want to know if this is possible and how? Sorry if this is a noob question.

something like this:

untested

unsigned long functionDuration = 30*60000UL; 
unsigned long cycleStart;
int state;

void setup() 
{
  Serial.begin(9600); 
}

void loop()
{ 
  if (millis() - cycleStart >= functionDuration)
  {
    state++;
    cycleStart = millis();
  }
  if (state == 0)
  {
    myLEDFunctionA();
  }
  else if (state == 1)
  {
    myLEDFunctionB();
  }
  else if (state == 2)
  {
    myLEDFunctionC();
  }
  else if (state == 3)
  {
    // well... you get it...
  }
  else state = 0;
  
}

void myLEDFunctionA()
{
  
}

void myLEDFunctionB()
{
  
}

void myLEDFunctionC()
{
  
}

Yes it is possible. How? Use a finite state machine as shown inthe blink without delay sketch. Only you are not blinking but changing the pattern.

Use CODE TAGS ("#") tool button when you post your code so it looks like this …

 void loop() { 
  // Turn the LED on, then pause
  
  for(int i = 0;i < NUM_LEDS;i++){
  leds = CRGB(50,150,60);
  }
  
  FastLED.show();
  delay(500);
  // Now turn the LED off, then pause
  for(int i = 0;i < NUM_LEDS;i++){
  leds = CRGB(150,50,60);
}
  FastLED.show();
  delay(500);
}

---------------------------------------------------------------------

Do that then move to this?


----------------------------------------------------------------------

void loop() { 
   // First slide the led in one direction
   for(int i = 0; i > NUM_LEDS; i++) {
      // Set the i'th led to red 
      leds = CRGB::Crimson; 
      // Show the leds
      FastLED.show();
      // now that we've shown the leds, reset the i'th led to black
      //leds = CRGB::Lime;
      
      delay(30);
   }

Also, can you please post your ENTIRE sketch instead of just a piece of it ?

Ok, I'll post the code properly next time. Thanks for all the help. I got it working now!

Forum etiquette dictates that you post your final working code before disappearing back into the woodwork. (that others may benefit. from your trials and tribulations...)(who knows, you might even get a karma click or two out of it)

My code isn’t actually done, but I can post a working rough copy.

Oh yeah, what does this line do?
Serial.begin(9600);

#include "FastLED.h"
#define NUM_LEDS 30
#define datapin  4
CRGB ledsA[NUM_LEDS];


void setup(){
  Serial.begin(9600);
  LEDS.setBrightness(255);
  LEDS.addLeds<WS2812B, datapin, GRB>(ledsA, NUM_LEDS);
 // memset(ledsA, 0,  NUM_LEDS * sizeof(struct CRGB)); 
}

unsigned long functionDuration = 30*60UL;//30*60000UL; 
unsigned long cycleStart;
int state;

//void setup() 
//{
//  Serial.begin(9600); 
//}

void loop()
{ 
  if (millis() - cycleStart >= functionDuration)
  {
    state++;
    cycleStart = millis();
  }
  if (state == 0)
  {
    myLEDFunctionA();
  }
  else if (state == 1)
  {
    myLEDFunctionB();
  }
  else if (state == 2)
  {
    myLEDFunctionC();
  //}
  //else if (state == 3)
  //{
    // well... you get it...
  }
  else state = 0;
  
}

void myLEDFunctionA()
{
  for(int i = 0;i < NUM_LEDS;i++){
  ledsA[i] = CRGB(123,150,90);
  }
  
  FastLED.show();
  delay(20);
  // Now turn the LED off, then pause
  for(int i = 0;i < NUM_LEDS;i++){
  ledsA[i] = CRGB(150,50,60);
}
  FastLED.show();
  delay(20);
}

void myLEDFunctionB()
{
  for(int i = 0; i > NUM_LEDS; i++) {
		// Set the i'th led to red 
		ledsA[i] = CRGB::Crimson; 
		// Show the leds
		FastLED.show();
		// now that we've shown the leds, reset the i'th led to black
		//leds[i] = CRGB::Lime;
		
		delay(30);
	}

	// Now go in the other direction.  
	for(int i = NUM_LEDS-1; i >= 0; i--) {
		// Set the i'th led to red 
		ledsA[i] = CRGB::Navy;
		// Show the leds
		FastLED.show();
		// now that we've shown the leds, reset the i'th led to black
		ledsA[i] = CRGB::SkyBlue;
		// Wait a little bit before we loop around and do it again
                delay(30);
}
}
void myLEDFunctionC(){
 
  
    for(int i = 0; i < NUM_LEDS; i++) {
  static uint8_t hue = 0;
    for (int px = NUM_LEDS - 1; px > 0; px--)
      {
        ledsA[px] = ledsA[px - 1];
      }
    ledsA[0] = CHSV(hue++, 255, 255);
    FastLED.show();
    delay(10);
  //return;
}
}

Oh yeah, what does this line do? Serial.begin(9600);

I assume your not joking. The Serial.begin([baudrate]}; statement initializes the serial port to a specific baudrate. The available rates are listed in the drop down menu of the Serial Monitor (Tool\Serial Monitor) but it connects to the device connected to COM PORT of your choice, meaning it would probably work just as well with a non-arduino device connected on that port if that device had serial capability. The serial monitor doesn't know or care (AFAIK) if the device it is connecting to is an arduino. We use the serial port for many reasons, not the least of which is to view data collected from devices we are controlling with our arduino, but one use of it is almost more important and that is diagnostic print outs. If you have some code that is supposed to calculate some value you can print that value to the serial port with a Serial.print(x); statement. You print the state of booean flags (true=1, false =0). You can print things inside a FOR loop to see if it is working correctly. The list goes on and on. I use it all the time for everything. If you bought the new Sparkfun TTL logic serial lcd you could print stuff to it with your serial port:

https://www.sparkfun.com/products/9395

Communication with SerLCD requires 5V TTL serial at a default baud rate of 9600bps (8-N-1). You can adjust the baud to any standard rate between 2400 and 38400bps.

http://www.karlssonrobotics.com/cart/serial-enabled-16x2-lcd-black-on-green-5v/?gclid=CjwKEAjwg_afBRD3rpChlqiKt1ESJACwY6NkXdoDTHo7liNe-MAapyTZMy0RiagWAzZFaGXuTe0jxhoC12vw_wcB

Communication with SerLCD requires 5V TTL serial at a default baud rate of 9600bps (8-N-1). You can adjust the baud to any standard rate between 2400 and 38400bps.

You can also use unused pins for feedback so if you turn D4 ON, and you have D5 setup as an INPUT and connected to D4, you can use D5 to read the state of D4 using a digitalRead(4); . if the nature of the switching is too fast to use an LED as an indicator. Most people don't think of that. If you turned on D4, is there a command you could print to the serial port to tell you if it was on ? (Using feedback detection and digitalRead() all it takes is a serial print :

x= digitalRead(4);
Serial.println(x);

(don't forget you can use faster speeds if the prints are slowing down your code, like 115200 baud.)

Ha HA . No, I wasn't joking . I hardly know anything about coding. I had a C++ class, but the teacher was horrible and wouldn't make time to answer my questions. I've had some experience with programming a PicKit before. Now that I have time I trying to teach myself by jumping right in and making programs.

Thanks for writing out all that info.