Noob question... Reset Arduino

First time learning how to code with Arduino and... I've made a mistake:

#include <Keyboard.h>

int fx1 = 0;

void setup() {
// put your setup code here, to run once:
pinMode(fx1, INPUT);
Keyboard.begin();
}

void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(fx1)==HIGH){
Keyboard.print("test");
}
}

Apparently, this causes the word "test" to be printed non-stop... and I can't even upload an empty sketch because the board will input test into the sketch while I am trying to upload it. Is the board still usable?

Any help would be appreciated! #savethenoob

Did you try to load it with Blink (an Example sketch that is in you IDE under File->Examples in section 1) because the load process -does- interrupt the sketch.

Does the compiler make anything of an empty sketch? It optimizes unused variables and code out, maybe you got an error?
Newly bootloaded AVR's get a copy of Blink so you can see that the chip is operating.

Final question is what board you have? It must have the 32u4 chip or a SAMD chip. That is Arduino Micro or Leonardo or Due and a number of compatibles like the Teensy boards.

Reference > Language > Functions > Usb > Keyboard
Keyboard
[USB]
Description

The keyboard functions enable 32u4 or SAMD micro based boards to send keystrokes to an attached computer through their micro’s native USB port.

If you don't have one of those boards, you can connect a PS2 keyboard (and/or mouse or other PS2 device) to any Arduino (that I know of) through the pins.

Arduino Playground - HomePage ---- the simplest
Arduino Playground - HomePage ---- the next level

yeah.. if you have an arduino with the capability, using the keyboard class like this is a very bad idea... As the doc (which you probably did not read) states

A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move() and Keyboard.print() will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control.

When using the Mouse or Keyboard library, it may be best to test your output first using Serial.print(). This way, you can be sure you know what values are being reported. Refer to the Mouse and Keyboard examples for some ways to handle this.

you might get lucky holding the reset button down and letting it go just when you see the message uploading code in the IDE (upload the blink program - not yours again of course !)

If the board is an Uno with the AVR in the socket (rev 0 to rev 3, the "Pro" has an SMD chip) then you can get a bootloaded ATmega328P (might cost $5, DIPMicro sells bootloaded 328P's) and put that in the socket. It's one reason I don't worry as much about burning a pin on my Unos, sure I could ruin the USB chip but short of that or worse I can replace the chip I program.

PS -- oh wait! There is another solution. The Arduino has an ICSP header, you may be able to program the chip through that. You wouldn't need to connect USB but you would need a chip programmer (another Arduino will do, a real cheap one, software and tutorials exist for it) and jumpers, maybe a capacitor too.

Spacemelody:
the board will input test into the sketch while I am trying to upload it.

Only if you have the editor portion of the Arduino IDE selected.

Consider your code:

Spacemelody:

  if(digitalRead(fx1)==HIGH){

Keyboard.print("test");
  }

The simplest way to make it stop typing is to merely connect pin 0 to GND. Then it will not read HIGH and the Keyboard.print() statement won't run.

I think I'd put a resistor between pin 0 and GND. Pin 0 connects to the USB chip, you'd be grounding that pin too.

OP, which board are you using?

Pressing the reset button will probably work; this needs to be done after the verify step of the upload process.

In file -> preferences, enable verbose output for both compilation and upload. Press and release the reset button at the moment that you see the lines like below

Sketch uses 2444 bytes (7%) of program storage space. Maximum is 32256 bytes.
Global variables use 266 bytes (12%) of dynamic memory, leaving 1782 bytes for local variables. Maximum is 2048 bytes.

It will start the boot loader of your board which runs for about 5 seconds (Leonardo, other boards might be longer or shorter) and the upload should be able to find the boot loader and communicate with it.

GoForSmoke:
If the board is an Uno with the AVR in the socket (rev 0 to rev 3, the "Pro" has an SMD chip) then you can get a bootloaded ATmega328P (might cost $5, DIPMicro sells bootloaded 328P's) and put that in the socket. It's one reason I don't worry as much about burning a pin on my Unos, sure I could ruin the USB chip but short of that or worse I can replace the chip I program.

The code will not compile if it's an Uno; only if it's a board that can act as a HID.

GoForSmoke:
I think I'd put a resistor between pin 0 and GND. Pin 0 connects to the USB chip, you'd be grounding that pin too.

While that would be reasonable advice for an Arduino using a microcontroller without native USB, on the boards that do keyboard emulation the primary microcontroller itself is the USB chip. Pins 0 and 1 are not used for Serial. They are actually Serial1.

Ahhhh. I don't use my Teensys enough.....

GoForSmoke:
I don't use my Teensys enough.....

Who said anything about Teensy (other than you)?
There is nothing that implies a Teensy so far. Could be a Leonardo or a Micro or this or that or ... or a Teensy.

oqibidipo:
Who said anything about Teensy (other than you)?
There is nothing that implies a Teensy so far. Could be a Leonardo or a Micro or this or that or ... or a Teensy.

It may correctly imply that I have Teensy boards and not a Leo, Micro, or Due.

The Teensy 2.0 was out long before the Leo or Micro which use the code that Paul Stoffgren wrote for the Teensy's. I got the Teensy++ 2.0 back then. If I used it more, I'd be more aware of how it works. How can I do that with boards I don't have?

And BTW, I don't need anyone's permission to name boards with 32u4 and better USB/AVR chips in a convo where that chips and others with USB capability are discussed. Sorry that you couldn't see that from under your rock.

... On closer inspection it looks like someone may have been just yearning after Teensys without suggesting one is necessarily involved in this case... :slight_smile:

Seems I have a troll.