I tried to turn on a LED with my PC keyboard and an Arduino Uno with the letter A.
I get the message
exit status 1
'keypressed' was not declared in this scope
My code:
int led1 = 10;
void setup()
{
pinMode(led1, OUTPUT);
}
void loop()
{
if(keypressed == 'a'){
digitalWrite(led1, HIGH);
delay(500);
}
}
What is the problem?
2 Answers 2
Firstly, you can't just make up the term "keypressed" because the programming language is unable to understand it (unless you assign it as a variable but it's purpose then would just be to store and retrieve values). Secondly the arduino itself is unable to detect keystrokes from the computer by itself. It needs a program on the computer to be able to detect keystrokes from the keyboard then send it to the arduino.
So how would one do this?
Assuming you use Windows, learn Python (A not-so-hard programming language on the computer), then look up the msvcrt library and from there learn how to get key strokes (this only works if you are currently selecting the running program). Then in arduino and python, learn how to use serial. For Python, you need to download this library and learn how to use it. For Arduino it's already in there you just have to learn how to use it. Then you can do the rest and make it turn on your LED.
I hope this helps.
Here you declared keypressed as a variable, so you need to assign an input to it using
'keypressed = serial.read()' without quotes before if statement in void loop. Note make make use of serial monitor for input and also assign 'serial.begin(9600)' without quotes in void setup function before pinmode statement.
keypressed()
should beserial.Read()
and then send the lettera
from the serial monitor or maybe build a python script to find, connect to the arduino serial port then you are able to send this character.keypressed()
is not a valid arduino function!