Neopixel / WS2812 Led animation

First of all here is the link to my project and my code for the ceiling lamp I’ve made:

// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"


#define PIXEL_PIN D3
#define PIXEL_COUNT 66
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);


char auth[] = "XXxxXXxxXXxxXX";

WidgetTerminal terminal(V11);

void setup()
{
  Serial.begin(9600);
  delay(5000);
  Blynk.begin(auth);
  pinMode(2, OUTPUT);
  pinMode(1, OUTPUT);
  analogWrite(2, 255);
  analogWrite(1, 255);
  while (Blynk.connect() == false) {
    // Wait until connected
  }
  // This will print Blynk Software version to the Terminal Widget when
  // your hardware gets connected to Blynk Server
  terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
  terminal.flush();
  
  strip.begin();
  strip.show();
}

void loop()
{
    Blynk.run();
}

BLYNK_WRITE(V0) {
    analogWrite(2, param.asInt());
    }

BLYNK_WRITE(V1)
{
    analogWrite(1, param.asInt());
}

//
//  Neopixel
BLYNK_WRITE(V3) {
  switch (param.asInt())
  {
    case 1: { // Item 1
      Serial.println("rainbowCycle selected");
      rainbowCycle(1);
      strip.show();
      break;
    }
    case 2: { // Item 2
      Serial.println("rainbow selected");
      rainbow(1);
      strip.show();
      
      break;
    }
    case 3: { // Item 3
      Serial.println("theatherChaseRainbow selected");
      theaterChaseRainbow(1);
      strip.show();
      break;
    }
    case 4: { // Item 4
      Serial.println("white selected");
      colorAll(strip.Color(255, 255, 255), 1);
      strip.show();
      break;
    }
    case 5: { // Item 5
      Serial.println("Neopixel off selected");
      colorAll(strip.Color(0, 0, 0), 1);
      strip.show();
      break;
    }
    default: {
      Serial.println("Unknown item selected");
    }
  }
}


BLYNK_WRITE(V4)
{
  int shift = param.asInt();
  
  for (int i = 0; i < strip.numPixels(); i++)
  {
    strip.setPixelColor(i, Wheel(shift & 255));
    // OR: strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + shift) & 255));
  }
  strip.show();
}

// Attach a ZeRGBa widget
BLYNK_WRITE(V21) {
    int r = param[0].asInt();
    int g = param[1].asInt();
    int b = param[2].asInt();
    if (r > 0 && g > 0 && b > 0) {

        colorAll(strip.Color(r, g, b), 1);
        strip.show();
    } else {

    }
}


// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget
BLYNK_WRITE(V11)
{
  // Ensure everything is sent
  terminal.flush();
}

  // Some example procedures showing how to display to the pixels:
  // Do not run more than 15 seconds of these, or the b/g tasks
  // will be blocked.
  //--------------------------------------------------------------

void colorAll(uint32_t c, uint8_t wait) {
  uint16_t i;

  for(i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(1);
}

uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}


void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}


//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

The big problem for me is now that if I make a continuous loop like this:

