2015-09-24 22 views
0

我在我的工作場所使用Arduino + NeoPixels爲萬聖節動畫/照明標誌。我的兩個功能之一(緩慢的,擴散的,紅色的發光)可以正常工作。另一種 - 心跳放緩 - 從不減速,似乎陷入了無限循環。功能不適合Arduino使用NeoPixels

下面是完整的,相關代碼:

void loop() { 
    // HeartBeat Test 
    for(int bpm=60;bpm >= 0;bpm=bpm-3) { 
     systolicUp(bpm); 
     systolicDown(bpm); 
     diastolicUp(bpm); 
     diastolicDown(bpm); 
     restBeat(bpm); 
    } 
} 


void systolicUp(int i) { 
    uint16_t beatSeconds, firstPulse, firstIncrement, j, k; 
    beatSeconds = 60000/i; 
    firstPulse = beatSeconds * 0.6; 
    firstIncrement = firstPulse/170; 
    for(j=0; j <= 255; j=j+3) { 
     uint32_t redShade = strip.Color(j, 0, 0); 
     for (k=0; k<strip.numPixels(); k++) { 
      strip.setPixelColor(k, redShade); 
     } 
     strip.show(); 
     delay(firstIncrement); 
    } 
} 

void systolicDown(int i) { 
    uint16_t beatSeconds, firstPulse, firstIncrement, j, k; 
    beatSeconds = 60000/i; 
    firstPulse = beatSeconds * 0.6; 
    firstIncrement = firstPulse/170; 
    for(j=255; j >= 0; j=j-3) { 
     uint32_t redShade = strip.Color(j, 0, 0); 
     for (k=0; k<strip.numPixels(); k++) { 
      strip.setPixelColor(k, redShade); 
     } 
     strip.show(); 
     delay(firstIncrement); 
    } 
} 

void diastolicUp(int i) { 
    uint16_t beatSeconds, secondPulse, secondIncrement, j, k; 
    beatSeconds = 60000/i; 
    secondPulse = beatSeconds * 0.3; 
    secondIncrement = secondPulse/170; 
    for(j=0; j <= 255; j=j+3) { 
     uint32_t redShade = strip.Color(j, 0, 0); 
     for (k=0; k<strip.numPixels(); k++) { 
      strip.setPixelColor(k, redShade); 
     } 
     strip.show(); 
     delay(secondIncrement); 
    } 
} 

void diastolicDown(int i) { 
    beatSeconds = 60000/i; 
    uint16_t beatSeconds, secondPulse, secondIncrement, j, k; 
    secondPulse = beatSeconds * 0.3; 
    secondIncrement = secondPulse/170; 
    for(j=255; j >= 0; j=j-3) { 
     uint32_t redShade = strip.Color(j, 0, 0); 
     for (k=0; k<strip.numPixels(); k++) { 
      strip.setPixelColor(k, redShade); 
     } 
     strip.show(); 
     delay(secondIncrement); 
    } 
} 

void restBeat(int i) { 
    uint16_t beatSeconds, rest, g; 
    beatSeconds = 60000/i; 
    rest = beatSeconds * 0.1; 
    for (g=0; g<strip.numPixels(); g++) { 
     strip.setPixelColor(g, 0, 0, 0); 
    } 
    strip.show(); 
    delay(rest); 
} 

由數學,我應該交出一定數量的心跳每分鐘,秒,每搏數計算,光一次超過60%的脈衝,再次超過30%的脈衝,然後靜音10%。這應該發生20次,每個節拍逐漸變慢,直到完全停止。

相反,我每秒鐘穩定閃爍一次。

我確定它最終會成爲我完全忽略的或者在NeoPixel文檔中遺漏的東西。但是,因爲我要麼俯視,要麼完全錯過了,幫助將不勝感激。 :)

回答

1

void diastolicDown(int i) { beatSeconds = 60000/i; uint16_t beatSeconds, secondPulse, secondIncrement, j, k; 這裏你試圖在聲明前給beatSeconds賦值。

+0

我會暫時測試一下。我認爲這可能是一個格式化SO錯誤,因爲它編譯乾淨,如果我試圖分配一個未聲明的變量不會。 – slink

+0

這是問題的一部分。 *更大*的問題是'uint16_t'用於'beatSeconds'或脈衝,因爲計算的大值會引起問題。我交換到普通的'int'聲明,並立即開始工作。 – slink