So all I wanted to do is that whenever I press an external button, I get the "W" keystroke on the computer. Simple, right? Problem is that I seemed to have screwed up the code and now I can't even revert the arduino to its default formatted status. Whenever I plug the arduino to the PC, it's as though "W" is constantly pressed. When I try to upload a generic "empty" code that does nothing, I get "An error occurred while uploading the sketch", despite the fact the verify process goes well. I also tried resetting before/during uploading, didn't help.
As the title says it's an arduino micro. Right now I less care about it working, more about how do I bring it back to its default, formatted status?
Here is the code I uploaded, not sure how to post with proper syntax highlights
#include <Keyboard.h>
int inPin = 2; // pushbutton connected to digital pin 2
int val = 0; variable to store the read value
void setup() {
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop() {
val = digitalRead(inPin); // read the input pin
if (inPin, HIGH)
{
Keyboard.press('W');
}
It shouldn't be spamming W unless you press the button, so it's not hosing up your execution cycles.
On a side note, I would always add some Serial.println statements in your code so you can verify what's going on during execution using the Serial Monitor. It helps debugging problems with code such as yours.
Another side note, I would recommend you add some amount of delay to your loop() routine, that way that block of code has a legitimate amount of time to react to sensors, button presses, etc.
Can you verify your Arduino is still properly detected by your PC and getting a COM port assigned?
It will spam as the if statement is wrong.
I will post a possible solution once I'm behind a PC.
It shouldn't be spamming W unless you press the button, so it's not hosing up your execution cycles.
I agree, but it still does. I'm not pressing the button. Nevertheless, I can't debug it since I can't override the code now.
On a side note, I would always add some Serial.println statements in your code so you can verify what's going on during execution using the Serial Monitor. It helps debugging problems with code such as yours.
Duly noted, but too late to add anything now.
Another side note, I would recommend you add some amount of delay to your loop() routine, that way that block of code has a legitimate amount of time to react to sensors, button presses, etc.
Fair enough, makes sense, but this is stuff I'd probably figure out on my own with trial an error, problem is I can't do anymore of that since my arduino refuses to be overwritten.
Can you verify your Arduino is still properly detected by your PC and getting a COM port assigned?
It is.
It will spam as the if statement is wrong.
Right, but I can't debug it now because I can't write anything else. My biggest problem is why the heck am I getting "An error occurred while uploading the sketch" 
Steps to possibly resolve the issue
-
Disconnect the board.
2)
Reboot PC.
3)
Load blink sketch or something else in IDE
4)
In file -> preferences, enable verbose output during upload.
5)
Press and keep the reset button pressed.
6)
Connect the board, still keeping the reset button pressed.
7)
Compile and upload a sketch.
8)
Once the IDE reports the memory usage, it will show lines indicating something with ports. Release the reset button.
Let us know if it works. No board at hand so not tested.
Regarding your button
a)
Are you using a pull-down resistor?
b)
The if should be
if(val==HIGH)
c)
If I'm not mistaken, after a key press you need to release as well.
PS
Please read How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum, specifically point #7 about posting code.
"5)
Press and keep the reset button pressed.
6)
Connect the board, still keeping the reset button pressed.
7)
Compile and upload a sketch."
That seemed to have solved the problem! Thanks a lot!!!