//  Neopixel
BLYNK_WRITE(V3) {
  switch (param.asInt())
  {
    case 1: { // Item 1
      Serial.println("rainbowCycle selected");
      while (1) {
      rainbowCycle(1);
      strip.show();
        }
      break;
    }

I can’t stop the animation from the strandtest example or change the color with the Virtual Pin 4. Should I use an Interrupt for this? Never used them before so I really would appreciate any help!

Thanks a lot

Matte

do
{
  //do stuff here
} while(vPin1 == 1)

A conditional loop is what you are looking for I think :slight_smile:

Hello
I have the same issue but don’t know how to resolve it.

Here is my small program:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

char auth[] = "---";

char ssid[] = "---";
char pass[] = "---";

const int PIN = D4;

const int diod = 72;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(diod, PIN, NEO_GRB + NEO_KHZ800);

BLYNK_WRITE(V0){
  colorWipe(strip.Color(0, 0, 0), 0);
  Blynk.syncAll();
}

BLYNK_WRITE(V2) {
  iceflakes(100, diod);
}

BLYNK_WRITE(V3){
  int r = param[0].asInt(); // get a RED channel value
  int g = param[1].asInt(); // get a GREEN channel value
  int b = param[2].asInt(); // get a BLUE channel value

  colorWipe(strip.Color(r, g, b), 50); // Red
}

BLYNK_WRITE(V5) {
  rainbow(20);
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  strip.begin();
  strip.show();
}

void loop() {
  Blynk.run();
}
void rainbow(uint8_t wait) {
  uint16_t i, j;
  
  for(; ; ) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    j++;
    if(j >= 256) {
      j = 0;
    }
    delay(wait);
  }
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void iceflakes(uint8_t wait, int ile) {

  // Setup the pixel array
  int pixel[ile];
  for(int p=0; p<ile; p++){
    pixel[p] = random(0,255); 
  }

  // Run some snowflake cycles
  for (int j=0; j<200; j++) {
  
    // Every five cycles, light a new pixel
    if((j%5)==0){
      strip.setPixelColor(random(0,ile), 0,0,255);
    }
  
    // Dim all pixels by 10
    for(int p=0; p<ile; p++){
      strip.setPixelColor(p, 0,0,pixel[p] );
      pixel[p] =  pixel[p] - 10;
    }
    strip.show();
    delay(wait);
  }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

It’s working but not well. I can run all virtal pins but I dont know how to break loop… for example rainbow. How I can change efect.

Thanks for any suggestions.
By the way… I try to use somethink like

do
{
  //do stuff here
} while(vPin1 == 1)

but or I don’t know how or I don’t know where to use… it doesn’t work for me.

This is an old topic with old code.

Try my own Blynk WS281x project. It has the control you are wanting.

@Jamin … wow, it’s like I was predicting the future :wink:

1 Like

Hello Sir. Thank you so much for sharing your project and share your time, I realize this tutorial is old, but when I compile the code, then nothing happens, can you help me? if so, please Just let me know it in order to share to you the error I am getting.

Are you saying that when you compile the code nothing happens, or that you get a compilation error?
If you’re getting an error then it’s most likely a missing library, but unless you share the details it’s impossible for us to help.

Pete.

Hello Sir
Thank you so much for taking your time guiding me and answer my question. Ok I will try
to explain you step by step what is the issue I am getting when I upload the code you
provide.
First of all, I am using a NeoPixel ring / ws2812b – 24 leds
Second, I am using a esp8266 wifi board NodemCU
THE CODE I AM USING IS THIS:

#define BLYNK_PRINT Serial
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#define PIN 15 // DIN PIN (GPIO15, D8)
#define NUMPIXELS 24 // Number of you led
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char auth[] = "xxxxxxxxxxxxxxxxxxxxx"; char
ssid[] = "DTVNET_0FE345";
char pass[] = "XXXXXXXXX";
const byte interruptPin = 0;
int state = 6;
int R = 255; int
G = 255;
int B = 255;
WidgetLCD lcd(V5);
BLYNK_WRITE(V0) {
 R = param[0].asInt();
 G = param[1].asInt();
 B = param[2].asInt();
 Blynk.virtualWrite(V1, R);
 Blynk.virtualWrite(V2, G);
 Blynk.virtualWrite(V3, B);
}
BLYNK_WRITE(V1) { 
 R = param[0].asInt();
}
BLYNK_WRITE(V2) {
 G = param[0].asInt();
}
BLYNK_WRITE(V3) {
 B = param[0].asInt();
}
void setup() {
 Serial.begin(115200);
 Serial.println("hello");
 pinMode(interruptPin, INPUT);
 attachInterrupt(interruptPin, neoState, CHANGE);
 Blynk.begin(auth, ssid, pass);
 lcd.clear();
 pixels.begin();
}
void loop() {
Serial.println(state);
 if (state == 0 || state == 1 || state == 2 || state == 6 ) Blynk.run();
lcdShow();
 switch (state) { case 0: for (int i = 0; i <
NUMPIXELS; i++) { pixels.setPixelColor(i,
pixels.Color(R, G, B)); pixels.show();
 }
 break;
 case 1: theaterChase(pixels.Color(R, G, B), 50);
break; case 2:
 colorWipe(pixels.Color(255, 0, 0), 50); // Red
colorWipe(pixels.Color(0, 255, 0), 50); // Green
colorWipe(pixels.Color(0, 0, 255), 50); // Blue break;
 case 3: rainbow(20);
break; case 4:
rainbowCycle(20);
 break; 
 case 5: theaterChaseRainbow(50);
 break;
 }
}
///////////////////////////////////////////////
void neoState() {
state = state + 1;
 if (state > 5) state = 0;
}
void lcdShow() {
lcd.print(6, 0, "MODE"); if
(state == 0) {
 lcd.print(0, 1, " MANUAL ");
 } else if (state == 1) {
lcd.print(0, 1, " THEATER "); }
else if (state == 2) {
 lcd.print(0, 1, " COLOR WIPE ");
 } else if (state == 3) {
lcd.print(0, 1, " RAINBOW ");
 } else if (state == 4) {
 lcd.print(0, 1, " RAINBOW CIRCLE ");
 } else if (state == 5) {
 lcd.print(0, 1, " THEATER RAINBOW");
 }
}
void theaterChase(uint32_t c, uint8_t wait) { for (int
j = 0; j < 10; j++) { //do 10 cycles of chasing for (int
q = 0; q < 3; q++) { for (uint16_t i = 0; i <
pixels.numPixels(); i = i + 3) {
 pixels.setPixelColor(i + q, c); //turn every third pixel on
 }
 pixels.show();
 delay(wait);
 for (uint16_t i = 0; i < pixels.numPixels(); i = i + 3) {
pixels.setPixelColor(i + q, 0); //turn every third pixel off }
 }
 }
} 
void colorWipe(uint32_t c, uint8_t wait) { for
(uint16_t i = 0; i < pixels.numPixels(); i++) {
pixels.setPixelColor(i, c); pixels.show();
 delay(wait);
 }
}
void rainbow(uint8_t wait) {
uint16_t i, j;
 for (j = 0; j < 256; j++) {
if (state != 3) break;
Blynk.run();
 for (i = 0; i < pixels.numPixels(); i++) {
 if (state != 3) break;
Blynk.run();
 pixels.setPixelColor(i, Wheel((i + j) & 255));
 }
 pixels.show();
 delay(wait);
 }
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
 for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
if (state != 4) break; Blynk.run();
 for (i = 0; i < pixels.numPixels(); i++) {
 if (state != 4) break;
Blynk.run();
 pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
 }
 pixels.show();
 delay(wait);
 }
}
void theaterChaseRainbow(uint8_t wait) {
 for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
if (state != 5) break; Blynk.run(); for (int q = 0; q < 3; q++) {
if (state != 5) break; Blynk.run();
 for (uint16_t i = 0; i < pixels.numPixels(); i = i + 3) {
if (state != 5) break; Blynk.run(); 
 pixels.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
 }
 pixels.show();
 delay(wait);
 for (uint16_t i = 0; i < pixels.numPixels(); i = i + 3) {
if (state != 5) break; Blynk.run();
 pixels.setPixelColor(i + q, 0); //turn every third pixel off
 }
 }
 }
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos; if
(WheelPos < 85) {
 return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if (WheelPos < 170) {
WheelPos -= 85;
 return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} 


THIS IS THE ERROR I AM GETTING WHEN I OPEN THE SERIAL MONITOR
__________________________________________________________!

[No%20connection|399x500](upload://xRqGDhmWENMyH9pGixmCzu5xAKw.jpeg) ![Setting%20the%20board|483x500](upload://nxctrCQEMWXmUs23fQcFkzrVeE4.png) 
22:30:55.384 ->
22:30:55.384 -> Abort called
22:30:55.384 ->
22:30:55.384 -> >>>stack>>>
22:30:55.384 ->
22:30:55.384 -> ctx: cont
22:30:55.384 -> sp: 3ffffdb0 end: 3fffffc0 offset: 01b0
22:30:55.384 -> 3fffff60: 3ffe8794 00000001 3ffee9fc 40203fd5
22:30:55.384 -> 3fffff70: 3ffe8704 00000001 3ffee9fc 3ffeeb1c
22:30:55.431 -> 3fffff80: 3fffdad0 3ffee9fc 3ffeeae8 40100546
22:30:55.431 -> 3fffff90: feefeffe 3ffee9fc 3ffeeae8 4020251d
22:30:55.431 -> 3fffffa0: 3fffdad0 00000000 3ffeeae8 40204800
22:30:55.431 -> 3fffffb0: feefeffe feefeffe 3ffe8558 40100831
22:30:55.431 -> <<<stack<<<
22:30:55.431 ->
22:30:55.431 -> ets Jan 8 2013,rst cause:2, boot mode:(3,4)
22:30:55.431 ->
22:30:55.431 -> load 0x4010f000, len 1384, room 16
22:30:55.431 -> tail 8 
22:30:55.431 -> chksum 0x2d
22:30:55.431 -> csum 0x2d
22:30:55.431 -> vac02aff5
22:30:55.431 -> ~ld
22:30:55.525 -> hello
22:30:55.525 -> ISR not in IRAM!
22:30:58.715 ->
22:30:58.715 -> Abort called
22:30:58.715 ->
22:30:58.715 -> >>>stack>>>
22:30:58.715 ->
22:30:58.715 -> ctx: cont
22:30:58.715 -> sp: 3ffffdb0 end: 3fffffc0 offset: 01b0
22:30:58.715 -> 3fffff60: 3ffe8794 00000001 3ffee9fc 40203fd5
22:30:58.715 -> 3fffff70: 3ffe8704 00000001 3ffee9fc 3ffeeb1c
22:30:58.715 -> 3fffff80: 3fffdad0 3ffee9fc 3ffeeae8 40100546
22:30:58.715 -> 3fffff90: feefeffe 3ffee9fc 3ffeeae8 4020251d
22:30:58.715 -> 3fffffa0: 3fffdad0 00000000 3ffeeae8 40204800
22:30:58.762 -> 3fffffb0: feefeffe feefeffe 3ffe8558 40100831
22:30:58.762 -> <<<stack<<<
22:30:58.762 ->
22:30:58.762 -> ets Jan 8 2013,rst cause:2, boot mode:(3,4) 22:30:58.762
->
22:30:58.762 -> load 0x4010f000, len 1384, room 16
22:30:58.762 -> tail 8
22:30:58.762 -> chksum 0x2d
22:30:58.762 -> csum 0x2d
22:30:58.762 -> vac02aff5
22:30:58.762 -> ~ld
22:30:58.808 -> hello
22:30:58.808 -> ISR not in IRAM!
22:31:02.000 ->
22:31:02.000 -> Abort called
22:31:02.000 ->
22:31:02.000 -> >>>stack>>>
22:31:02.000 ->
22:31:02.000 -> ctx: cont
22:31:02.037 -> sp: 3ffffdb0 end: 3fffffc0 offset: 01b0
22:31:02.037 -> 3fffff60: 3ffe8794 00000001 3ffee9fc 40203fd5
22:31:02.037 -> 3fffff70: 3ffe8704 00000001 3ffee9fc 3ffeeb1c
22:31:02.037 -> 3fffff80: 3fffdad0 3ffee9fc 3ffeeae8 40100546
22:31:02.037 -> 3fffff90: feefeffe 3ffee9fc 3ffeeae8 4020251d
22:31:02.037 -> 3fffffa0: 3fffdad0 00000000 3ffeeae8 40204800
22:31:02.037 -> 3fffffb0: feefeffe feefeffe 3ffe8558 40100831
22:31:02.037 -> <<<stack<<<
22:31:02.072 ->
22:31:02.072 -> ets Jan 8 2013,rst cause:2, boot mode:(3,4)
22:31:02.072 -> 
22:31:02.072 -> load 0x4010f000, len 1384, room 16
22:31:02.072 -> tail 8
22:31:02.072 -> chksum 0x2d
22:31:02.072 -> csum 0x2d
22:31:02.072 -> vac02aff5
22:31:02.072 -> ~ld
22:31:02.142 -> hello
22:31:02.142 -> ISR not in IRAM! 


I am sorry for being annoying, but I really want to do this project works. Thank you so much in advance Sir.

Please edit your post and add triple backticks at the beginning and end of your code, and your serial output, so that they display correctly.

Triple backticks look like this:
```

Pete.

Hello Sir, I did what you suggested.

Is it good?

Thank you in advance sir

If you google this error, or search this forum, you’ll find that its something that happens with the later versions of the ESP core when the function called by the interrupt (the ISR) isn’t defined correctly. Earlier version off the ESP core were not as fussy about this.

You need to change this line:

to this:

ICACHE_RAM_ATTR void neoState() {

However, you’ll then probably find that your sketch still has issues, because of what is in your void loop.

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Pete.

Hello Sir, thank you so much againg for taking your time replying my questions. Ok I already know there are some issues on the lastest version of ESP8266, but could you help with this please?

I want to control a ws2812 neopixel ring with my esp8266 lolin and a Blynk app where I can chose some effects such as rainbow, chase and aome others if it is possible. Could you give me an advice where can I find a good example?

That is to say, can you help me with this project? I did step by step what the tutorial says but my esp8266 dosen´t get connection. I´ll appreciate it Sir

this is the project

I’m not aware of any issues with any ESP8266’s. Would you like to elaborate?

The tutorial is for an Arduino using a serial connection, not a standalone ESP/NodeMCU device.

Pete.

I am sorry Sir. but, how do you say that the tutorial is for an Arduino?, if obviously in the video the man is using a esp8266, ws2812 led strip and Blynk app.

I don´t understand your reply Sir.

I am a Blynk user looking for some help.

This .rar file contains this file, called circuit_example.jpg:

That looks like

Pete.

Sir, that is to say that this tutorial is fake? ohh that is disappointed , anyway, I have been looking for a project like this on internet, but I did not get good luck, could you guide me to find a project like this? If you know abou it, for sure.

Thanks a lot for replying

I didn’t say that, I said that the .rar document that you linked to was a project for an Arduino. I’ve not watched the YouTube video that you’ve linked to.

There are over 50 results for “WS2812” on this forum:
https://community.blynk.cc/search?q=WS2812

Your stats say that you’ve viewed 16 topics since you joined the forum.
image
Maybe you should do more research yourself.

Pete.