Stop script by pressing same button again

Status
Not open for further replies.

Bishamon1987

New member
Hello there,
Im very new to the Teensy-verse but I like the endless possibilities!
I am writing a script which takes a lot of work away from my colleagues.

At the moment I got this code:
Code:
#include <Key.h>
#include <Mouse.h>
#include <Bounce.h>

const String text;

// Creates Bounce objects for each button. 
// A bounce object automatically deals with 
// contact chatter or "bounce", which makes 
// detecting changes very simple.
Bounce MSGroen = Bounce(1, 10);
Bounce MSBlauw = Bounce(3, 10);
Bounce MSGeel = Bounce(5, 10);
Bounce MSRood = Bounce(7, 10);
Bounce MSNS1 = Bounce(9, 10);
Bounce MSNS2 = Bounce(11, 10);

String msg;

// MAIN PROGRAM
void setup() 
{
  Serial.begin(9600);
  Mouse.begin();
  Mouse.screenSize(1920,1080);
  
  pinMode(1, INPUT_PULLUP);
  pinMode(2, OUTPUT);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, OUTPUT);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, OUTPUT);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, OUTPUT);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, OUTPUT);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  
}

void loop()
{       
  MSGroen.update();
  MSBlauw.update();
  MSGeel.update();
  MSRood.update();
  MSNS1.update();
  MSNS2.update();
  
  if (MSGroen.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING GROEN - PRESSED");
      Fullscreen(); //Sets the window on fullscreen (so I can use the mouse coordinates standardised)
      delay(250);
      NieuwContactMedischeScreening(); //Does some actions in the program we use
      delay(250);
      ORegel(); //Does some actions in the program we use
      delay(250);
      ERegel(); //Does some actions in the program we use
      delay(250);
      MedischeScreeningEpisode("Medische Screening"); //Does some actions in the program we use
      delay(250);
      PRegelExp(); //Does some actions in the program we use
      VerrichtingMI(); //Does some actions in the program we use
      delay(250);        
      LabelsUit(); //Does some actions in the program we use
      delay(250); 
      AkkoordAan(); //Does some actions in the program we use
  }
  if (MSBlauw.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING BLAUW - PRESSED");
      Fullscreen();
      delay(250);
      NieuwContactMedischeScreening();
      delay(250);
      ORegel();
      delay(250);
      ERegel();
      delay(250);
      MedischeScreeningEpisode("Medische Screening");
      delay(250);
      PRegelTriage();
      //PRegelExp();
      //AttentieRegelNietAkkoord();
      delay(250);
      VerrichtingMI();
      delay(250);
      //VerrichtingNS1();
      //VerrichtingNS2();              
      LabelsUit();
      delay(250);
      //NietAkkoordAan();
      AkkoordAan();
  }
  if (MSGeel.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING GEEL - PRESSED");
      Fullscreen();
      delay(250);
      NieuwContactMedischeScreening();
      delay(250);
      ORegel();
      delay(250);
      ERegel();
      delay(250);
      MedischeScreeningEpisode("Medische Screening");
      delay(250);
      //PRegelTriage();              
      PRegelExp();
      delay(250);
      AttentieRegelNietAkkoord();
      delay(250);
      VerrichtingMI();
      delay(250);
      //VerrichtingNS1();
      //VerrichtingNS2();              
      LabelsUit();
      delay(250);
      NietAkkoordAan();
      //AkkoordAan();
  }
  if (MSRood.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING ROOD - PRESSED");
        Fullscreen();
        delay(250);
        NieuwContactMedischeScreening();
        delay(250);
        ORegel();
        delay(250);
        ERegel();
        delay(250);
        MedischeScreeningEpisode("Medische Screening");
        delay(250);
        PRegelTriage();
        delay(250);
        //PRegelExp();
        AttentieRegelNietAkkoord();
        delay(250);
        VerrichtingMI();
        delay(250);
        //VerrichtingNS1();
        //VerrichtingNS2();              
        LabelsUit();
        delay(250);
        NietAkkoordAan();
        //AkkoordAan();
  }
  if (MSNS1.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING NO SHOW 1 - PRESSED");
        Fullscreen();
        delay(250);
        NieuwContactMedischeScreening();
        delay(250);
        ORegelNS();
        delay(250);
        MouseMovement(700, 70); //moveTo(900, 101)
        Mouse.click();
        delay(50);
        MedischeScreeningEpisode("Medische Screening - No Show 1");
        delay(250);
        PRegelExp();
        delay(250);
        VerrichtingNS1();
        delay(250);
  }
  if (MSNS2.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING NO SHOW 2 - PRESSED");
        Fullscreen();
        delay(250);
        NieuwContactMedischeScreening();
        delay(250);
        ORegelNS();
        delay(250);
        MouseMovement(700, 70); //moveTo(900, 101)
        Mouse.click();
        delay(50);
        MedischeScreeningEpisode("Medische Screening - No Show 2");
        delay(250);
        PRegelPasVerstuurd();
        delay(250);
        VerrichtingNS2();
        delay(250);
        EtiketPrinten();
  } 
}

