hello,
when i press a single key from keyboard it gives me same output two time,
i need output only one time. (on pressing F1 one time it show F1 is pressed F1 is pressed <<<why 2 times????)
please suggest me code for solve my problem
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
#include <KeyboardController.h>
// Initialize USB Controller
USBHost usb;
// Attach keyboard controller to USB
KeyboardController keyboard(usb);
int inten=0;
int x;
void setup()
{
analogWrite(6,inten); //Display intensity
lcd.begin(20,4);
Serial.begin(115200);
Serial.println("Program started");
delay(200);
}
void loop()
{
// Process USB tasks
x=0;
usb.Task();
x = keyboard.getOemKey();
if (x==58)lcd.print("F1 is ptressed");
if (x==59)lcd.print("F2 is ptressed");
if (x==60)lcd.print("F3 is ptressed");
delay(1000);
}
Looks like you should create a keyPressed() function to handle key presses. I think your call to getOemKey() is returning a value on press and release.
And to if what @johnwasser suggested doesn't work, add try adding in a simple button denounce code before the keypressed() or getOemKey(). Just another thought to consider although I agree with John, I think it is reading the key depression and release.
I am sorry for the delay, been button mapping. The following code goes in your Setup.
boolean denounce (loolean last){
boolean current = digitalRead(BUTTON);
if(last != current){
delay(5);
current = digitalRead(BUTTON);
}
return current;
}
Then in your Loop you use this to define when action is taken based on Button press.
currentButton = denounce(lastButton);
if(lastButton == LOW && currentButton == HIGH){
//whatever your statement is
}
Now you can modify that to suit your specific need but that is the basic denounce I use whenever I have a button press jn any project. Hope it helps. Best of luck
thanks for quick response but in your case there was only two state of button (
low& high) while in my case state of button have many variable value more then 32 key each key have their unique value. problem is got this unique value to time in single pressing. (last state and current state same)
i hope u understand my problem i will wait for your valuable response.
Ok, I just want to make sure I understand you. You have 'x' amount of keys and each key has it's own set variable that shouldn't change. And you need to possibly, and I say "possibly" debounce each key press to get the output just the one time.
I have only denounced about 4 buttons at one time, I know you can do more, but you base each button from a digital pin input. And you included a so your trying to emulate the Keyboard, I feel so stupid now. Going back to what @johnwasser said about creating a keyPress() function. I'd suggest that instead of <KeyboardController.h> try using it should already be in your library. It supports the keyPress() function and others it should give you the one output that you are needing instead of the 2.
Does it work if you replace your loop() function with this:
void keyPressed()
{
int x = keyboard.getOemKey();
if (x == 58) lcd.print("F1 is ptressed");
if (x == 59) lcd.print("F2 is ptressed");
if (x == 60) lcd.print("F3 is ptressed");
}
void loop()
{
// Process USB tasks
usb.Task();
}
sir i'm got every key value on lcd after do as u suggest.i want to make a program to write on lcd using usb keyboard . but i'm facing some problem.
how to erase using' back space button ' how controller got the information about my cursor position.
my final objective is maintain the database of any organisation using due +lcd +usb key board.
can u suggest any program that would be helpful for me to achieve my gole
You can't get the cursor position from the LCD. You have to remember the cursor position. You can move the cursor to any position with lcd.setCursor().
To delete a character with backspace, move the cursor back one space, print a space character (' ') and move the cursor back one space again.