0

我正在使用 arduino 用红外遥控器制作云灯。我正在尝试使其中一个按钮模拟雷暴,其中白色 LED 将在特定代码下的布尔语句内随机闪烁...问题是,我只能让它经历一次闪光期...这是风暴的部分代码:

#include <IRremote.h>
// My modified code
int RECV_PIN = 11;
int red = 2;
int yellow = 3;
int green = 4;
int blue = 5;
int purple = 6;
int white = 7;
long randOn = random(10,200);
long guess = random(1000,20000);

#define powercode 16726725
#define stormcode 4294967295                   




IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
      pinMode(green, OUTPUT);
        pinMode(blue, OUTPUT);
          pinMode(purple, OUTPUT);
            pinMode(white, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}


int redbool = 0;
int orangebool = 0;
int yellowbool = 0;
int greenbool = 0;
int bluebool = 0;
int purplebool = 0;
int whitebool = 0;
int rgbbool = 0;
int rainbowbool = 0;
int sunbool = 0;
int rainbool = 0;
int stormbool = 0;
int quickbool = 0;

unsigned long last = millis();

void loop() {
  if (irrecv.decode(&results)) {


//Sun

    if (results.value == 16726725) { 
      if (millis() - last > 250) {
        sunbool = !sunbool;
        if (sunbool == 0) {
          digitalWrite (red, LOW);
          digitalWrite (yellow, LOW);
          digitalWrite (green, LOW);
          digitalWrite (blue, LOW);
          digitalWrite (purple, LOW);
          digitalWrite (white, LOW);
        }
      else { 
          digitalWrite (red, LOW);
          digitalWrite (green, LOW);
          digitalWrite (blue, LOW);
          digitalWrite (purple, LOW);

          digitalWrite (white, HIGH);
          digitalWrite (yellow, HIGH);
          }
      }
      last = millis();
    }

 //rain

    if (results.value == 16745085) { 
      if (millis() - last > 250) {
        rainbool = !rainbool;
        if (rainbool == 0) {
          digitalWrite (red, LOW);
          digitalWrite (yellow, LOW);
          digitalWrite (green, LOW);
          digitalWrite (blue, LOW);
          digitalWrite (purple, LOW);
          digitalWrite (white, LOW);
        }
      else { 
          digitalWrite (red, LOW);
          digitalWrite (green, LOW);
          digitalWrite (yellow, LOW);
          digitalWrite (purple, LOW);

          digitalWrite (white, HIGH);
          digitalWrite (blue, HIGH);
          }
      }
      last = millis();
    }   
//storm

    if (results.value == 16759365) { 
      if (millis() - last > 250) {
        stormbool = !stormbool;
        if (stormbool == 0) {
          digitalWrite (red, LOW);
          digitalWrite (yellow, LOW);
          digitalWrite (green, LOW);
          digitalWrite (blue, HIGH);
          digitalWrite (purple, HIGH);
          digitalWrite (white, LOW);
        }
      else { 
          digitalWrite (red, LOW);
          digitalWrite (green, LOW);
          digitalWrite (yellow, LOW);
          digitalWrite (white, LOW);

          digitalWrite (blue, HIGH);
          digitalWrite (purple, HIGH);

   digitalWrite(white, HIGH);  
   delay(randOn);  
   digitalWrite(white, LOW);   
   delay(randOn + randOn);  
   digitalWrite(white, HIGH);  
   delay(randOn);    
   digitalWrite(white, LOW); 
          }
      }
      last = millis();
    }
 //rainbow
        if (results.value == 16712445) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        rainbowbool = !rainbowbool;
        digitalWrite(white, LOW);
        digitalWrite(red, rainbowbool ? HIGH : LOW);
                digitalWrite(yellow, rainbowbool ? HIGH : LOW);
                        digitalWrite(green, rainbowbool ? HIGH : LOW);
                                digitalWrite(blue, rainbowbool ? HIGH : LOW);
                                        digitalWrite(purple, rainbowbool ? HIGH : LOW);


      }
      last = millis();
    }   

 //red
        if (results.value == 16718565) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        redbool = !redbool;
        digitalWrite(red, redbool ? HIGH : LOW);
      }
      last = millis();
    }
//orange
    if (results.value == 16726215) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        orangebool = !orangebool;
        digitalWrite(red, orangebool ? HIGH : LOW);
        digitalWrite(yellow, orangebool ? HIGH : LOW);
      }
      last = millis();
    }
//yellow
    if (results.value == 16718055) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        yellowbool = !yellowbool;
        digitalWrite(yellow, yellowbool ? HIGH : LOW);
      }
      last = millis();
    }
//green
    if (results.value == 16751205) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        greenbool = !greenbool;
        digitalWrite(green, greenbool ? HIGH : LOW);
      }
      last = millis();
    }
//blue
    if (results.value == 16753245) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        bluebool = !bluebool;
        digitalWrite(blue, bluebool ? HIGH : LOW);
      }
      last = millis();
    }
//purple
    if (results.value == 16757325) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        purplebool = !purplebool;
        digitalWrite(purple, purplebool ? HIGH : LOW);
      }
      last = millis();
    }
//white
    if (results.value == 16720605) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
        whitebool = !whitebool;
        digitalWrite(white, whitebool ? HIGH : LOW);
      }
      last = millis();
            }
        //Quick section
        if (results.value == 16771095) { // TIVO button
      // If it's been at least 1/4 second since the last
      // IR received, toggle the relay
      if (millis() - last > 250) {
          quickbool =! quickbool;

            digitalWrite(white, HIGH);  
           delay(guess);  
           digitalWrite(white, LOW);   
           delay(guess);  
           digitalWrite(white, HIGH);  
           delay(guess + guess);    
           digitalWrite(white, LOW); 

      }

      last = millis();
    }
    irrecv.resume(); // Receive the next value
  }


}

如果我继续添加这些延迟和数字写入,我无法将“通道”或 LED 更改为不同的颜色,我会卡住直到事情完成!感谢您提前帮助我!

4

1 回答 1

0

您是正确的,使用延迟功能时不会发生其他操作。您应该尝试使您的代码类似于blinkWithoutDelay示例。所以你可以做这样的事情

// struct that represents the values for each led
struct LED{
  int pin; // pin # the led is connected to
  int state; // state of the led , on or off
  long interval; // how long we should wait before toggling
  long timeToggled; // last time we toggled
};

if(results.value == 16759365){
  digitalWrite(0,HIGH);
  digitalWrite(1,HIGH);
  digitalWrite(2,HIGH); 

  LED white = {0,HIGH,100,millis()}; 
  LED yellow = {1,HIGH,10,millis()};
  LED blue = {2,HIGH,420,millis()};

  results = readIr(); // check the ir value again and keep looping until it changes
  while(results.value == 16759365){
     toggleLed(white);
     toggleLed(yellow);
     toggleLed(blue);
     results = readIr(); // if ir value hasnt changed keep looping until it does
  }

  digitalWrite(0,LOW);
  digitalWrite(1,LOW);
  digitalWrite(2,LOW);
}



void toggleLed(struct LED currentLed){
  unsigned long currentMillis = millis();

  if(currentMillis - currentLed.timeToggled > currentLed.interval) {
    // save the last time you blinked the LED 
    currentLed.timeToggled = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (currentLed.state == LOW)
      currentLed.state = HIGH;
    else
      currentLed.state = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(currentLed.pin, currentLed.state);
  }
}
于 2013-05-30T01:24:18.840 回答