使用 FastLED + 编码器随机化颜色

问题描述 投票:0回答:0

我正在尝试编写一个程序,使用开关在两种状态之间切换 LED 行为。 当开关接合时,我希望 LED 循环显示彩虹色。我用一个锅来控制整体亮度,用一个锅来控制循环速度。 当开关未接合时,我正在尝试使用旋转编码器生成随机颜色。

我遇到的问题是,编码器似乎只在颜色循环时工作,但在颜色停止时不工作。所以它基本上是在彩虹序列中短暂地抛出一种随机颜色,但当它是静态的时候就不会了。

#include <FastLED.h>
#define num_leds 18
#define LED_pin 5
#define FastPin 4
#define SwitchPin 7
#define ENC_PinA 2
#define ENC_PinB 3

CRGB leds[num_leds];
uint8_t hue = 0;
uint8_t sat = 255;
uint8_t dim = 200;
int dim_pot = analogRead(A0);
int delayPot = analogRead(A5);
uint8_t delayT =20 ;

//uint8_t thisTimer = 35;

void setup() {
  FastLED.addLeds<APA102, LED_pin, FastPin, BGR>(leds, num_leds);
  FastLED.setBrightness(150);
  Serial.begin(9600);
  pinMode(SwitchPin, INPUT_PULLUP);
  pinMode(ENC_PinA, INPUT_PULLUP);
  pinMode(ENC_PinB, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(ENC_PinA), randomHSV, CHANGE);  
 
}

void loop() {
  //Check if switch is on or off
  int switchVal = digitalRead(SwitchPin);
  //debounce the switch
   // delay(10);
    //print switch state to serial monitor 
  Serial.println(switchVal);
  //Main loop. If switch on, use rotary encoder to generate random color. 
  //If switch off, use two knobs to control brightness and speed of color fade. 
  if (switchVal == HIGH) {
        //define dim potentiometer and map to 255
        dim = map(dim_pot, 0, 1023, 0, 245);
        dim_pot = analogRead(A0);

        //define delay potentiometer and map 
        
        delayPot = analogRead(A5);
        delayT = map(delayPot, 0, 1023, 15, 60);

        Serial.println(delayT);

        for ( int i =0; i<num_leds; i++) {
          leds[i] = CHSV(hue, sat, 240);
          //delay(delayT);
        }

        FastLED.show();

        FastLED.setBrightness(dim);

        EVERY_N_MILLIS_I (thisTimer, 35) {
          thisTimer.setPeriod(delayT);
          hue++;
        }
    
  }  else {
      Serial.println(switchVal);

    attachInterrupt(digitalPinToInterrupt(ENC_PinA), randomHSV, CHANGE);
    
    FastLED.show();
    
  }
}

  void randomHSV () {
    leds[num_leds] =CHSV(random8(), random8(150, 255), random8(100, 220));
    FastLED.show();
    
  }
arduino encoder fastled
© www.soinside.com 2019 - 2024. All rights reserved.