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. Signal from the Futaba receiver goes to pin2 of the aruino. 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. Er there some error in the script? // Certainly! Here is the modified script with the updated onRCInputChange function:
#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);
pinMode(RC_INPUT_PIN, INPUT); // Configure RC input pin 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() { // No need to check button state anymore
// Update palette based on mode switch (mode) { case 0: gPal = CRGBPalette16(CRGB::Red, CRGB::Red, CRGB::Yellow, CRGB::White); break; case 1: gPal = CRGBPalette16(CRGB::Aqua, CRGB::Blue, CRGB::Aqua, CRGB::White); break; case 2: gPal = CRGBPalette16(CRGB::Green, CRGB::Green, CRGB::Yellow); break; }
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame FastLED.delay(500 / FRAMES_PER_SECOND); }
#define COOLING 55 #define SPARKING 120
//********************************************************************************************// // PLAY WITH FALLING, COOLING AND SPARKLING
void Fire2012WithPalette() { // Array of temperature readings at each simulation cell static uint8_t heat[NUM_LEDS];
// Step 1. Cool down every cell a little for (int i = 0; i < NUM_LEDS; i++) { heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2)); }
// Step 2. Heat from each cell drifts ‘up’ and diffuses a little for(int k = NUM_LEDS - 1; k >= 2; k--) { heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3; }
// Step 3. Randomly ignite new ‘sparks’ of heat near the bottom if (random8() < SPARKING) { int y = random8(7); heat[y] = qadd8(heat[y], random8(160, 255)); }
// Step 4. Map from heat cells to LED colors for (int j = 0; j < NUM_LEDS; j++) { // Scale the heat value from 0-255 down to 0-240 // for best results with color palettes. uint8_t colorindex = scale8( heat[j], 240); // Corrected line CRGB color = ColorFromPalette( gPal, colorindex); int pixelnumber; if( gReverseDirection ) { pixelnumber = (NUM_LEDS-1) - j; } else { pixelnumber = j; } leds[pixelnumber] = color; } // Your fire animation code here }
void onRCInputChange() { // Read the RC input to determine the mode int switchPosition = digitalRead(RC_INPUT_PIN);
if (switchPosition == LOW) { mode = 0; // Position 1 on the switch } else if (switchPosition == HIGH) { mode = 2; // Position 3 on the switch } else { mode = 1; // Position 2 on the switch } }
// Please note that you might need to adjust the RC_INPUT_PIN value to match the pin you are using for the switch input on your Arduino.
- Assistant
There are a few corrections that need to be made in your script:
-
The interrupt function
onRCInputChangeshould have the signaturevoid onRCInputChange(), and notvoid onRCInputChange(int switchPosition). TheswitchPositionvalue is already being determined inside the function usingdigitalRead(RC_INPUT_PIN), so it does not need to be passed as a parameter. -
In the
loopfunction, the call toFire2012WithPalette()should be inside the switch block, after setting the palette based on the mode. This ensures that the fire simulation is updated based on the selected mode.
Here is the corrected script:
#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); pinMode(RC_INPUT_PIN, INPUT); // Configure RC input pin 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 //********************************************************************************************// // PLAY WITH FALLING, COOLING AND SPARKLING void Fire2012WithPalette() { // Array of temperature readings at each simulation cell static uint8_t heat[NUM_LEDS]; // Step 1. Cool down every cell a little for (int i = 0; i < NUM_LEDS; i++) { heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2)); } // Step 2. Heat from each cell drifts ‘up’ and diffuses a little for(int k = NUM_LEDS - 1; k >= 2; k--) { heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3; } // Step 3. Randomly ignite new ‘sparks’ of heat near the bottom if (random8() < SPARKING) { int y = random8(7); heat[y] = qadd8(heat[y], random8(160, 255)); } // Step 4. Map from heat cells to LED colors for (int j = 0; j < NUM_LEDS; j++) { // Scale the heat value from 0-255 down to 0-240 // for best results with color palettes. uint8_t colorindex = scale8( heat[j], 240); // Corrected line CRGB color = ColorFromPalette( gPal, colorindex); int pixelnumber; if( gReverseDirection ) { pixelnumber = (NUM_LEDS-1) - j; } else { pixelnumber = j; } leds[pixelnumber] = color; } // Your fire animation code here } void onRCInputChange() { // Read the RC input to determine the mode int switchPosition = digitalRead(RC_INPUT_PIN); if (switchPosition == LOW) { mode = 0; // Position 1 on the switch } else if (switchPosition == HIGH) { mode = 2; // Position 3 on the switch } else { mode = 1; // Position 2 on the switch } }Make sure to select the correct pin for the RC input on the Arduino and connect it to the correct pin on the Futaba receiver.
-
- User
Cab you review please one time more, the LEDS still do not change.
- Assistant
Apologies for the confusion. Upon reviewing the script again, I realized that there is an issue with the
onRCInputChangefunction. TheonRCInputChangefunction is an interrupt service routine (ISR) and should be kept as short as possible to ensure its responsiveness. Therefore, it is not recommended to usedigitalReadinside an ISR.To fix this issue, you can use the
pulseInfunction instead ofdigitalReadto read the pulse duration of the receiver input signal. Here's the updated script:#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 } }Make sure to connect the signal wire from the receiver to
RC_INPUT_PINon the Arduino. Additionally, adjust the pulse duration ranges in theonRCInputChangefunction to match the specific values for each switch position on your transmitter.