2 keys usb box - sparkfun micro pro

Hi everyone.

I need to build a usb box with 2 push-buttons, one to send space bar and one send escape to a pc.
i bought sparkfun micro pro and i used a template code but its written only for 1 key to send space bar.
it works great and i have "space bar" when i ground pin 9.
can anyone help inserting a "esc" lets say on pin 10?
i no nothing about programing.
Any help is highly appreciated!!!
Thanks

The code i used is:

#include <Keyboard.h>
int buttonPin = 9; // Set a button to any pin

void setup()
{
pinMode(buttonPin, INPUT); // Set the button as an input
digitalWrite(buttonPin, HIGH); // Pull the button high
}

void loop()
{
if (digitalRead(buttonPin) == 0) // if the button goes low
{
Keyboard.write(' '); // send a ' ' to the computer via Keyboard HID
delay(1000); // delay so there aren't a kajillion z's
}
}

Do you understand the code that you have?

sterretje:
Do you understand the code that you have?

Honestly, i dont event know how i made it work... i know nothing... i just followed some steps in a website

The ASCII code for escape 27 decimal. Maybe try to write that value to send an escape. Change the Keyboard.write(' '); to

Keyboard.write(27);

Not sure if it will work, no experience with an Arduino as a HID.

I will try to find time to explain your code to you tomorrow.

sterretje:
The ASCII code for escape 27 decimal. Maybe try to write that value to send an escape. Change the Keyboard.write(' '); to

Keyboard.write(27);

Not sure if it will work, no experience with an Arduino as a HID.

I will try to find time to explain your code to you tomorrow.

Thanks sterretje

I searched for escape code and tried myself changind (' ') to (KEY_ESC) and it worked. i tried to to repeat the code, ex copy code, and paste again and change buttonpin = 9 to 10, but didnt work...

#include <Keyboard.h>
int buttonPin = 9;  // Set a button to any pin

void setup()
{
  pinMode(buttonPin, INPUT);  // Set the button as an input
  digitalWrite(buttonPin, HIGH);  // Pull the button high
}

void loop()
{
  if (digitalRead(buttonPin) == 0)  // if the button goes low
  {
    Keyboard.write(' ');  // send a ' ' to the computer via Keyboard HID
    delay(1000);  // delay so there aren't a kajillion spaces's
  }
  int buttonPin = 10;  // Set a button to any pin

void setup()
{
  pinMode(buttonPin, INPUT);  // Set the button as an input
  digitalWrite(buttonPin, HIGH);  // Pull the button high
}

void loop()
{
  if (digitalRead(buttonPin) == 0)  // if the button goes low
  {
    Keyboard.write(KEY_ESC);  // send a 'esc' to the computer via Keyboard HID
    delay(1000);  // delay so there aren't a kajillion esc's
  }

i searched alot in the web for 2 keys arduino code but can only find keypad matrixes etc... :confused:

Show your latest version of the code. And please use code tags when posting code.

sterretje:
Show your latest version of the code. And please use code tags when posting code.

Oops... sorry have no idea how to tag code... googling it now

Before your code, type [code]. After the code, type [/code]

sterretje:

Before your code, type [code]. After the code, type [/code]

Thanks! just tagged code in previous answer

Sorted out, found a sketch for 4 buttons and edited it down to 2 keys! its working like a charm!
Thanks for your help

#include <Keyboard.h>    
void setup() {
pinMode(9,INPUT_PULLUP);  // sets pin 9 to input & pulls it high w/ internal resistor
pinMode(10,INPUT_PULLUP);  // sets pin 10 to input & pulls it high w/ internal resistor

Serial.begin(9600);       // begin serial comms for debugging

}

void loop() {
  
 Keyboard.begin();         //begin keyboard 
 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
  }
  else if (digitalRead(10) == 0){  // if button 10 is pressed
    Keyboard.write(KEY_ESC); // send string
    delay(200);
}
}

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.