Go Down

Topic: How to send keyboard presses to arduino for testing purposes (Read 456 times) previous topic - next topic

allanonmage

I'd like to test out my project by using keyboard input on my PC.  I've found lots of things to use a keyboard directly on an arduino, and lots of other projects, but that's more work than a few lines of code, so if I have to do that, I'll just keep going and use the PS2 controller.  But that's significantly more work before I start testing, so I'd rather not do that.  I find testing frequently makes for an easier project.

I've got an Arduino ADK on top of a Roomba 4 series.  I've tested the communication with code from
http://hackingroomba.com/code/micro/
using Arduino 0023 (simpler than fixing the code) and would like to use the keyboard on my PC to make the Roomba move backwards and forwards a little bit on my desk.  Once I get that done, I'd like to move to getting the PS2 interface to wok, and then I'll use the PS2 to remote control the Roomba.

allanonmage

I found this guide:

http://arduino.cc/en/Tutorial/ReadASCIIString

And have generated the following code:

Code: [Select]

  int ledPin =  13;
 
 
void setup(){

  // initialize serial:
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
} // end setup

void loop(){
   while (Serial.available() > 0) {
     int inVal = Serial.parseInt();
     
     if (Serial.read() == '\n') {
       if (inVal > 0){
       digitalWrite(ledPin, HIGH);
       } // end if
      else
         digitalWrite(ledPin, LOW);
     } // end if
       
       
   } //end while()
 
} // end loop


I think I am missing something pretty basic.  I'm trying to take a key and turn the LED on or off based on criteria.  I am using the serial monitor to send a value to the ADK.  I don't seem to be getting a response from the Arduino ADK.  The LED will stay on for a period of time, then turn off, and then will not respond.

PaulS

Quote
I am using the serial monitor to send a value to the ADK.

Are you afraid to write to the serial monitor for some reason?

PeterH

Rather than trying to read a decimal number from the serial port, it seems to me you could simply read one character and test whether it corresponds to a movement command.

Code: [Select]

// untested
if(Serial.available() > 0)
{
    switch(Serial.read())
    {
        case 'f':
            Serial.println("Moving forwards");
            moveForwards();
            break;

        case 'l':
            Serial.println("Turning left");
            turnLeft();
            break;

        ... etc for any other commands
    }
}



Note that the Arduino IDE's serial monitor will only send characters over the serial port when you click the 'send' button. It would be possible to access the serial port using other applications that send each character as you type it - if this is just for initial testing, I don't suppose you will be bothered about that.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up