2013-05-30 63 views
0

我正在使用紅外遙控器來製作雲燈。我試圖讓其中一個按鈕模擬一個雷電風暴,其中白色LED將在特定代碼下的布爾語句中隨機閃爍......麻煩是,我只能讓它經歷一次閃光期...這是風暴的代碼部分:Arduino雲光燈 - 布爾雷暴

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


} 

如果我繼續增加這些延誤和digitalWrites,我無法改變的「通道」或LED的顏色不同,我堅持,直到事情結束!感謝您提前幫助我!

+0

你能請張貼所有的代碼或解釋每個變量是什麼?你是100%它只是在這個*部分?這可能會改變變量。如果您發佈所有代碼,請嘗試解釋如何推斷您的遙控器並顯示您的電路,以便我們可以爲您提供幫助。沒有一切,我們無法幫助你。 :)還有一件事,當你挑選你的標籤時,將鼠標懸停在它們上面以確保它們是你想要的。當你將鼠標懸停在「閃電」上時,它會說:「關於Mozilla Thunderbird閃電擴展的問題」。 –

+0

等等......你的整個代碼在哪裏? –

+0

對不起,我正在摔跤這個新的「插入代碼正確」的消息。哈哈,我真的很新,很明顯。感謝大量的提示,一切都很有幫助!另外,/ /快速部分是我在工具中修復代碼 –

回答

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); 
    } 
} 
+0

現在你已經發布了完整的代碼,因爲你正在檢查void loop函數中的變化,所以你不需要while循環,但是你應該能夠從我發佈的示例中得到一個想法 –