Hey I'm trying to make this code where it pretty much when I press the button (Pin 2) it prints the letter n.
Then when I press it again it prints it again but I'm having a bit of trouble right now the pin isn't even connected and it just keeps non stop printing n
Thanks But how do I add multiple buttons to different pins how would I write that?
#include <Keyboard.h>
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//if the button is pressed
if(digitalRead(2)==LOW) {
//Send the message
Keyboard.print("Hello!");
delay(1500);
}
}
#include <Keyboard.h>
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//if the button on pin 2 is pressed
if(digitalRead(2)==LOW) {
//Send the message
Keyboard.print("Hello!");
delay(1500);
}
//if the button on pin 3 is pressed
if(digitalRead(3)==LOW) {
//Send the message
Keyboard.print("World!");
delay(1500);
}
}
how do I add multiple buttons to different pins how would I write that?
Put the pin numbers in an array and iterate through that. When you find that one of the buttons has become pressed, note become pressed not is pressed then use the index number of the button pin to access an array of values to be sent.
As an example, this illustrates what I described above but needs to be changed to slow down the repetition rate (do NOT use delay()) or to detect when a button becomes pressed, see the StateChangeDetection example in the IDE.
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//if the button on pin 2 is pressed
if(digitalRead(2)==LOW) {
//Send the message
Keyboard.print("Hello!");
delay(1500);
}
//if the button on pin 3 is pressed
if(digitalRead(3)==LOW) {
//Send the message
Keyboard.print("World!");
delay(1500);
}
}
It's crude but will work. How many buttons?
Thanks it worked but for some reason when I try to make it, it doesn't work could you please make the file with 11 buttons.
Thanks it worked but for some reason when I try to make it, it doesn't work
So it worked but it did not work ???
Please post the code that you tried, but if you have 11 buttons then 11 copies of the same piece of code is not the way to do it. See my previous post suggesting using arrays.
#include <Keyboard.h>
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
//if the button on pin 2 is pressed
if(digitalRead(2)==LOW){
//Send the message
Keyboard.print("Hello!");
delay(1500);
}
//if the button on pin 3 is pressed
if(digitalRead(3)==LOW){
//Send the message
Keyboard.print("World!");
delay(1500); }
}
It compiles OK for me if I set the board to be a Leonardo
Here is the code laid out slightly differently but fundamentally unchanged
#include <Keyboard.h>
void setup()
{
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
Keyboard.begin();
}
void loop()
{
//if the button on pin 2 is pressed
if (digitalRead(2) == LOW)
{
//Send the message
Keyboard.print("Hello!");
delay(1500);
}
//if the button on pin 3 is pressed
if (digitalRead(3) == LOW)
{
//Send the message
Keyboard.print("World!");
delay(1500);
}
}
Try copy/pasting it into an empty IDE window and compiling it
A struct is like a record in a phone book with e.g. first name, last name and phone number. The above links a pin (where the button is connected) to a specific text (that will be 'printed' when the button is pressed). The addNewline allows you to send a new line after the text is 'printed'. Keyboard.println() does not seem to send the cursor to the beginning of a new line).
Next you can declare an array with an entry for each button action.
BUTTONACTION buttonActions[] =
{
{2, "Hello", false}, // pin 2, text to 'print', do not send KEY_RETURN
{3, "World", true}, // pin 3, text to 'print', send KEY_RETURN
// add more here
};
In setup(), you can set all the pins to INPUT_PULLUP.
void setup()
{
// start keyboard functionality
Keyboard.begin();
//setup all button pins for INPUT_PULLUP; buttons need to be wired between pin and ground
for (int cnt = 0; cnt < sizeof(buttonActions) / sizeof(buttonActions[0]); cnt++)
{
pinMode(buttonActions[cnt].pin, INPUT_PULLUP);
}
}
And in loop you can loop through the array
void loop()
{
static int buttonNumber = 0;
if(digitalRead(buttonActions[buttonNumber].pin) == ISPRESSED)
{
Keyboard.print(buttonActions[buttonNumber].txt);
if(buttonActions[buttonNumber].addNewline== true)
{
Keyboard.write(KEY_RETURN);
}
// a crude debounce
delay(100);
// wait for button to be released
while (digitalRead(buttonActions[buttonNumber].pin) == ISPRESSED)
{
// do nothing
}
}
// next button
buttonNumber++;
if(buttonNumber == sizeof(buttonActions) / sizeof(buttonActions[0]))
{
buttonNumber = 0;
}
}