Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

With this script I want to change between 3 modes of LED illumination. I want to change…

  1. 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.

  2. Assistant

    There are a few corrections that need to be made in your script:

    1. The interrupt function onRCInputChange should have the signature void onRCInputChange(), and not void onRCInputChange(int switchPosition). The switchPosition value is already being determined inside the function using digitalRead(RC_INPUT_PIN), so it does not need to be passed as a parameter.

    2. In the loop function, the call to Fire2012WithPalette() 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.

  3. User

    Cab you review please one time more, the LEDS still do not change.

  4. Assistant

    Apologies for the confusion. Upon reviewing the script again, I realized that there is an issue with the onRCInputChange function. The onRCInputChange function is an interrupt service routine (ISR) and should be kept as short as possible to ensure its responsiveness. Therefore, it is not recommended to use digitalRead inside an ISR.

    To fix this issue, you can use the pulseIn function instead of digitalRead to 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_PIN on the Arduino. Additionally, adjust the pulse duration ranges in the onRCInputChange function to match the specific values for each switch position on your transmitter.