Hello! I am currently working with individually adressable LEDs aka the NeoPixels (RGBW chip). My goal is to adjust the brightness of the strip by using a remote control while different programs are running and then read the final value of brightness (a value between 0-255).
With this code so far it works fine, red can be dimmed.
#include <RCSwitch.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN 10
RCSwitch mySwitch = RCSwitch();
Adafruit_NeoPixel strip = Adafruit_NeoPixel(500, PIN, NEO_GRBW + NEO_KHZ800);
int counter = 0;
void setup()
{
strip.begin();
strip.clear();
strip.show();
Serial.begin(9600);
mySwitch.enableReceive(0);
pinMode(PIN, OUTPUT);
}
void loop() {
if (mySwitch.available())
{int value = mySwitch.getReceivedValue();
if (value == 20483)
{counter = counter + 5;
if (counter >= 255) {
counter = 255;
} }
if (value == 20492)
{counter = counter - 5;
if (counter <= 0) {
counter = 0;
} }
Serial.println(counter);
mySwitch.resetAvailable();
strip.fill(strip.Color(counter, 0, 0, 0), 0, 14);
strip.show();
}
}
By using "Processing" as GUI or just the Serial Monitor, I am sending letters to Arduino to switch between different use-cases. For example if I type "a" LEDs will light up red at 1/5 of brightness first, but the brightness will not update when I push up or down buttons on the remote control. I have to type "a" again so that I can get the updated value. As long as I am in case 'a', I want to be able to adjust the brightness with the rc. I will have a lot more than just two cases, but only posting those two guys to avoid complexity. Any help to fix the code is appreciated!
#include <RCSwitch.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN 10
RCSwitch mySwitch = RCSwitch();
Adafruit_NeoPixel strip = Adafruit_NeoPixel(500, PIN, NEO_GRBW + NEO_KHZ800);
int cycle[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int counter = 55;
void setup()
{
strip.begin();
strip.clear();
strip.show();
Serial.begin(9600);
mySwitch.enableReceive(0);
pinMode(10, OUTPUT);
}
void remote() {
if (mySwitch.available())
{int value = mySwitch.getReceivedValue();
if (value == 20483)
{counter = counter + 5;
if (counter >= 255) {
counter = 255;
} }
if (value == 20492)
{counter = counter - 5;
if (counter <= 0) {
counter = 0;
} }
Serial.println(counter);
mySwitch.resetAvailable();
}}
void loop() {
remote();
if(Serial.available()){
char val = Serial.read();
switch (val){
case 'a':
strip.fill(strip.Color(counter, 0, 0, 0), 0, 14);
strip.show();
while(Serial.available() == 0);
break;
case 'b':
while(cycle[1] < 10){cycle[1]++;
strip.fill(strip.Color(counter, 0, 0, 0), 116, 353); // (R, G, B), Pixel pos., +n Pixels
strip.show();
delay(1000);
strip.fill();
strip.show();
delay(1000);}
while(Serial.available() == 0);
break;
}
}}