Host Shield to act as keyboard

I have set up my code to control 3 led's on my arduino uno

const int ledPin = 13; // LED 13
const int ledPin1 = 12;// LED 12
const int LedPin2 = 11; // LED 11
const int buttonPin = 2; // pushbutton pin

int buttonPushCounter = 0; // counter for number of button presses
int buttonState = 0;        // current state of the button
int lastButtonState = 0;   // previous state of the button

void setup()
{
  pinMode (buttonPin, INPUT); // initialise pin as input
  pinMode(ledPin, OUTPUT);    // initialise pin as output
  pinMode (ledPin1, OUTPUT) ; // initialise pin as output
  pinMode (11, OUTPUT) ; // sets the digital pin as output
  Serial.begin (9600);
}

void loop() {
  buttonState  = digitalRead (buttonPin); // read the pushbutton input pin
  if (buttonState != lastButtonState && buttonState == HIGH) {          
    buttonPushCounter++;
    Serial.println ("on");
    Serial.print ("number of button pushes");
    // your actions here when the button is dteceted as being on.
    delay (3000);
    digitalWrite (ledPin, HIGH);   // sets the LED1 on
    delay(10000);                  // waits for 10 seconds
    digitalWrite(ledPin, LOW);    // sets the LED1 off
    delay(5000);                  // waits for 5 seconds
    digitalWrite (ledPin1,HIGH);  // sets LED2 on
    delay (10000);               // wait 10 seconds
    digitalWrite (ledPin1, LOW); // sets LED2 off
    delay (5000) ;               //wait 5 seconds
    digitalWrite (11, HIGH); // turn on led3
    delay (5000);                // wait 5 seconds
    digitalWrite (11, LOW);  // turn off led 3
  }
  else {
    Serial.println ("off");          // if current state is LOW then the button went from on to off
  }
    lastButtonState = buttonState; 
}

I want to run this simultaneously with a computer software program which is started by pressing space bar on the PC. I want the arduino to send the spacebar when the button which starts my loop is presssed. Do i just buy a host shield to put onto the arduino uno and simply plug the usb into the computer for it to see it as a keyboard?

Do i just buy a host shield to put onto the arduino uno and simply plug the usb into the computer for it to see it as a keyboard?

No you do not want a USB host at all you want a USB client.

However, I have told you what you want to do this in your other thread. I have given you example code for doing this and yet you seem intent on ignoring advice given to you.

You can also emulate a keyboard using the keyboard or ps/2 port on the computer. I believe there's a library available to do just such a thing.

KenF: You can also emulate a keyboard using the keyboard or ps/2 port on the computer. I believe there's a library available to do just such a thing.

Yes, here we go:

The core libraries allow an Arduino Leonardo, Micro, or Due board to appear as a native USB Mouse and/or Keyboard to a connected computer.

http://arduino.cc/en/Reference/MouseKeyboard

Grumpy_Mike: No you do not want a USB host at all you want a USB client. ...

Yes, just as Mike said it is important as USB client mode.

FYI.

Arduino Yun which could have two USB ports in client mode, one from Leonardo and the other one from AR9331.

sonnyyu: Yes, here we go:

The core libraries allow an Arduino Leonardo, Micro, or Due board to appear as a native USB Mouse and/or Keyboard to a connected computer.

NO! That's not what I was talking about. That relies on the user having exactly the right kind of arduino.

This is what I was talking about.

KenF: NO! That's not what I was talking about. That relies on the user having exactly the right kind of arduino.

This is what I was talking about.

Now It is OP's job to find PC has PS2 port. Last time I saw one is at 10 years ago. ;)

Grumpy Mike I have not ignored you I simply didn't understand. We all have different rates of learning I guess I'm just well out of my Comfort zone! Is my code now correct?

sonnyyu: Now It is OP's job to find PC has PS2 port. Last time I saw one is at 10 years ago. ;)

Funny enough, I hadn't even realised that they had been phased out.

My laptop has two (one for the mouse, one for the keyboard.) My desktop also has two.

Just checked every other machine in the house. 3 tablets, 4 netbooks, 2 desktops, 3 laptops and not one of them has one.

I've searched high and low but all I can find is a "USB host shield" where will I find a "USB client"

KenF would your idea work using a ps/2 to USB converter!?

gregbarnard: KenF would your idea work using a ps/2 to USB converter!?

I see no reason why not. But they're expensive. Almost a whole dollar. :)

I see no reason why not. But they're expensive. Almost a whole dollar. :)

OUCH! haha ok so to clarify i would need to get a PS/2 to USB converter as well as an old PS/2 cable which i can then connect to my arduino uno and send keystrokes?

gregbarnard:
I’ve searched high and low but all I can find is a
“USB host shield” where will I find a “USB client”

That is because if you want to do what you do then there are Arduinos that will do that. Before those came out there were USB client shields, but now for the same price as a shield you can just get the right arduino.

PS/2 port was introduced in 1987 by IBM.

http://en.wikipedia.org/wiki/PS/2_port

Lenovo to Acquire IBM Personal Computing Division in 2005

http://www.lenovo.com/news/us/en/2005/04/ibm_lenovo.html

Less than two years Lenovo Dropped Name of IBM

http://www.dealerscope.com/article/lenovo-dropping-ibm-name-81569/1

Now IBM is stands for "I Bought Macintosh".

http://www.ahajokes.com/com028.html

Ordered a ps/2 to USB to try it that way but also ordered a Leonardo to do it properly! Cheers guys

Leonardo works perfectly!! Very simple but it works.

thank you all for your help

const int ledPin = 13; // LED 13
const int ledPin1 = 12;// LED 12
const int LedPin2 = 11; // LED 11
const int buttonPin = 2; // pushbutton pin


int buttonPushCounter = 0; // counter for number of button presses
int buttonState = 0;        // current state of the button
int lastButtonState = 0;   // previous state of the button

void setup()
{
  Keyboard.begin();
  pinMode (buttonPin, INPUT); // initialise pin as input
  pinMode(ledPin, OUTPUT);    // initialise pin as output
  pinMode (ledPin1, OUTPUT) ; // initialise pin as output
  pinMode (11, OUTPUT) ; // sets the digital pin as output
  Serial.begin (9600);
}

void loop() {
  buttonState  = digitalRead (buttonPin); // read the pushbutton input pin
  if (buttonState != lastButtonState && buttonState == HIGH) {          
    buttonPushCounter++;
    Keyboard.write (' ');
    Serial.println ("on");
    Serial.print ("number of button pushes");[CODE]

Well done. :)