Function 重复一个函数直到指定操作(Aruino IDE)

Function 重复一个函数直到指定操作(Aruino IDE),function,while-loop,arduino,exit,break,Function,While Loop,Arduino,Exit,Break,在我的程序中,我通过蓝牙在OLED上显示时间,这是我在麻省理工学院AppInventor上创建的一个应用程序。当我显示时间字符串时,我使用一个函数从“Sparkfun APDS9660手势传感器”搜索“向上”手势。一旦我做了一个“向上”的手势,我想清除显示并显示字符串“摄像头”。我希望它在完成任务时保持“摄像头”功能(在代码中),直到我做一个向下的手势以返回显示“时间”功能 void handleGesture() { if ( apds.isGestureAvailable() ) {

在我的程序中,我通过蓝牙在OLED上显示时间,这是我在麻省理工学院AppInventor上创建的一个应用程序。当我显示时间字符串时,我使用一个函数从“Sparkfun APDS9660手势传感器”搜索“向上”手势。一旦我做了一个“向上”的手势,我想清除显示并显示字符串“摄像头”。我希望它在完成任务时保持“摄像头”功能(在代码中),直到我做一个向下的手势以返回显示“时间”功能

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循环”重复代码,然后使用“break”退出代码。如果有人知道更好的解决方案,请发表评论


感谢所有回复

我没有使用这个特定的传感器,因此我无法评论您是否正确阅读手势

我假设您是,并且
handleGesture()
充当传感器引发的中断的事件处理程序。在处理程序中,比
while
break
更好的解决方案是让程序处于几个显式状态之一(比如“摄影机模式”和“时间模式”)

手势处理程序只需在它们之间切换,实际逻辑将进入
循环
(或预期的特定于模式的功能)

这基本上使您的程序成为一个状态机。例如:

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

这比将所有程序逻辑放在处理程序中更可取,因为它清楚地区分了不同的功能(处理程序处理手势、交换程序模式),并使将来添加功能(模式等)更容易。

我没有使用这个特定的传感器,所以我无法评论你是否正确解读了这个手势

我假设您是,并且
handleGesture()
充当传感器引发的中断的事件处理程序。在处理程序中,比
while
break
更好的解决方案是让程序处于几个显式状态之一(比如“摄影机模式”和“时间模式”)

手势处理程序只需在它们之间切换,实际逻辑将进入
循环
(或预期的特定于模式的功能)

这基本上使您的程序成为一个状态机。例如:

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

这比将所有程序逻辑放在处理程序中更可取,因为它清楚地区分了不同的功能(处理程序处理手势、交换程序的模式),并使将来添加功能(模式等)更容易。

@Aidan。感谢您对代码的建议。我已经写了,但它仍然没有按计划工作。我猜这和中断有关。但是图片和视频会重复闪烁(不刷卡),时间显示为一瞬间,然后消失约1秒,然后再次显示。我仍然在做一些错误的事情。我会把代码放在一个答案中,因为它的评论太多了。@Aidan。谢谢你对代码的建议。我已经写了,但它仍然没有按计划工作。我猜这和中断有关。但是图片和视频会重复闪烁(不刷卡),时间显示为一瞬间,然后消失约1秒,然后再次显示。我仍然在做一些错误的事情。我会把代码放在一个答案中,因为它太大了,需要评论。也被问到也被问到