Pages: [1]   Go Down
Author Topic: Comunication between Arduino and keyboard  (Read 1179 times)
0 Members and 1 Guest are viewing this topic.
Milan
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys!
Can I send signal with my keyboard to Arduino with specific function? Even in C language if it exist!

s.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Milan
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to connect a keyboard directly to the Arduino, your best bet is a PS2 keyboard.
See http://www.practicalarduino.com/projects/ps2-keyboard-or-mouse or http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html 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:

Code:
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
  }
}
« Last Edit: April 02, 2011, 03:07:15 pm by sjfaustino » Logged

Milan
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks smiley
I tried to write a little sketch:
Code:
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?
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this
Code:
#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.
« Last Edit: April 02, 2011, 05:39:34 pm by gravebond » Logged

Milan
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok thanks smiley I didn't know that Serial.read() returns a char!
I will try this afternoon!

Thanks gravebond smiley-wink
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int val = Serial.read()-('0');
 
 if (val ==1 ) {

Or, you can do this:
 int val = Serial.read();
 
 if (val == '1' ) {
Logged

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 31
wewt
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int val = Serial.read()-('0');
 
 if (val ==1 ) {

Or, you can do this:
 int val = Serial.read();
 
 if (val == '1' ) {

Or yes you can do that smiley-razz
just remember when you put a digit like this '2' , it means its a character.
Logged

Pages: [1]   Go Up
Jump to: