Hi guys!
Can I send signal with my keyboard to Arduino with specific function? Even in C language if it exist!
s.
Hi guys!
Can I send signal with my keyboard to Arduino with specific function? Even in C language if it exist!
s.
You need some application running on the PC that collects the keystrokes and sends them to the serial port. The Arduino then reads the serial port to get the key data.
The PC application can be written in a variety of languages, including C, C++, C#, PHP, Python, Ruby, etc.
PaulS:
You need some application running on the PC that collects the keystrokes and sends them to the serial port. The Arduino then reads the serial port to get the key data.The PC application can be written in a variety of languages, including C, C++, C#, PHP, Python, Ruby, etc.
Ok, i suppose there is a C library for this work! Right? What's its name?
If you want to connect a keyboard directly to the Arduino, your best bet is a PS2 keyboard.
See practicalarduino.com or PS2Keyboard Library, Connect a keyboard for user input for explanation and code.
If you want to have the Arduino connected to your computer and control the arduino with the computer's keyboard, why not use the IDE's serial monitor? It will send anything you type to the arduino and all you need is:
void setup()
{
Serial.begin(9600); // Starts serial data
}
void loop()
{
if (Serial.available() > 0)
{
char inByte = Serial.read(); // waits untill somthing is typed
Serial.print(" I got: ");
Serial.println(inByte); // shows what was stored
}
}
Thanks
I tried to write a little sketch:
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop(){
if(Serial.available() > 0){
int inByte = Serial.read();
if(inByte == 1)
digitalWrite(13, HIGH);
if(inByte == 0)
digitalWrite(13, LOW);
}
}
If my input on serial monitor is 1, the led turn on else if my input is 0, the led turn off!
But it doesn''t work. Why?
Try this
#define LED 13
int val =0;
void setup(){
Serial.begin(9600);
pinMode (LED, OUTPUT);
}
void loop (){
while (Serial.available() ==0 ); // Arduino waits to recieve data
int val = Serial.read()-('0');
if (val ==1 ) {
digitalWrite (LED, HIGH);
Serial.println ("ON");
}
else if (val == 0){
digitalWrite (LED, LOW);
Serial.println ("OFF");
}else{
Serial.println ("Invalid!");
}
Serial.println(val);
Serial.flush();
}
The important bit to note is
int val = Serial.read()-('0')
What you need to remember is that when you type 1 into the serial monitor it isnt sending an int, its sending a char. If you look at an ascii table you'll see that the char "1" is equal to 49 in decimal. So in your sketch when your sending 1 your actually sending it 49, which is why your code doesnt turn the led on. The way to get around this is to -('0'-) from your serial.read. This subtracts the value of the character 0 (48) to give you your desired number.
so when you input 1 for example this will happen.
49 - 48 = 1.
Hope this helps.
happy to answer any questions you have about it.
Ok thanks I didn't know that Serial.read() returns a char!
I will try this afternoon!
Thanks gravebond
int val = Serial.read()-('0');
if (val ==1 ) {
Or, you can do this:
int val = Serial.read();
if (val == '1' ) {
PaulS:
int val = Serial.read()-('0');
if (val ==1 ) {
Or, you can do this: int val = Serial.read()<mark>;</mark> if (val == <mark>'1'</mark> ) {
Or yes you can do that
just remember when you put a digit like this '2' , it means its a character.