Repeat a Function until specified Action (Aruino IDE)

In my program I am displaying time on an OLED via bluetooth from an app I’ve created on MIT app inventor. While I’m displaying the strings of time I am using a function to search for an ‘Up’ Gesture from the ‘Sparkfun APDS9660 Gesture Sensor’. Once I do an ‘Up’ gesture I would like to clear the display and show the string “camera”. I would like it to stay in the ‘camera’ function (in code) while completing the tasks, until I do a down gesture to return to showing ‘time’ function.
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;
    }
    }
 
   
 
  }
 }
}

I’m trying to use a ‘while loop’ to repeat the code and then a ‘break’ to exit the code. If anyone knows any better solutions please comment.

Thanks for All Reply’s

Why not use a Switch statement in your handler?

void handleGesture()
{
   if ( apds.isGestureAvailable() )
   {
    switch ( apds.readGesture() )
    {
     case DIR_UP:
       DoPanUp();
       break;
     case DIR_DOWN:
       DoPanDown();
       break;
     case DIR_LEFT:
      DoPaneLeft();
       break;
     case DIR_RIGHT:
       DoPanRight();
       break;
    }
   }
 }

I wouldn’t use a while loop inside your handler… that ties up your microprocessor inside that function and becomes unresponsive to anything else.

A better solution is using interrupts to handle events from your sensor, and just rely on the built in loop() of the program.

It continually loops, when it detects an interrupt from the sensor, process the action, and exit immediately… going back to the main loop().

Now if your want the LEFT or RIGHT button do something different, depending on what “menu” or “mode” the user is in, then look at Finite State Machine programming, i.e. keep track of what state you’re in and process the LEFT or RIGHT events depending on what State you’re in. You can even move to a new State depending on the action, and quickly exit the loop. On the next loop iteration, you’re in the new State and the LEFT/RIGHT buttons/events can function as something else.

Anyways, this is a forum for web development, I think you’ll get more help from the Arduino forum. Good luck!

1 Like
while(!DIR_DOWN) {
  int id;
  if(DIR_RIGHT)
    id=13;
  else if(DIR_LEFT)
    id=12;
  digitalWrite(id, HIGH);
  delay(1000);
  digitalWrite(id, LOW);
  delay(1000);
}