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