int button1 = 2; // Set a button to any pin
int button2 = 3; // Set a button to any pin
int button3 = 4; // Set a button to any pin
void setup()
{
pinMode(button1, INPUT); // Set the button as an input
digitalWrite(button1, HIGH); // Pull the button high
pinMode(button2, INPUT); // Set the button as an input
digitalWrite(button2, HIGH); // Pull the button high
pinMode(button3, INPUT); // Set the button as an input
digitalWrite(button3, HIGH); // Pull the button high
}
void loop()
{
if (digitalRead(button1) == 0) // if the button goes low
{
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('k');
delay(1000); // delay so there aren't a kajillion z's
Keyboard.release(KEY_LEFT_CTRL);
}
{
if (digitalRead(button2) == 0) // if the button goes low
{
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('o');
delay(1000); // delay so there aren't a kajillion z's
Keyboard.release(KEY_LEFT_CTRL);
}
{
if (digitalRead(button3) == 0) // if the button goes low
{
Keyboard.print('space');
delay(1000); // delay so there aren't a kajillion z's
}
}
}
To help people read your code , you should put it inside code tags like this
Its the button with a # on it
int button1 = 2; // Set a button to any pin
int button2 = 3; // Set a button to any pin
int button3 = 4; // Set a button to any pin
void setup()
{
pinMode(button1, INPUT); // Set the button as an input
digitalWrite(button1, HIGH); // Pull the button high
pinMode(button2, INPUT); // Set the button as an input
digitalWrite(button2, HIGH); // Pull the button high
pinMode(button3, INPUT); // Set the button as an input
digitalWrite(button3, HIGH); // Pull the button high
}
void loop()
{
if (digitalRead(button1) == 0) // if the button goes low
{
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('k');
delay(1000); // delay so there aren't a kajillion z's
Keyboard.release(KEY_LEFT_CTRL);
}
{
if (digitalRead(button2) == 0) // if the button goes low
{
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('o');
delay(1000); // delay so there aren't a kajillion z's
Keyboard.release(KEY_LEFT_CTRL);
}
{
if (digitalRead(button3) == 0) // if the button goes low
{
Keyboard.print('space');
delay(1000); // delay so there aren't a kajillion z's
}
}
}
BTW. You've not stated why you think your code is not working.
I suspect you have a problem using delay() inside the main loop
Take a look at the Blink without Delay example. as it shows how you can achieve delays without holding up your whole loop() function
i.e
/* Blink without Delay
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
Hi, if you do a TOOLS, AutoFormat, it will tell you you have too many left {, if you look you need an extra right } at the end.
Otherwise it compiles for me.
When yo u have added the extra }, do a AUTO Format, it will format your sketch to look neater, and easier to see where the { and } are nested.
I didn't even look at the formatting the code looked dodgy with those delays in it !
But there is an extra brace after the first if, which I suspect should not be there
if (digitalRead(button1) == 0) // if the button goes low
{
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('k');
delay(1000); // delay so there aren't a kajillion z's
Keyboard.release(KEY_LEFT_CTRL);
}
{
My practice is to always lay out the code with good indentation, then its far easier to spot these sorts of mistakes
Unfortunately the Arduino IDE is a long way behind the curve on decent editors.
My day job is programming, and most of the editors have lots of great features like brace matching as well as automatic code completion for class methods, etc etc etc