Each script takes about 5-10sec to complete.
Now I want a "safety feature" which allows the user to stop the script from running when pressed by accident.
So is it possible to use the same button again to stop the script immediately?

Thanks in advance!
 
Yes you can use the same button... Although your current organization of the code may make it a little more difficult...
That is you have section like:
Code:
 if (MSGroen.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING GROEN - PRESSED");
      Fullscreen(); //Sets the window on fullscreen (so I can use the mouse coordinates standardised)
      delay(250);
      NieuwContactMedischeScreening(); //Does some actions in the program we use
      delay(250);
      ORegel(); //Does some actions in the program we use
      delay(250);
      ERegel(); //Does some actions in the program we use
      delay(250);
      MedischeScreeningEpisode("Medische Screening"); //Does some actions in the program we use
      delay(250);
      PRegelExp(); //Does some actions in the program we use
      VerrichtingMI(); //Does some actions in the program we use
      delay(250);        
      LabelsUit(); //Does some actions in the program we use
      delay(250); 
      AkkoordAan(); //Does some actions in the program we use
  }

So you are not ever updating the buttons nor checking state... There are many ways to do this, like setup a sort of state machine and check it all from the top...

But simpler way would be something like:
Code:
bool check_buttons_and_delay(uint32_t delay_time, Bounce &abort_button)
{
  elapsedMicros em = 0
  while (em < delay_time) {
    MSGroen.update();
    MSBlauw.update();
    MSGeel.update();
    MSRood.update();
    MSNS1.update();
    MSNS2.update();
    if (abort_button.fallingEdge) return false;
  }
  return true;
}
...
if (MSGroen.fallingEdge()) {
      Serial.println("MEDISCHE SCREENING GROEN - PRESSED");
      Fullscreen(); //Sets the window on fullscreen (so I can use the mouse coordinates standardised)
      bool continue_processing = check_buttons_and_delay(250, MSGroen);
      if (continue_processing)
        NieuwContactMedischeScreening(); //Does some actions in the program we use
        continue_processing = check_buttons_and_delay(250, MSGroen);
      }
      if (continue_processing)
        ORegel(); //Does some actions in the program we use
        continue_processing = check_buttons_and_delay(250, MSGroen);
      }
      if (continue_processing)
        ERegel(); //Does some actions in the program we use
        continue_processing = check_buttons_and_delay(250, MSGroen);
      }
      if (continue_processing)
        MedischeScreeningEpisode("Medische Screening"); //Does some actions in the program we use
        continue_processing = check_buttons_and_delay(250, MSGroen);
      }
      ... 
     if (!cononue_processing) {
        // do anything you need to cleanup
      }
  }

Or you could so something even simpler with gotos instead of all the if stuff...
 
Status
Not open for further replies.
Back
Top