help using Keyboard.press triggered by a ping sensor

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Davessi
 
Posts: 5
Joined: Sun Oct 03, 2021 10:38 am

help using Keyboard.press triggered by a ping sensor

Post by Davessi »

Hi Forum,

Looking for some help on a tricky project. I have been dabbling with Arduino for simple projects, but this one has me stumped. I need to be able to toggle a video to play at a distance of 5' or less and a different video at 6' and further. I can get the ping sensor to do the simple function of reading the distance and displaying in the serial monitor. That all works fine and I can get the 'Keyboard.press and Keyboard.release functions to work too. That said, I have the issue of the Void Loop constantly activating the keyboard and printing out A's or D's (see code below). I simplified my code so I would just be working within 20 cm and I could use my hand to test the functions.

I am using a program on my PC that uses mapped keyboard presses to trigger the playback of the video files. For example, if i hit the 'D' key on my keyboard it will play a certain video and if I hit the 'A' key it will play a different video. Works great when it's a human pressing the buttons. However, the Arduino (my code) keeps hitting the keys over and over and it restarts the videos.

Is there a way to still ping for the distance parameters (6' >= press 'A') and (5' <= press 'D') and toggle the keyboard one time?

Code: Select all

#include <NewPing.h>
#include <Keyboard.h>


#define PING_PIN  12  // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); // NewPing setup of pin and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(1000);                     // Wait 1s between pings. 
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

Keyboard.begin();
 if (sonar.ping_cm()>= 21)
  { 
  Keyboard.press('A');
  delay(100);
  Keyboard.release('A'); 
  delay(6000);
  }
 else if (sonar.ping_cm()<= 20){ 
  Keyboard.press('D'); 
  delay(100);
  Keyboard.release('D');
  delay(6000);
 } 
Keyboard.end();
}

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: help using Keyboard.press triggered by a ping sensor

Post by adafruit_support_bill »

If you only want it to execute once, move all that code into your setup.

User avatar
Davessi
 
Posts: 5
Joined: Sun Oct 03, 2021 10:38 am

Re: help using Keyboard.press triggered by a ping sensor

Post by Davessi »

Hey Bill, thank you for the reply. I probably should have been more clear. I do need the program to loop and check the state of the ping distance, but instead of just printing keyboard characters in a stream, I would like for the keyboard.press to act more like a monetary push button.

For example, if the ping is > 20cm toggle character 'D' on/off and wait for a 30 sec video to play, BUT if the ping if <20cm toggle character 'A' on/off and wat for a 60 sec video. this is my dilemma.

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: help using Keyboard.press triggered by a ping sensor

Post by adafruit_support_bill »

if the ping is > 20cm toggle character 'D' on/off and wait for a 30 sec video to play, BUT if the ping if <20cm toggle character 'A' on/off and wat for a 60 sec video.
Then you need to increase your delays. The parameter to the delay function is in milliseconds. Currently you are delaying by 6000 milliseconds which is just 6 seconds.

User avatar
Davessi
 
Posts: 5
Joined: Sun Oct 03, 2021 10:38 am

Re: help using Keyboard.press triggered by a ping sensor

Post by Davessi »

Thank you for helping me think through this ping sensor project. I have simplified the code and this works just fine. i have set a max limit on the sensor ping and dialed in to trigger my media player at 5' or less. I did use a delay for 14 sec. as a test with a short video loop.

my only REAL problem is I won't know how long these video will be until we actually build these kiosks. Some might be 1 minute and some might be 2 minutes. i just don't know at this point. I can probably program my media player to send out a 'keystroke' or ASCII character once the video is done playing. Can the Arduino read a keystroke or character through the USB port?

Code: Select all

#include <Keyboard.h>


#define PING_PIN  12  // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); // NewPing setup of pin and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(600);                     // Wait 1s between pings. 
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

Keyboard.begin();
 //if (sonar.ping_cm()> 20)
  //{ 
  //Keyboard.press('A');
  //delay(100);
  //Keyboard.release('A'); 
  //delay(14000);
  
 if (sonar.ping_cm()<= 150){ 
  Keyboard.press('D'); 
  delay(100);
  Keyboard.release('D');
  delay(14000);
 } 
Keyboard.end();

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: help using Keyboard.press triggered by a ping sensor

Post by adafruit_support_bill »

Can the Arduino read a keystroke or character through the USB port?
Yes it can. Use Serial.Read();

https://www.arduino.cc/reference/en/lan ... rial/read/

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”