button to press backspace on keyboard

First post, be gentle.

Micro board. I have 2 buttons, pins 4 and 7. One to press spacebar, one to press backspace. Using notepad.exe to test. the Spacebar part works fine, but I cannot get backspace to work.

I tried not defining KEY_BACKSPACE. I tried Keyboard.write. I tried decimal 178. Does anything stand out here?

#include "Keyboard.h"
#define KEY_BACKSPACE 0xB2

void setup() {
  Serial.begin(9600);
  pinMode(4, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  Keyboard.begin();

  int buttonState4 = digitalRead(4);
  int buttonState7 = digitalRead(7);

  if (buttonState4 == LOW){
    Keyboard.press(KEY_BACKSPACE);
    delay(100);
    Keyboard.releaseAll();
  }
  if (buttonState7 == LOW){
    Keyboard.print(" ");
  }
}
void loop(){
}

Thanks in advance.

Can you try 0x08 as backspace?

Usually these types of problems are caused by the computer being set to use a different keyboard layout than the US keyboard layout the Keyboard library is written for. Are you using a different layout?

The USB Keycode for the "backspace" key on the US PC keyboard is 42 (0x2A).

The ASCII map in the Keyboard library maps 0x08 to 0x2A so sending 0x08 should work.

To send a raw USB Keycode you would add 136 to it to bypass the ASCII-to-Keycode translation. That means 42+136 (== 178 == 0xB2) should also work.

Unless your OS is set for a strange keyboard mapping it should be working. Try making it a printable character to see if your button is working. It may not be.

Current keyboard is pretty standard US keyboard with default windows 8.1 drivers.

Thanks folks. Bad weather and long day. I’ll get back to you all soon and let you know what happened.

Turns out that at some point my sketches stopped uploading and I didn't realize it. The web IDE wanted com port 8, and is the only option. It said there's an error at com7 even though 8 is chosen. Windows says 7 it's in use. See pic.

Alright, let's get away from web version. Installed 1.8.8 standalone IDE instead. Plugged micro into USB port on back of PC instead of front header. I let IDE install whatever drivers it wanted.
Configured Arduino type and port. Was able to successfully upload, but now I get no keyboard responses from pin 4 or 7 at all.

I added Serial.print("4"); and Serial.print("7"); to the respective buttons, but the serial monitor shows nada. Plugged USB back into front header where it used to work, but still no input.
Just toned out my switches to make sure they still work.
Did the standalone IDE drivers fudge things up?

I ran a wire to RST and grounded it twice and tried then tried to upload a sketch. Now board shows up as unrecognized. Is my controller bricked?

Please do this:

  • Attempt an upload.
  • Hover the mouse over the red bar that shows the error message. Click and drag it up so you can see the black console window.
  • Click the icon in the top right corner of the console that looks like two pieces of paper.
  • Paste the error in a message here USING CODE TAGS (</> button on the forum toolbar).

If the length of the output exceeds the forum’s 9000 character limit, save it in a txt file and post it here as an attachment. If you click the “Reply” button you’ll see the “Attachments and other options” link.

Ok, after a week of fiddling it uploads now.

Sketch uses 5888 bytes (20%) of program storage space. Maximum is 28672 bytes.
Global variables use 226 bytes (8%) of dynamic memory, leaving 2334 bytes for local variables. Maximum is 2560 bytes.

Even if I upload exactly the code posted in the original posting I get nothing. Nothing works now. I tried adding conditions to output to serial monitor, nothing.

Gonna go for a refund. Wish I get my week back too.

If those are the last lines shown in the console window, it means you only compiled, and didn't actually do an upload. The compile button is the check mark. The upload button is the arrow that points right that's next to the compile button.

After a successful upload, there should be a teal bar that says "Success: Done uploading {sketch name}".

It says done uploading. I get all the USB connection noises.

The online editor still hasn't worked since I installed the IDE.
Here's what that shows

./opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -hardware opt/arduino-builder/hardware -hardware ./opt/cores -tools opt/arduino-builder/tools -tools ./opt/tools -built-in-libraries opt/libraries/latest -logger humantags -fqbn arduino:avr:micro -build-cache /tmp -build-path /tmp/006121069/build -verbose -prefs runtime.tools.avr-gcc.path=./opt/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2 -prefs runtime.tools.avrdude.path=./opt/tools/avrdude/6.3.0-arduino14 -prefs runtime.tools.arduinoOTA.path=./opt/tools/arduinoOTA/1.2.1 -libraries /tmp/006121069/custom -libraries /tmp/006121069/pinned /tmp/006121069/KSP2

Sketch uses 5856 bytes (20%) of program storage space. Maximum is 28672 bytes.

Global variables use 226 bytes (8%) of dynamic memory, leaving 2334 bytes for local variables. Maximum is 2560 bytes.

Programming with: Serial

Restarting in bootloader mode

Get port list before reset

[COM7]

Touching port COM7 at 1200bps

Set DTR off

Flashing with command:C:/Users/klondike151/.arduino-create/arduino/avrdude/6.3.0-arduino9/bin/avrdude.exe -CC:/Users/klondike151/.arduino-create/arduino/avrdude/6.3.0-arduino9/etc/avrdude.conf -v -patmega32u4 -cavr109 -PCOM7 -b57600 -D -Uflash:w:C:/Users/KLONDI~1/AppData/Local/Temp/arduino-create-agent733566770/KSP2.hex:i

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53

Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:/Users/klondike151/.arduino-create/arduino/avrdude/6.3.0-arduino9/etc/avrdude.conf"

Using Port : COM7

Using Programmer : avr109

Overriding Baud Rate : 57600

AVR Part : ATmega32U4

Chip Erase delay : 9000 us

PAGEL : PD7

BS2 : PA0

RESET disposition : dedicated

RETRY pulse : SCK

serial program mode : yes

parallel program mode : yes

Timeout : 200

StabDelay : 100

CmdexeDelay : 25

SyncLoops : 32

ByteDelay : 0

PollIndex : 3

PollValue : 0x53

Memory Detail :

Block Poll Page Polled

Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack

----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------

eeprom 65 20 4 0 no 1024 4 0 9000 9000 0x00 0x00

flash 65 6 128 0 yes 32768 128 256 4500 4500 0x00 0x00

lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00

hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00

efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00

lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00

calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00

signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : butterfly

Description : Atmel AppNote AVR109 Boot Loader

Connecting to programmer: .avrdude: butterfly_recv(): programmer is not responding

Ok, got an older win7 64bit laptop. Just installed drivers and plugin for chrome. Here's what I get on the online editor.

./opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -hardware opt/arduino-builder/hardware -hardware ./opt/cores -tools opt/arduino-builder/tools -tools ./opt/tools -built-in-libraries opt/libraries/latest -logger humantags -fqbn arduino:avr:micro -build-cache /tmp -build-path /tmp/675664074/build -verbose -prefs runtime.tools.avr-gcc.path=./opt/tools/avr-gcc/5.4.0-atmel3.6.1-arduino2 -prefs runtime.tools.avrdude.path=./opt/tools/avrdude/6.3.0-arduino14 -prefs runtime.tools.arduinoOTA.path=./opt/tools/arduinoOTA/1.2.1 -libraries /tmp/675664074/custom -libraries /tmp/675664074/pinned /tmp/675664074/KSP2

Sketch uses 5856 bytes (20%) of program storage space. Maximum is 28672 bytes.

Global variables use 226 bytes (8%) of dynamic memory, leaving 2334 bytes for local variables. Maximum is 2560 bytes.

Programming with: Serial

Restarting in bootloader mode

Get port list before reset

Same error on 2 different PCs?

Ah, sorry. I forgot that you had switched back to the standard Arduino IDE from the Arduino Web Editor (the standard Arduino IDE only shows upload output when that is enabled at File > Preferences > Show verbose output during upload but the Arduino Web Editor always shows it). So that was the cause of my confusion.

So I’m really confused then. Is it really actually uploading? If it is, why is does nothing happen anymore, even on the serial monitor? Why would it upload on the IDE and not the web app?

This went from a single simple question to completely non functional. Wonder if I should start a new thread in a new section.

This is really hitting me hard emotionally and I’m at the point where I’m thinking about giving it all up completely. It feels bad when you can’t even get past blink tutorial, ya know.

Upload this sketch to your board. It should blink the RX LED (just in case you actually have the Pro Micro, which doesn't have an LED on pin 13):

const byte LEDpin = LED_BUILTIN_RX;
const unsigned int duration = 1000;

void setup() {
  pinMode(LEDpin, OUTPUT);
}

void loop() {
  digitalWrite(LEDpin, HIGH);
  delay(duration);
  digitalWrite(LEDpin, LOW);
  delay(duration);
}

When nothing makes sense, good old Blink is always the best sanity check. I've been using Arduino for almost 7 years now and I still upload Blink quite often.

I believe you when you say it's a sanity check, but I'm still very confused. Here's why.

Your blink code absolutely works, just tried it now. It also worked on the web console which was just erroring out last night.

The code on the first post, I get no output at all on button presses. Spacebar used to work though.

Spring loaded push buttons, normally open, common ground, no resistors. I've toned out the buttons so I know they work. Here's the picture if you want to double-check I'm on the correct pins.

I'm going to search for an emulator. In the meantime do you all see anything wrong with the code in the original post?

One problem is all the code is in the setup function and the loop function is empty. This means the buttons will be checked only once.

The next step is to look at a tutorial on button/switch debouncing. The 100 ms delay is a crude way to prevent contact bounce from generating multiple key presses. There are libraries devoted to debouncing with names like Bounce and Bounce2.

#include "Keyboard.h"

void setup() {
  Serial.begin(9600);
  pinMode(4, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  int buttonState4 = digitalRead(4);
  int buttonState7 = digitalRead(7);

  if (buttonState4 == LOW){
	Keyboard.print("\b");
	delay(100);
  }
  if (buttonState7 == LOW){
    Keyboard.print(" ");
	delay(100);
  }
}

I have to leave town for a few days. I'll read up on debounce and try again when I return. I'll reply then. Thank you all so far.

Ok, gonna wrap up this thread.

0xB2 is actually the keyboard.press function that worked on my US Windows 8.1 box.

Most of my problems stem from my PC still randomly not uploading (from web or IDE) and my failing to recognize when it did or did not. It makes a trial and error approach not work and had me frustrated.

The other part was my expectation that I didn't need to loop since I wasn't doing any mathematics. I thought once a program was loaded into memory that it would execute those functions always.

I'm still working on debounce, but now I know when an upload actually occurs so I can go back to trial and error method.

Thanks for taking time to provide an update klond! I'm glad to hear you're making progress. Thinking you have uploaded new code when the upload actually failed could make things extremely confusing. I have a Micro permanently attached to my computer. Sometimes when I attach another Micro to my computer to do development with, I accidentally select the port for the wrong board from the Tools > Port menu. Then I get all confused wondering why the code I'm uploading isn't doing anything.