How to get LED rainbow run forever

I am coding the neopixel LED to go rainbow, but eventually the rainbow has to stop changing the color light and I don't know how to last the rainbow flash forever. Please help me solve this

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 3
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

 for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i=0; i<pixels.numPixels(); i++) { 
      int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
      pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
    }
    pixels.show();
    delay(10);

Please post your full sketch rather than just a snippet of it

I am controling that Neopixel light by bluetooth.

#include <SoftwareSerial.h> 
SoftwareSerial mybluetooth(2,3);
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 3
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char state;

void setup() {

pixels.begin();
 Serial.begin(9600); // Kết nối bluetooth module ở tốc độ 9600
mybluetooth.begin(9600);
pixels.begin();
}
 
void loop() {
 pixels.clear();

  
  
  if(mybluetooth.available() > 0){
   // Đọc giá trị nhận được từ bluetooth
   state = mybluetooth.read();
  Serial.println(state);
  Serial.println("");
 } else {
   state = 0;
   }
   if(state == '1')
   for(int i = 0; i<NUMPIXELS; i++){
    pixels.setPixelColor(i, pixels.Color(255,140,0));
    pixels.show();
   } if (state == '2'){
  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i=0; i<pixels.numPixels(); i++) { 
      int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
      pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
    }
    pixels.show();
    delay(10);
  }
}}
  

Do you want the rainbow effect to repeat without the need to send a '2' again ?

If so then remove the line of code that sets state to 0 when nothing is available from Bluetooth

SoftwareSerial and neopixel libraries are basically enemies of each other. Because the neopixel libraries disable interrupts while updating the strip and SoftwareSerial relies on interrupts, I think that your chances are slim that it will work reliably.

No I mean when i send the data 2 the color changing gonna keep running until there is another data send to it

I'm using them all the time, it's okay i guess. I need the serial to send the data.

That is what I was describing when I said

Have you tried my suggestion ?

Who is "them"? The combination of SoftwareSerial (to receive) and Neopixel library?

Remove this from loop()

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