Stopping leonardo from pressing character multiple times(Only press once)

Hi here is an example section of my code for a button:

if (gatesPOS == LOW){
  Keyboard.press(KEY_RIGHT_ARROW);
  delay (2000);
  Keyboard.releaseAll();}
else{
  Keyboard.press(KEY_LEFT_ARROW);
  delay (2000);
  Keyboard.releaseAll();}

So basically, if the selector switch is in the low pos it will press the right arrow and if it isn't (else) press the left arrow. I coded this a couple years ago for a starter programming project. I am now taking computer science so I have a better understanding of code.

The main problem with this code is it would press the key multiple times per second which isn't ideal, I have just struggled to wipe the board to get this script off as all the buttons were being pressed. Anyway, I was wondering how to get it to only do it once.
I could have a long delay after the button has been released (10secs) as the button most likely won't be pressed in that time interval. However, this is just a temporary solution.

I am thinking a boolean variable but I don't know how you could do it.

Also, I have four buttons on the panel that do not work.
Here is the code relating to them, I've checked the wiring and made sure its all correct and it is.

const int switch1 = 3;
const int switch2 = 4;
const int switch3 = 5;
const int switch4 = 6;

void setup -

  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);[code/]

void loop -
[code]int switch1POS = digitalRead(switch1);
int switch2POS = digitalRead(switch2);
int switch3POS = digitalRead(switch3);
int switch4POS = digitalRead(switch4);

if (switch1POS == HIGH){
  Keyboard.press('e');
  delay(100);
  Keyboard.releaseAll();}

Thanks in advance,
Matty.

Look at the state change detection example that comes with the IDE.

And please post code in a normal way; just copy the complete sketch (properly formatted with the tools->auto format menu item in the IDE) between [code] and [/code] (that you had more or less right :wink: )

sterretje:
Look at the state change detection example that comes with the IDE.

And please post code in a normal way; just copy the complete sketch (properly formatted with the tools->auto format menu item in the IDE) between [code] and [/code] (that you had more or less right :wink: )

I did the code that way because I just took the relevant info from a larger script. I don't get the change state detection, I read the article. Could you do an example which is relevant to the top code.
The way I was thinking was like this:

if (switch1 == LOW){
   Keyboard.write('a')
   switch1VAR = True
   delay(10000)
   switch1VAR = False}

I know this isn't the proper way to do it and I can see the faults but I don't understand the example.
Thanks.

sterretje:
Look at the state change detection example that comes with the IDE.

And please post code in a normal way; just copy the complete sketch (properly formatted with the tools->auto format menu item in the IDE) between [code] and [/code] (that you had more or less right :wink: )

int gatesPOS = digitalRead(gates);


//Gates
if (gatesPOS != gatesstate){
  Keyboard.write(KEY_RIGHT_ARROW);
  gatestate == gatesPOS;}

Is this how I would do it?

static boolean prevGatesPOS = LOW;
if (gatesPOS != prevGatesPOS) {
  prevGatesPOS = gatesPOS; 
  Keyboard.press(gatesPOS ? KEY_LEFT_ARROW : KEY_RIGHT_ARROW);
  delay (2000);
  Keyboard.releaseAll();}
}

johnwasser:

static boolean prevGatesPOS = LOW;

if (gatesPOS != prevGatesPOS) {
  prevGatesPOS = gatesPOS;
  Keyboard.press(gatesPOS ? KEY_LEFT_ARROW : KEY_RIGHT_ARROW);
  delay (2000);
  Keyboard.releaseAll();}
}

Would this alternate between the left and right arrow?

mxttymitch:
Would this alternate between the left and right arrow?

Yes, when the switch position changes. It will act like you pressed the key for two seconds. You OS may do auto-repeat when you hold the button that long.

johnwasser:
Yes, when the switch position changes. It will act like you pressed the key for two seconds. You OS may do auto-repeat when you hold the button that long.

Thanks for the help!

Here’s the code I’ve done… Lets hope this works. Will the Keyboard.begin() and Keyboard.end() work to stop any keyboard inputs being pressed(The power button)?

#include <Keyboard.h>
// ====================================================================

//Set Pin Values for buttons
const int power = 1;
const int reset = 2;
const int switch1 = 3;
const int switch2 = 4;
const int switch3 = 5;
const int switch4 = 6;
const int sstop = 7;
const int preshow = 8;
const int estop = 9;
const int gates = 10;
const int restraintsOPEN = 11;
const int restraintsCLOSED = 12;
const int tilt = 13;
const int selector = A0;
const int launch = A1;
const int dispatch = A2;

