0

arduino を使用して、IR リモートで Cloud Light を作成しています。ボタンの 1 つが雷雨をシミュレートするようにしようとしています。この場合、特定のコードの下のブールステートメント内で白色 LED がランダムに点滅します...問題は、フラッシュ期間を 1 回しか通過できないことです...これが嵐のコードの一部:

#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
  }


}

これらの遅延と digitalWrite を追加し続けると、「チャンネル」や 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 に答える