Good Morning All,
I'm trying to make a simple code for a child's night light using:
WS2812 LED Strips (2 strips of 10 LED's)
Simple 2 Pole Push Button
3 Pole Sound Sensor
Nano Board.
Ultimately I'd like to expand the program to several different options but at present I'm trying to combine a simple solid color pattern and a music reactive code
I've used the basic button code (included below) to cycle through 5 color options with no problem and I've used the music reactive code (Included below) with no problem. It's when I combine the 2 coded that things go south...I get a solid color that does not respond to music and can not cycle through the 2 programs.
I know the wiring and set up works because I can run each seperate sketch on the night light and get expected results so I'm assuming code issue and given my level of inexperience I'm hoping for something simple that I'm leaving out...
CODE FOR SIMPLE PUSH BUTTON 2 OPTIONS:
#include <FastLED.h>
#include <OneButton.h>
#define NUM_LEDS_PER_STRIP 10
#define BTN_PIN 10
CRGB firstleds[NUM_LEDS_PER_STRIP];
CRGB secondleds[NUM_LEDS_PER_STRIP];
uint8_t patternCounter = 0;
uint8_t hue = 0;
// Push button connected between pin 7 and GND (no resistor required)
OneButton btn = OneButton(BTN_PIN, true, true);
void setup() {
FastLED.addLeds<WS2812B, 4>(firstleds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B, 6>(secondleds, NUM_LEDS_PER_STRIP);
FastLED.setBrightness(100);
btn.attachClick(nextPattern);
}
void loop() {
switch (patternCounter) {
case 0:
Rainbow();
break;
case 1:
ColorFade();
break;
}
FastLED.show();
btn.tick();
}
void nextPattern() {
patternCounter = (patternCounter + 1) % 2; // Change the number after the % to the number of patterns you have
}
//------- Put your patterns below -------//
void Rainbow() {
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
firstleds[i] = CHSV(hue + (i * 10), 255, 255);
secondleds[i] = CHSV(hue + (i * 10), 255, 255);
}
EVERY_N_MILLISECONDS(50) {
hue++;
}
}
void ColorFade() {
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
firstleds[i] = CHSV(hue, 255, 255);
secondleds[i] = CHSV(hue, 255, 255);
}
EVERY_N_MILLISECONDS(200) {
hue++;
}
}
CODE FOR SIMPLE MUSIC REACTIVE STRIP
#include <FastLED.h> // https://github.com/FastLED/FastLED
#define NUM_LEDS_PER_STRIP 10 // Number of LEDs
#define SENSITIVITY 300 // Ranges from 0 to 1023
#define MAX_BRIGHTNESS 200 // Ranges from 0 to 255
#define SOUND_PIN A0 // Connect sound detector to this pin
#define SATURATION 150 // Ranges from 0 to 255
#define MINVAL 60
#define HUE_INIT 10
#define HUE_CHANGE 1
CRGB firstleds[NUM_LEDS_PER_STRIP];
CRGB secondleds[NUM_LEDS_PER_STRIP];
byte brightness[NUM_LEDS_PER_STRIP];
byte hue[NUM_LEDS_PER_STRIP];
int analogVal;
int DELAY;
void setup() {
Serial.begin(9600);
pinMode(SOUND_PIN, INPUT);
FastLED.addLeds<WS2812B, 4>(firstleds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B, 6>(secondleds, NUM_LEDS_PER_STRIP);
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
brightness[i] = 0;
hue[i] = 0;
}
//Turn off all the LEDs
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
firstleds[i] = CRGB::Black;
secondleds[i] = CRGB::Black;
}
//Update the LED strip
FastLED.show();
}
void loop() {
analogVal = analogRead(SOUND_PIN);
if (analogVal > SENSITIVITY)
analogVal = SENSITIVITY;
if (analogVal < MINVAL)
analogVal = 0;
/*----------------------------------------------------------
- 5 styles for sound reactive led strip are given below in different lines.
- Uncomment the function which you want to try and comment the others.
- "LinearFlowing" is uncommented by default.
-----------------------------------------------------------*/
LinearFlowing();
// LinearReactive();
// BrightnessReactive();
//CentreProgressive();
// EdgeProgressive();
FastLED.show();
}
void LinearFlowing() {
byte val = map(analogVal, 0, SENSITIVITY + 1, 0, MAX_BRIGHTNESS);
DELAY = map(analogVal, 0, SENSITIVITY + 1, 20, 1);
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
brightness[NUM_LEDS_PER_STRIP - i] = brightness[NUM_LEDS_PER_STRIP - i - 1];
}
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
hue[NUM_LEDS_PER_STRIP - i] = hue[NUM_LEDS_PER_STRIP - i - 1];
}
brightness[0] = val;
byte hue = HUE_INIT;
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
firstleds[i] = CHSV(hue += HUE_CHANGE, SATURATION, brightness[i]);
secondleds[i] = CHSV(hue += HUE_CHANGE, SATURATION, brightness[i]);
}
delay(DELAY);
}
MY ATTEMPT TO COMBINE THE 2 CODES:
#include <FastLED.h>
#include <OneButton.h>
#define NUM_LEDS_PER_STRIP 10 // Number of LEDs
#define SENSITIVITY 300 // Ranges from 0 to 1023
#define MAX_BRIGHTNESS 200 // Ranges from 0 to 255
#define SOUND_PIN A0 // Connect sound detector to this pin
#define BTN_PIN 10
#define SATURATION 150 // Ranges from 0 to 255
#define MINVAL 60
#define HUE_INIT 10
#define HUE_CHANGE 1
CRGB firstleds[NUM_LEDS_PER_STRIP];
CRGB secondleds[NUM_LEDS_PER_STRIP];
byte brightness[NUM_LEDS_PER_STRIP];
byte hue[NUM_LEDS_PER_STRIP];
uint8_t patternCounter = 0;
int analogVal;
int DELAY;
OneButton btn = OneButton(BTN_PIN, true, true);
void setup() {
Serial.begin(9600);
pinMode(SOUND_PIN, INPUT);
FastLED.addLeds<WS2812B, 4>(firstleds, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B, 6>(secondleds, NUM_LEDS_PER_STRIP);
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
brightness[i] = 0;
hue[i] = 0;
}
//Turn off all the LEDs
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
firstleds[i] = CRGB::Black;
secondleds[i] = CRGB::Black;
}
//Update the LED strip
FastLED.show();
btn.attachClick(nextPattern);
}
void loop() {
switch (patternCounter) {
case 0:
LinearFlowing();
break;
case 1:
BlueLight();
}
FastLED.show();
btn.tick();
}
void nextPattern() {
patternCounter = (patternCounter + 1) % 2;
}
void LinearFlowing() {
analogVal = analogRead(SOUND_PIN);
if (analogVal > SENSITIVITY)
analogVal = SENSITIVITY;
if (analogVal < MINVAL)
analogVal = 0;
byte val = map(analogVal, 0, SENSITIVITY + 1, 0, MAX_BRIGHTNESS);
DELAY = map(analogVal, 0, SENSITIVITY + 1, 20, 1);
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
brightness[NUM_LEDS_PER_STRIP - i] = brightness[NUM_LEDS_PER_STRIP - i - 1];
}
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
hue[NUM_LEDS_PER_STRIP - i] = hue[NUM_LEDS_PER_STRIP - i - 1];
}
brightness[0] = val;
byte hue = HUE_INIT;
for (int i = 0; i <= NUM_LEDS_PER_STRIP; i++) {
firstleds[i] = CHSV(hue += HUE_CHANGE, SATURATION, brightness[i]);
secondleds[i] = CHSV(hue += HUE_CHANGE, SATURATION, brightness[i]);
}
delay(DELAY);
}
void BlueLight() {
for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
firstleds[i] = CHSV(160, 128, 255);
secondleds[i] = CHSV(160, 128, 255);
}
FastLED.show();
}
I've tried to search for information on line to correct this but I'm getting confused with the options from multiple sources. Does anyone have a suggestion for a good onliine reference for basic arduino coding/problem solving. (I have about 4 bookmarked in my computer but it gets confusing).
Given that the individual programs work as expected on the project I have not included a mock up of the wiring but can if needed.
Thanks in advance for help.