OK, comments based on the code in reply #4
A usual Arduino code consists of 3 (or 4) sections.
1a)
Includes; this tells the compiler which extra functionalities you want to use (in your case the keyboard functionality)
1b)
Variable declarations; this defines / declares the variables that will be known in all functions (like setup() and loop()); e.g. buttonPin
2)
the setup() function that is only executed once
3)
the loop() function that is executed repeatedly
So
#include <Keyboard.h>
int buttonPin = 9; // Set a button to any pin
first tells the compiler that you want to use the Keyboard functionalities and next tells the compiler that you intend to have a button connected to pin 9.
It would make sense if you have two buttons to give them sensible names; e.g.
#include <Keyboard.h>
int buttonSpace = 9; // space button
int buttonEscape = 10; // escape button
Later in your code it will be easier to determine what the intention of e.g. a digitalRead() is. It also makes it easier to change if there ever is a need to move a button to another pin as you only have to change one variable; in your latest code you need to change it in a couple of places.
Next
void setup()
{
pinMode(buttonPin, INPUT); // Set the button as an input
digitalWrite(buttonPin, HIGH); // Pull the button high
}
This instructs the Arduino to make buttonPin an input and next it instructs the Arduino to enable the internal pull-up.
Using the new button declarations, you can use
pinMode(buttonSpace, INPUT_PULLUP);
pinMode(buttonEscape, INPUT_PULLUP);
Similar to your newest code, but no hard-coded pin numbers.
Next your loop(); you made a mistake there (as you might have realised). You can not declare a function inside another function.
void loop()
{
...
...
void setup()
{
...
...
}
...
...
}
Based on your latest loop() code in reply #9
if (digitalRead(9) == 0) // if button 9 is pushed
{
Keyboard.write(' '); // send single character " "
delay(1000); // delay so you don't get 20 spaces's
}
The digitalRead instructs the Arduino to read pin 9 and next check what the result is. Although your comment is correct, it does not make clear that you're reading the space button
The better way is
if (digitalRead(buttonSpace) == 0)
Usually we don't use 0 but LOW and to make it less confusing (does this mean pressed or not pressed), you can add after your include
#define ISPRESSED LOW
which defines a sensible name for the pressed state of the button and next use
if (digitalRead(buttonSpace) == ISPRESSED)
This makes the code fully self-explaining; you can do the same for the button on pin 10.
I although noticed that you have a Keyboard.begin() in loop(). This might not be necessary as I suspect that you can do that once in setup().
Below the full reworked version; I can't test but it does compile. Give it a shot
#include <Keyboard.h>
#define ISPRESSED LOW
const byte buttonSpace = 9;
const byte buttonEscape = 10;
void setup()
{
pinMode(buttonSpace, INPUT_PULLUP); // sets pin 9 to input & pulls it high w/ internal resistor
pinMode(buttonEscape, INPUT_PULLUP); // sets pin 10 to input & pulls it high w/ internal resistor
Keyboard.begin(); //begin keyboard
}
void loop()
{
if (digitalRead(buttonSpace) == ISPRESSED)
{
Keyboard.write(' '); // send single character " "
delay(1000); // delay so you don't get 20 spaces's
}
else if (digitalRead(buttonEscape) == ISPRESSED)
{
Keyboard.write(KEY_ESC); // send string
delay(200);
}
}
As pin number usually are not modified in your code, I have made the const; it you accidentally try to modify it in e.g. loop(), the compiler will complain. I've also made them byte instead of int to save some space.
Lastly about your delays. You should read the pin change detection example and apply it's principles. From your comments, I understand that you don't want to send a space when the button is pressed but when the button goes from 'not pressed' to 'pressed'.
I hope this helps you to understand what you have written and improve your coding skills.