Hi guys,
I´m struggling defining a one button control in the PS/2 code.
In the PS/2 dev code attached, the loop tells to automatically write the letter "A" if the host is not sending.
Now what I try to accomplish is to push a button so that it writes the letter .
Anyone who would be willing to guide me through that?
I´m stuck.
My question:
I guess, my problem is to make this button work inside the ps/2 code.
The attached code should send "a" when pressing the button. it doesn´t.
When I define a LED Pin inside this PS/2 code, I can make the code working (I attach the arduino to the PC, and by pushing the button the LED turns on), and I can make the "A"s be written, if I grey out " if (buttonState == HIGH)" in the loop.
But making the button write the letter - nope.
Thanks
the code:
#include "ps2dev.h" // to emulate a PS/2 device
const int buttonPin = 6;
int buttonState = 0;
PS2dev keyboard(3,2); // PS2dev object (2:data, 3:clock)
int enabled = 0; // pseudo variable for state of "keyboard"
void ack() {
//acknowledge commands
while(keyboard.write(0xFA));
}
int keyboardcommand(int command) {
unsigned char val;
switch (command) {
case 0xFF: //reset
ack();
//the while loop lets us wait for the host to be ready
while(keyboard.write(0xAA)!=0);
break;
case 0xFE: //resend
ack();
break;
case 0xF6: //set defaults
//enter stream mode
ack();
break;
case 0xF5: //disable data reporting
//FM
enabled = 0;
ack();
break;
case 0xF4: //enable data reporting
//FM
enabled = 1;
ack();
break;
case 0xF3: //set typematic rate
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
case 0xF2: //get device id
ack();
keyboard.write(0xAB);
keyboard.write(0x83);
break;
case 0xF0: //set scan code set
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
case 0xEE: //echo
//ack();
keyboard.write(0xEE);
break;
case 0xED: //set/reset LEDs
ack();
keyboard.read(&val); //do nothing with the rate
ack();
break;
}
}
void setup() {
pinMode(buttonPin, INPUT);
while(keyboard.write(0xAA)!=0);
delay(10);
}
void loop() {
unsigned char c;
buttonState = digitalRead(buttonPin);
if( (digitalRead(3)==LOW) || (digitalRead(2) == LOW)) {
while(keyboard.read(&c)) ;
keyboardcommand(c);
}
else
if (buttonState == HIGH)
{
keyboard.write(0x1C); // \
keyboard.write(0xF0); // |- send 'a'
keyboard.write(0x1C); // /
delay (1000); // wait 1 second
}
}
I don't suppose that it matters how the switch is wired.
No, really, it is absolutely critical that it is wired correctly. And, since you haven't said anything about how it is wired, I can only guess that it is not wired correctly.
The simplest way to wire a switch is to attach one leg to ground. Attach the other way to a digital pin, and turn the internal pullup resistor on for that pin. Then, LOW means pressed, and HIGH means released.
But: with the same wiring and the same code, if I define a ledpin at 13 and insert a correspondent odeline in the loop,
the button can switch the led on and off.
This implies for me, that the wiring is correct, and there is a problem in the code.
So I rewired and programmed with internal pull-up resistor, and programmed a control-led, that switches on when the "A"message should be sent.
Same behaviour: when connected only to power, I can switch the led on and off, when connected to PS/2,
nothing happens.
But I discovered another interesting fact: when I connect the arduino to PS/2with the button pressed, I get it to send "A" messages as it should (and the control led turns on). When the button is released and pressed again, it does nothing, as before.
So I think: the wiring is ok, the programming is almost ok. There must be something in the loop preventing the arduino from sending the "A" messages.
The backslash at the end of the line is a continuation character. It means that the comment is continued on the next line. The next line being the one that is supposed to send a letter, it really isn't a good idea to comment it out.