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');
}
}