void setup(){
  // Pin modes
  pinMode(power, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
  pinMode(switch1, INPUT_PULLUP);
  pinMode(switch2, INPUT_PULLUP);
  pinMode(switch3, INPUT_PULLUP);
  pinMode(switch4, INPUT_PULLUP);
  pinMode(sstop, INPUT_PULLUP);
  pinMode(preshow, INPUT_PULLUP);
  pinMode(estop, INPUT_PULLUP);
  pinMode(gates, INPUT_PULLUP);
  pinMode(restraintsOPEN, INPUT_PULLUP);
  pinMode(restraintsCLOSED, INPUT_PULLUP);
  pinMode(tilt, INPUT_PULLUP);
  pinMode(selector, INPUT_PULLUP);
  pinMode(launch, INPUT_PULLUP);
  pinMode(dispatch, INPUT_PULLUP);}
 
void loop() {
// Set variables for button states

int powerPOS = digitalRead(power);
int resetPOS = digitalRead(reset);
int switch1POS = digitalRead(switch1);
int switch2POS = digitalRead(switch2);
int switch3POS = digitalRead(switch3);
int switch4POS = digitalRead(switch4);
int sstopPOS = digitalRead(sstop);
int preshowPOS = digitalRead(preshow);
int estopPOS = digitalRead(estop);
int gatesPOS = digitalRead(gates);
int tiltPOS = digitalRead(tilt);
int selectorPOS = digitalRead(selector);
int launchPOS = digitalRead(launch);
int dispatchPOS = digitalRead(dispatch);
int prevestoppos = LOW;
int prevgatestate = HIGH;

// |------------------------------------------------------------------------------|
// |===============================Station Controls===============================|
// |------------------------------------------------------------------------------|

//Power (stops keyboard working when down)
if (powerPOS == LOW){
    Keyboard.begin();}
if (powerPOS == HIGH){
    Keyboard.end();}

 //Reset
 if (estopPOS == HIGH and resetPOS == LOW){
  Keyboard.press('r');
  delay(500);
  Keyboard.releaseAll();}

//Station Stop
if (sstopPOS == LOW){
  Keyboard.press('s');
  delay(500);
  Keyboard.releaseAll();}

//Emergency Stop
if (estopPOS != prevestoppos){
  Keyboard.press('e');
  delay(500);
  Keyboard.releaseAll();
  estopPOS = prevestoppos;}
//Grey switch 1
if (switch1POS == LOW){
  Keyboard.press('+');
  delay(500);
  Keyboard.releaseAll();}

//Grey Switch 2
if (switch2POS == LOW){
  Keyboard.press('1');
  Keyboard.press('v');
  delay(500);
  Keyboard.releaseAll();}

//Grey switch 3
if (switch3POS == LOW){
  Keyboard.press('-');
  delay(500);
  Keyboard.releaseAll();}

//Grey switch 4
if (switch4POS == LOW){
  Keyboard.press('2');
  Keyboard.press('z');
  delay(500);
  Keyboard.releaseAll();}

//Gates
if (gatesPOS != prevgatestate){
  Keyboard.press(gatesPOS ? KEY_LEFT_ARROW : KEY_RIGHT_ARROW);
  delay(500);
  Keyboard.releaseAll();
  prevgatestate = gatesPOS;} 

//Restraints
if (digitalRead(restraintsOPEN) == LOW){
  Keyboard.press(KEY_UP_ARROW);
  delay(1000);
  Keyboard.releaseAll();}
  
if (digitalRead(restraintsCLOSED) == LOW){
  Keyboard.press(KEY_DOWN_ARROW);
  delay(1000);
  Keyboard.releaseAll();}
  
// Tilt Button
if (tiltPOS == LOW){ // If tilt is pressed
  Keyboard.press(KEY_RETURN); // Press enter for 4 seconds
  delay (4000); 
  Keyboard.releaseAll();}

//2nd 2 pos switch/selector switch

//Launch Button
if (launchPOS == LOW){ // If launch is pressed
  Keyboard.press('l'); // Press L
  delay (500);
  Keyboard.releaseAll();}

// Dispatch Button
if (dispatchPOS == LOW){ // If dispatch is pressed
  Keyboard.press(' '); // Press spacebar
  delay (1000);
  Keyboard.releaseAll();}



//Preshow doors/start
if (preshowPOS == LOW){ // If preshow is pressed
  Keyboard.press(KEY_RETURN); // press return
  delay(1000); 
  Keyboard.releaseAll();}

}

Will the Keyboard.begin() and Keyboard.end() work to stop any keyboard inputs being pressed(The power button)?

Let's take a look at what they do. What do you think? :wink:

void Keyboard_::begin(void)
{
}

void Keyboard_::end(void)
{
}

oqibidipo:
Let's take a look at what they do. What do you think? :wink:

void Keyboard_::begin(void)

{
}

void Keyboard_::end(void)
{
}

I have no idea. I have just tested it... Gates doesn't work, s-stop doesn't and neither does the power button. Can you help?

The gates only goes one way...

mxttymitch:
The gates only goes one way...

That is because you left out the 'static' keyword:

int prevestoppos = LOW;
int prevgatestate = HIGH;

That causes the variable to be created fresh each time through loop(). It will detect a change to LOW and will repeat every time through loop().