Hello, I am relatively a newbie on programming and just started exploring neopixels recently.
Currently, I'm working on a project involving 2 neopixels strips and an IR obstacle sensor. The first strip changes effects when the IR sensor is triggered, the second strip should be running a different effect constantly but I cant get it to do so, so I just left it on fill_solid().
[PS. The code for the effects are mainly from Tweaking4All, thus the delays(). Also, I am not so sure how to make them into millis.]
Although It seems to be running fine on my Bluno Beetle but I just want to be sure since I might be missing some points. Thanks in advance!
/* This code uses 1 IR proximity sensor, 2 Neopixels strips that produces 2 different effects, and 1 5v LED strip that is constantly on and directly connected to the power supply.
* The effects on Neopixel Strip A will change whenever the IR is triggered. Neopixel Strip B however is to produce the same effect throughout the program.
* SOURCES: https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/ && IR Proximity Sensor interface code (Turns on an LED on when obstacle is detected, else off.) by blog.circuits4you.com 2016
*/
//IR SENSOR
const int ProxSensor = 4;
//FastLED Effects
#include "FastLED.h"
#define NUM_LEDS 60 //leds per strip
CRGB leds[NUM_LEDS];
CRGB SNOW[NUM_LEDS];
#define PIN 3 //Neopixels Strip A
#define PIN2 5 //Neopixels Strip B
/* === START FastLED EFFECTS: === */
// switches off all LEDs
void showProgramCleanUp(long delayTime)
{
for (int i = 0; i < NUM_LEDS; ++i) {
leds[i] = CRGB::Black;
SNOW[i] = CRGB::Black;
}
FastLED.show();
delay(delayTime);
}
void showStrip() {
#ifndef ADAFRUIT_NEOPIXEL_H
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifndef ADAFRUIT_NEOPIXEL_H
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
//SNOW SPARKLE EFFECT
void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
setAll(red,green,blue);
int Pixel = random(NUM_LEDS);
setPixel(Pixel,0xff,0xff,0xff);
showStrip();
delay(SparkleDelay);
setPixel(Pixel,red,green,blue);
showStrip();
delay(SpeedDelay);
}
void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
setAll(0,0,0);
for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
// fade brightness all LEDs one step
for(int j=0; j<NUM_LEDS; j++) {
if( (!meteorRandomDecay) || (random(10)>5) ) {
fadeToBlack(j, meteorTrailDecay );
}
}
// draw meteor
for(int j = 0; j < meteorSize; j++) {
if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
setPixel(i-j, red, green, blue);
}
}
showStrip();
delay(SpeedDelay);
}
}
void fadeToBlack(int ledNo, byte fadeValue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
uint32_t oldColor;
uint8_t r, g, b;
int value;
oldColor = strip.getPixelColor(ledNo);
r = (oldColor & 0x00ff0000UL) >> 16;
g = (oldColor & 0x0000ff00UL) >> 8;
b = (oldColor & 0x000000ffUL);
r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
strip.setPixelColor(ledNo, r,g,b);
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
leds[ledNo].fadeToBlackBy( fadeValue );
#endif
}
//SETUP
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(PIN, OUTPUT);
pinMode(PIN2, OUTPUT);
pinMode(ProxSensor,INPUT);
FastLED.addLeds<WS2812, PIN>(leds, NUM_LEDS); //Neopixels Trigger ON
FastLED.addLeds<WS2812, PIN2>(SNOW, NUM_LEDS); //pin for "SNOW"
}
//START**
void loop() {
fill_solid(SNOW, NUM_LEDS, CRGB::White); //LED2 constantly ON
if(digitalRead(ProxSensor)==HIGH) //Check the sensor output
{
SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));
delay (100);
Serial.print("LED ON");
Serial.println();
delay (100);
}
else
{
Serial.print("Fade off... Waiting (15 seconds duration)");
Serial.println();
meteorRain(0xff, 0xff, 0xff, 10, 64, true, 70);
delay (1000);
// delay (100);
}
delay(100); // wait for a second
}