Leonardo Keyboard.write can't stop sending repeat characters

Hi there,

I've played around with an arduino uno a few times and just using the leonardo to make use of the Keyboard.write feature to send characters to the mac (that in turn trigger other reactions in other software).

I'm attempting to use a push button with two functions depending on digital pin reading HIGH or LOW.

HIGH = Upon button being pressed will send keyboard write 'A' to the computer.

LOW = release of button (no signal) would send character 'B' to the computer.

The problem is that upon the button being pressed, the arduino continues to send character 'A' repeatedly to the computer which would keep triggering the same script in an external program and causing mayhem - I'm trying to find a way to send a character only once and then wait for the switch to change states (to Low) before sending the opposite character ('B')

Hope that makes sense? Any help you can give would be much appreciated.

I've shopped around a lot for tutorials or code that can be adapted but I'm getting lost quite quickly.
Closest code to the effect I'm looking for is below taken from standard push button sketch, I just can't work out how to end the infinite keypress effect without simply adding a delay which would not be appropriate for the user interaction needed.

I look forward to any responses.


const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Keyboard.write('A');
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Keyboard.write('B');
}
}

Add a variable to track the state of the button. I'll used a boolean variable called pressed. Then add it to the if statement. I'll only send a keystroke if not pressed. When the button is released set pressed back to false.

// add this global variable

boolean pressed = false;

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

// we'll only act on a button press if button is high and pressed is false

  if (!pressed && buttonState == HIGH) 
  {     
    // turn LED on: and set pressed true
    pressed = true;    
    digitalWrite(ledPin, HIGH); 
    Keyboard.write('A'); 
  } 
  else if (pressed && buttonState == LOW) 
  {
    // turn LED off: and reset pressed flag
    pressed = false;
    digitalWrite(ledPin, LOW); 
    Keyboard.write('B');
  }
}

That's amazing thank you for your help! It works really well. I've gone with keyboard.press instead of write too as it allows multiple characters to be used as a signal then keyboard release to clear all. Works very well and looking forward to building it into something more interesting.

:slight_smile: