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.
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?
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.
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]