Go Down

Topic: Comunication between Arduino and keyboard (Read 1 time) previous topic - next topic

ste4os

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

s.

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.

ste4os


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?

sjfaustino

#3
Apr 02, 2011, 10:05 pm Last Edit: Apr 02, 2011, 10:07 pm by sjfaustino Reason: 1
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: [Select]

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
  }
}

ste4os

Thanks :)
I tried to write a little sketch:
Code: [Select]
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?

gravebond

#5
Apr 03, 2011, 12:33 am Last Edit: Apr 03, 2011, 12:39 am by gravebond Reason: 1
Try this
Code: [Select]
#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.

ste4os

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

Thanks gravebond ;)

PaulS

Code: [Select]
int val = Serial.read()-('0');

if (val ==1 ) {


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

if (val == '1' ) {

gravebond


Code: [Select]
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 :P
just remember when you put a digit like this '2' , it means its a character.

Go Up