Can't get out of a while loop

I’m currently working on a project where I have two actuators and a 4D systems touch screen display. I’m using the Arduino Mega 2560. In the touch screen display, I have two buttons, start and stop. When I press the start button, I want the actuators to turn on and alternate and when I press the stop button, I want them to stop. My problem is that when I press the start, they turn on and alternate how I want them to alternate, but when I press the stop button, they don’t stop because it seems like it’s stuck in a loop and I’m not sure how to get out.

#include <genieArduino.h>
#include <SoftwareSerial.h>
#include <Servo.h>

Servo myservo;
Servo myservo1;
const int ledPin = 8;
Genie genie;
SoftwareSerial DisplaySerial(51, 3);
#define RESETLINE 4  // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
int i = 0;
int j = 0;

void setup()

  pinMode(ledPin, OUTPUT);
  genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

  pinMode(RESETLINE, OUTPUT);  // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
  digitalWrite(RESETLINE, 0);  // Reset the Display via D4
  digitalWrite(RESETLINE, 1);  // unReset the Display via D4

  // Let the display start up after the reset (This is important)
  // Increase to 4500 or 5000 if you have sync problems as your project gets larger. Can depent on microSD init speed.
  delay (3500);

  // Set the brightness/Contrast of the Display - (Not needed but illustrates how)
  // Most Displays use 0-15 for Brightness Control, where 0 = Display OFF, though to 15 = Max Brightness ON.
  // Some displays are more basic, 1 (or higher) = Display ON, 0 = Display OFF.
  genie.WriteContrast(15); // About 2/3 Max Brightness

  genie.WriteObject(GENIE_OBJ_FORM, 0, 0);
  delay (3500);
  genie.WriteObject(GENIE_OBJ_FORM, 1, 0);


void loop()

  genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

void myGenieEventHandler(void)

  genieFrame Event;
  genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below
  //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)

  if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)                // If the Reported Message was from Button
    if (Event.reportObject.index == 0)                              // If Button0 / Start (Index = 0) 

      while(Event.reportObject.index == 0)
      digitalWrite(8, HIGH); // turn the LED off by making the voltage LOW

      myservo.writeMicroseconds(1000);     // 1ms to extend the arm
      myservo.writeMicroseconds(2000);     // 2ms to retract the arm
      genie.WriteObject(15, 0, i);

      digitalWrite(8, HIGH); // turn the LED off by making the voltage LOW

      myservo1.writeMicroseconds(1000);     // 1ms to extend the arm
      myservo1.writeMicroseconds(2000);     // 2ms to retract the arm

      genie.WriteObject(15, 1, j);

     if (Event.reportObject.index == 1)  //if Button1/ Stop  (Index 1)


I didn’t look too closely, but the use of delay() will severely affect the responsiveness of the program.

You need to restructure your code to run continuously (non blocking).
This will probably involve millis() timing, and a simple ‘state machine’ to identify when it should be in one mode or another.

This may sound complicated (they’re not so bad), but when you understand these two concepts, all your future programs WILL work better !

You need to put your main attention onto watching the user and what she's doing. Then, when you have some free time, check the genie. Currently you are doing it backwards. This is why you think you need delay() calls. Rewrite it watching the user and you should be able to drop the delays that are killing your touchscreen.

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.