2016-12-07 3 views
0
switch(mode) { 
    case 0: 
     if (millis() - timer >= 100) { 
     file.play(); 
     timer = millis(); 
     } 

     break; 
    case 1: 
     if (millis() - timer >= 1000) { 
     file1.play(); 
     timer = millis(); 
     } 
     break; 
    } 

トラフィックライトのような時間ベースのプログラムを作成します。 サウンドが再生され、最後の3秒間再生されます。 3秒後に別の音に変わり、最後の10秒です.10秒後に、最初の音に変わります。等々。 2件の時間をどのように数えることができますか?何かは時間ベースに依存します

答えて

0

millis()timerを論理的に見比べるために使用する条件は次のとおりです。

  • それは音と最後に再生されます:あなたは、単に3つの状態を必要とするそれはのように感じている3秒と10秒(の代わりに、0.1〜1秒)

    する値を変更する場合があります3秒。 3秒後

  • 、それは別の音に変わり、最後の10秒10秒後

  • だろう、それは最初の音に変わります。

このような何か:タイミング的には

int mode; 

int timer; 

void setup() { 
    println("starting in 1st mode, seconds:",second()); 
} 
void draw() { 


    switch(mode) { 
    case 0: 
    if (millis() - timer >= 3000) { 
     println("play sound 1, seconds:",second()); 
     timer = millis(); 
     mode = 1;//change to the state to the next one 
    } 

    break; 
    case 1: 
    if (millis() - timer >= 3000) { 
     print("play sound 2, seconds:",second()); 
     timer = millis(); 
     mode = 2;//go to next mode 
    } 
    break; 
    case 2: 
    if (millis() - timer >= 10000) {//wait 10s 
     println("\n10s passed, going to 1st mode, seconds:",second()); 
     timer = millis(); 
     mode = 0;//go to next mode, after 10s 
    } 
    break; 
    } 
} 

、あなたは3S、3Sおよび10S単位でループ状態を持っている必要があります。

サウンドが期待どおりに再生されるのであれば100%ではありませんが、最初にどのような状態が発生しているかを確認してから、適切な場所に必要なサウンドをトリガーするだけです。

デモ時間:

var mode = 0; 
 

 
var timer; 
 

 
function setup() { 
 
    createCanvas(100,100); 
 
    timer = millis() 
 
    console.log("starting in 1st mode, seconds:",second()); 
 
} 
 
function draw() { 
 
    background(255); 
 
    text("mode: " + mode + "\nseconds: " + second(),10,10); 
 

 
    switch(mode) { 
 
    case 0: 
 
    if (millis() - timer >= 3000) { 
 
     console.log("play sound 1, seconds:",second()); 
 
     timer = millis(); 
 
     mode = 1;//change to the state to the next one 
 
    } 
 

 
    break; 
 
    case 1: 
 
    if (millis() - timer >= 3000) { 
 
     console.log("play sound 2, seconds:",second()); 
 
     timer = millis(); 
 
     mode = 2;//go to next mode 
 
    } 
 
    break; 
 
    case 2: 
 
    if (millis() - timer >= 10000) {//wait 10s 
 
     console.log("10s passed, going to 1st mode, seconds:",second()); 
 
     timer = millis(); 
 
     mode = 0;//go to next mode, after 10s 
 
    } 
 
    break; 
 
    } 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/p5.min.js"></script>

関連する問題