2017-07-18 27 views
1

在我的程序中,我通過藍牙從我在麻省理工應用發明者創建的應用上顯示OLED的時間。在顯示時間串的同時,我正在使用函數從'Sparkfun APDS9660手勢傳感器'搜索'Up'手勢。一旦我做了'向上'手勢,我想清除顯示並顯示字符串「相機」。我希望它在完成任務時保留在「攝像頭」功能中(代碼中),直到我做出一個向下的手勢以返回顯示「時間」功能。重複一個函數直到指定的動作(Aruino IDE)

void handleGesture() { 
    if (apds.isGestureAvailable()) 
    { 
    if(DIR_UP) 
    { 
     Serial.println("UP"); 
     Serial.println("Camera"); 
     display.setTextSize(1); 
     display.setTextColor(WHITE); 
     display.setCursor(0,20); 
     display.println("Camera"); 
     display.display(); 
     Q = 0; 

     while(Q == 0) 
     { 
     if (DIR_RIGHT) 
     { 
      digitalWrite(13, HIGH); 
      delay(1000);    
      digitalWrite(13, LOW); 
      delay(1000); 
     } 

     if (DIR_LEFT) 
     { 
      digitalWrite(12, HIGH); 
      delay(1000);    
      digitalWrite(12, LOW); 
      delay(1000); 
     } 

     if (DIR_DOWN) 
     { 
      break; 
     } 
     } 
    } 
    } 
} 

我想用'while循環'重複代碼,然後'休息'退出代碼。如果有人知道更好的解決方案,請評論。

感謝所有回覆的

+0

另請參閱http://forum.arduino.cc/index.php?topic=490075 – per1234

回答

0

我沒有使用過這種特殊的傳感器,所以我不能不管你是正確的閱讀姿勢發表評論。

我假設你是,handleGesture()充當傳感器引發的中斷的事件處理程序。在處理程序中,比whilebreak更好的解決方案是讓程序處於幾個明確的狀態之一(比如'camera mode'和'time mode')。

手勢處理程序只是在它們之間切換,而實際的邏輯將在loop(或特定於模式的功能,如預期)中進行。

這基本上使你的程序變成一個狀態機。例如:

enum mode { 
    camera, 
    time 
}; 

mode currentMode; 

void loopCamera() { 
    // 'Camera mode' code goes here 
} 

void loopTime() { 
    // 'Time mode' code goes here 
} 

void setup() { 
    // Set initial mode 
    currentMode = time; 

    // Other setup follows... 
} 

void loop() { 
    switch (currentMode) { 
     case camera: 
      loopCamera(); 
      break; 
     case time: 
      loopTime(); 
      break; 
    } 
} 

void handleGesture() { 
    if (apds.isGestureAvailable()) { 
     if (DIR_UP) { 
      // Insert one-time code for switching to camera mode here 
      currentMode = camera; 
     } else if (DIR_DOWN) { 
      // Insert one-time code for switching to time mode here 
      currentMode = time; 
     } 
    } 
} 

這是最好把所有的程序邏輯的處理程序,因爲它清楚地分開不同的功能正在做的事情(的處理程序處理的手勢,交換程序的模式),可以更容易地添加功能(模式等)。

+0

@ Aidan.Thank您對代碼的建議。我寫了它,但它仍然沒有按計劃運作。我猜它與中斷有關。但是圖片和視頻重複閃爍(不用刷卡),時間顯示一秒鐘,然後消失約1秒鐘,然後再次顯示。我仍然在做錯事。 –

+0

我會把代碼放在答案中,因爲它的評論很大。 –

相關問題