0

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?

4
  • 5
    Uhh please don't tell me you just made up the term "keypressed" because I'm pretty sure that's NOT how programming works. By the way, getting a keyboard input to control a USB device is no where near simple. Along with the Arduino code for the Arduino, you need another program that will detect keyboard strokes, then interface serial with the Arduino.
    – Bradman175
    Jan 27, 2017 at 7:53
  • Is not there something that can help me Like plugin
    – kspgame ksp
    Jan 27, 2017 at 8:00
  • I think keypressed() should be serial.Read() and then send the letter a 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!
    – Elbehery
    Jan 27, 2017 at 8:33
  • 1
    It tells you EXACTLY what your problem is: "'keypressed' was not declared in this scope". It means you never defined "keypressed". How is the Arduino supposed to know what you wanted it to mean? Arduinos can't read minds.
    – DerStrom8
    Jan 27, 2017 at 11:26

2 Answers 2

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.