With this script I want to change between 3 modes of LED illumination. I want to change…
- User
With this script I want to change between 3 modes of LED illumination. I want to change between them radiocontrolled. I use a futaba T14SG RC transmitter. Signal from the Futaba receiver goes to pin2 of the arduino. The output of the receiver CH5 works correct, I tested with a servo. When I try to switch between the modes on the transmitter, the LED’s do not change - LED's stay blue. Is there still an error in the script? If so, can you correct it please. #include <FastLED.h>
#define LED_PIN 6 #define COLOR_ORDER GRB #define CHIPSET WS2811 #define NUM_LEDS 30
#define BRIGHTNESS 100 #define FRAMES_PER_SECOND 60
#define RC_INPUT_PIN 2
bool gReverseDirection = false;
int currentPalette = 0; int mode = 0;
CRGB leds[NUM_LEDS];
CRGBPalette16 gPal;
void setup() { delay(3000); // sanity delay FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); FastLED.setBrightness(BRIGHTNESS);
attachInterrupt(digitalPinToInterrupt(RC_INPUT_PIN), onRCInputChange, CHANGE); // Register interrupt for RC input changes
// Palette setup gPal = CRGBPalette16(CRGB::Red, CRGB::Red, CRGB::Yellow, CRGB::White); }
void loop() { // Update palette based on mode switch (mode) { case 0: gPal = CRGBPalette16(CRGB::Red, CRGB::Red, CRGB::Yellow, CRGB::White); Fire2012WithPalette(); // run simulation frame, using palette colors break; case 1: gPal = CRGBPalette16(CRGB::Aqua, CRGB::Blue, CRGB::Aqua, CRGB::White); Fire2012WithPalette(); // run simulation frame, using palette colors break; case 2: gPal = CRGBPalette16(CRGB::Green, CRGB::Green, CRGB::Yellow); Fire2012WithPalette(); // run simulation frame, using palette colors break; }
FastLED.show(); // display this frame FastLED.delay(500 / FRAMES_PER_SECOND); }
#define COOLING 55 #define SPARKING 120
void Fire2012WithPalette() { static uint8_t heat[NUM_LEDS];
for (int i = 0; i < NUM_LEDS; i++) { heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2)); }
for(int k = NUM_LEDS - 1; k >= 2; k–) { heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3; }
if (random8() < SPARKING) { int y = random8(7); heat[y] = qadd8(heat[y], random8(160, 255)); }
for (int j = 0; j < NUM_LEDS; j++) { uint8_t colorindex = scale8( heat[j], 240); CRGB color = ColorFromPalette( gPal, colorindex); int pixelnumber; if( gReverseDirection ) { pixelnumber = (NUM_LEDS-1) - j; } else { pixelnumber = j; } leds[pixelnumber] = color; } }
void onRCInputChange() { int pulseDuration = pulseIn(RC_INPUT_PIN, HIGH, 20000); // Read the pulse duration of the receiver input signal
if (pulseDuration > 1100 && pulseDuration < 1400) { mode = 0; // Position 1 on the switch } else if (pulseDuration > 1500 && pulseDuration < 1800) { mode = 1; // Position 2 on the switch } else if (pulseDuration > 1900 && pulseDuration < 2100) { mode = 2; // Position 3 on the switch } }
- Assistant
There is an error in the code. The issue is with the
onRCInputChangefunction. ThepulseInfunction is blocking, meaning it waits for the pulse duration to complete before returning. This causes the Arduino to freeze and prevents the LED modes from changing.To fix this, you can use a non-blocking approach using
pulseInLongfunction. Here's the updated code:void onRCInputChange() { static unsigned long lastPulseTime = 0; static int lastMode = -1; // Read the pulse duration of the receiver input signal unsigned long pulseDuration = pulseInLong(RC_INPUT_PIN, HIGH, 20000); if (pulseDuration != 0) { if (pulseDuration > 1100 && pulseDuration < 1400) { mode = 0; // Position 1 on the switch } else if (pulseDuration > 1500 && pulseDuration < 1800) { mode = 1; // Position 2 on the switch } else if (pulseDuration > 1900 && pulseDuration < 2100) { mode = 2; // Position 3 on the switch } // Only update the mode if it has changed if (lastMode != mode) { lastMode = mode; // Reset the fire simulation memset(heat, 0, sizeof(heat)); } lastPulseTime = millis(); } // If no pulse has been received for more than 1 second, assume the switch is off if (millis() - lastPulseTime > 1000) { mode = 0; // Position 1 on the switch lastMode = mode; // Reset the fire simulation memset(heat, 0, sizeof(heat)); } } - User
Can you please implement the changes in the script en give me the full script. Thanks
- Assistant
- User
Sorry, it does not work. I get following compilation errors: /Users/frankdebelder/Documents/Arduino/3posschakelaarffutabanoerror/3posschakelaarffutabanoerror.ino: In function 'void onRCInputChange()': /Users/frankdebelder/Documents/Arduino/3posschakelaarffutabanoerror/3posschakelaarffutabanoerror.ino:107:14: error: 'heat' was not declared in this scope memset(heat, 0, sizeof(heat)); ^~~~ /Users/frankdebelder/Documents/Arduino/3posschakelaarffutabanoerror/3posschakelaarffutabanoerror.ino:107:14: note: suggested alternative: 'beat8' memset(heat, 0, sizeof(heat)); ^~~~ beat8 /Users/frankdebelder/Documents/Arduino/3posschakelaarffutabanoerror/3posschakelaarffutabanoerror.ino:119:12: error: 'heat' was not declared in this scope memset(heat, 0, sizeof(heat)); ^~~~ /Users/frankdebelder/Documents/Arduino/3posschakelaarffutabanoerror/3posschakelaarffutabanoerror.ino:119:12: note: suggested alternative: 'beat8' memset(heat, 0, sizeof(heat)); ^~~~ beat8
exit status 1
Compilation error: 'heat' was not declared in this scope
- Assistant