Created Pause Function - Not Executing as Expected

Hi!

Pretty brand new to arduino, but the forums and help sections have been great guidance in creating a solid foundation for me. However, now, I need some help! I tried creating a pause function in my program and it’s running as expected. Basically, I have 2 “run” program buttons: Run1 and Run2 with a Pause button and a Continue button. When I press Run1, the “program” runs - it’s just a simple Serial.println command. However, when the Pause button is pressed, the logic doesn’t stop. The Pause is executed after Run1 has finished. I was hoping someone could take a look at my code and point out where I messed up my logic, etc.

Thanks in advanced!

#include <genieArduino.h>

#define 	BUT_RUN1	0x00
#define 	BUT_RUN2	0x03
#define 	BUT_RUN		0x06
#define 	BUT_PAUSE	0x07
#define 	BUT_CONT	0x08

//#define 	ACTION_NONE		0
//#define 	ACTION_RUN		1
//#define 	ACTION_RUN1		2
//#define 	ACTION_RUN2		3
//#define 	ACTION_PAUSE	        4

int x,z;
int a,b;
int runParam[2];
int previousZ;
int startZ;
int runID = 0;
int action;

void run1(){
  a = runParam[0];
  startZ==previousZ;
  for(int z=startZ; z<=a; z++){
    Serial.println("Run 1: ");
    Serial.println(z);
    delay (3000);

    previousZ==z;
  }
}

void run2(){
  b = runParam[1];
  startZ==previousZ;
  for(int z=startZ; z<=b; z++){
    Serial.println("Run 2: ");
    Serial.println(z);
    delay (3000);

    previousZ==z;
  }
}

// Test for the pressing of the pause button.
// return true if it was pressed, false if not
boolean pausePressed(){
  genieFrame Event;
  genieDoEvents();

  if (genieDequeueEvent(&Event)) {
    if(Event.reportObject.object == GENIE_OBJ_WINBUTTON){
      if (Event.reportObject.index == 0x07) {
        return true;
        Serial.println('True');
      }
    }
  }
  return false;
  Serial.println('False');
}

// Wait for the user to press the continue button
void pause(){
  genieFrame Event;

  while(1) {        //while(1) creates an infinite loop that you can break out of if the statement becomes false
    genieDoEvents();
    if (genieDequeueEvent(&Event)) {
      if(Event.reportObject.object == GENIE_OBJ_WINBUTTON){ 
        if(Event.reportObject.index == 0x08) {
          break;
          //return;
        }
      }
      delay(100); 
    }
  }
}

void myGenieEventHandler() {

  int action = 0; //Case 0 ACTION_NONE

  genieFrame Event;
  genieDequeueEvent(&Event);

  if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)  {
    if(Event.reportObject.index == 0x00){
      runParam[0]=10;
      Serial.println("run1 on");
      action = 2;  //Case 2 ACTION_RUN1
      //runID = 1;
    }
  }

  if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)  {
    if(Event.reportObject.index == 0x03){
      runParam[1]=10;
      Serial.println("run2 on");
      action = 3; //Case 3 ACTION_RUN2
      //runID = 2;
    }
  }

  if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)  {
    if(Event.reportObject.index == 0x06){
      Serial.println("Pressed Run: ");
      action = 1; //Case 1 ACTION_RUN
    }
  }

  if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)  {
    if(Event.reportObject.index == 0x07){
      Serial.println("Button [Pause] Pressed");
      action = 4; //Case 4 ACTION_PAUSE
      pausePressed();
    }
  }

  switch(action) {
  case 1: //ACTION_RUN:

    if (runID == 1) break;
    if (runID & 0) {
      run1();
    }
    if (pausePressed()) {
      // the pause button was pressed while run1() was executing
      pause();
    }
    if (runID & 2){ 
      run2();
    }
    runID = 0;
    break;

  case 2: //ACTION_RUN1:
    runID |= 1;
    break;

  case 3: //ACTION_RUN2:
    runID |= 2;
    break;

  case 4: //ACTION_PAUSE:
    Serial.println ("Pause");
    pause();  //waiting for the continue button to be pressed
    Serial.println ("Continue");
    break;
  }
}
//return action;

void setup(){
  genieBegin(GENIE_SERIAL_1, 9600);
  genieAttachEventHandler(myGenieEventHandler);
  pinMode(4, OUTPUT);
  digitalWrite(4,1);
  delay(100);
  digitalWrite(4,0);
  delay(3500);
  Serial.begin(9600);
}

void loop() {

  genieDoEvents();
  delay(100);
}
startZ==previousZ;

Compare two values, and throw the result away?

What does this do?

if (runID & 0) {
                                run1();
                        }

The layout of your code is very hard to read so debugging it is almost not doable.

Try hitting the CTRL-T before saving/posting code, that helps to see the structure
2) use spaces between operators and operands x=3; should be x = 3; improves readability.

thanks

Thanks for the quick responses.

AWOL: What you had pointed out, I was attempting to do a counter of sorts. So that when when the run got paused, it recorded what "number" it was at and started from there instead of starting at the very beginning.

radman: I was stating the case if the runID was true (not 0), then to execute the run program

robtillaart: Apologies. I was fit it!

So that when when the run got paused, it recorded what "number" it was at and started from there instead of starting at the very beginning.

But to record, you would do an assignment, "=", but you did a comparison, "==".

I was stating the case if the runID was true (not 0), then to execute the run program

What you have written is a bitwise AND which will always result in the 'if' being false.
It is not too obvious why you are using bitwise ANDs and ORs they are not adding clarity.

Bit-wise or logical, the result will be the same.

So after reading around, I think I know what the problem is. Since my logic is step by step, I can't do anything because with the way my method is setup, the logic is executed step by step, so to do my pause, I need the check to be running parallel at the same time. It looks like the solution would be to use Background Worker from as seen in Microsoft Visual Studio. This is where I'm getting a stuck. I tried to apply the commands into the Arduino IDE, but as I expected, it doesn't compile as it doesn't recognize the commands and it needs the library.

Does anyone have any suggestions on how to implement Background Worker in the Arduino IDE platform?

Thanks in advanced!

Multi-threading is not really practical on an Arduino. The way to manage multiple activities concurrently is to implement them with asynchronous (non-blocking) code. The blink without delay example sketch demonstrates how to carry out a simple timed activity asynchronously, and you can extend the technique to do as many things as you want (within the hardware limits of the Arduino).