KeyBoard emulation

Status
Not open for further replies.

sosidee

Member
We use teensy lc to emulate a keyboard.
We wrote a software in vb.net that hooks the keyboard to catch the key events on background.
The teensy is connected to a photocell to count people passages.
Recently in windows 10, after a random number of cicles, our software does not detect the teensy keyboard any more.
Do you have any suggestion?

The teensy code:

const int ledPin = 13;
const int buttonPin = 20;
const int debounceTime = 20; // // 10 ms debounce
int primoavvio = 0;
byte previousState = HIGH;

void setup() {

pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (primoavvio == 0) {
blink();
}
byte buttonState = digitalRead(buttonPin);
if (buttonState != previousState){
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
Keyboard.set_key1(KEY_F12);
Keyboard.send_now();
}
previousState = buttonState;
} else {
Keyboard.set_key1(0);
Keyboard.send_now();
Keyboard.releaseAll();
digitalWrite(ledPin, LOW);
}
delay(100);
}
void blink() {
primoavvio = 1;
digitalWrite(ledPin, HIGH); // set the LED on
delay(750); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(650); // wait for a second
digitalWrite(ledPin, HIGH); // set the LED on
delay(450); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(350); // wait for a second
digitalWrite(ledPin, HIGH); // set the LED on
delay(250); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(150); // wait for a second
digitalWrite(ledPin, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(100); // wait for a second
digitalWrite(ledPin, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
digitalWrite(ledPin, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(100); // wait for a second
digitalWrite(ledPin, HIGH); // set the LED on
delay(100); // wait for a second
digitalWrite(ledPin, LOW);


Thanks
 
You haven't shown us the vb.net part of the code. It's possible the bug is there.

The Teensy code spends a fair bit of time in delay(). It would be more responsive if it were rewritten to use elapsedMillis instead. It would also be good to add code to explicitly debounce the button. The Bounce2 library works pretty well.

Seems odd to transmit a stream of keystrokes when nothing is happening. Every roughly 100ms a keystroke is transmitted whether anything happened or not. That's a bit much. Maybe Windows 10 can't handle it?

Hope this helps!
 
sosidee, i was bored so i wrote this to simplify your massive delay
Code:
void blink() {
  for ( uint16_t i = 0, flash_delay = 750; i < 12; i++ ) {
    digitalWrite(ledPin, !digitalRead(ledPin));
    delay(flash_delay = constrain(flash_delay -= 100, 100, flash_delay));
  }
  primoavvio = 1;
}

Tony
 
sosidee, i was bored so i wrote this to simplify your massive delay
Code:
void blink() {
  for ( uint16_t i = 0, flash_delay = 750; i < 12; i++ ) {
    digitalWrite(ledPin, !digitalRead(ledPin));
    delay(flash_delay = constrain(flash_delay -= 100, 100, flash_delay));
  }
  primoavvio = 1;
}

Tony
Many thanks!
 
I solved the problem!
Thank you all!


#include <Bounce2.h>
const int ledPin = 13;
const int debounce_interval_time = 20; // // 10 ms debounce
const int buttonPin1 = 1;
int key1 = (unsigned char)KEY_F13;
Bounce button1 = Bounce();

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
button1 = Bounce();
button1.attach(buttonPin1);
button1.interval(debounce_interval_time);
blink_setup();

}
void loop()
{

if (button1.update())
{
if (button1.fallingEdge())
{
usb_keyboard_press(key1, 0);
blink_down();
}
else if (button1.risingEdge())
{
usb_keyboard_press(key1, (unsigned char)(MODIFIERKEY_LEFT_CTRL | MODIFIERKEY_RIGHT_CTRL));
blink_up();
}
}
}

void blink_down()
{
digitalWrite(ledPin, HIGH);
delay(150);
digitalWrite(ledPin, LOW);
}

void blink_up()
{
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
}

void blink_setup()
{
for ( uint16_t i = 0, flash_delay = 750; i < 12; i++ )
{
digitalWrite(ledPin, !digitalRead(ledPin));
delay(flash_delay = constrain(flash_delay -= 100, 100, flash_delay));
}
digitalWrite(ledPin, LOW);
}
 
Status
Not open for further replies.
Back
Top