Hey guys,
I have an ultrasonic sensor that works as a keyboard. Whenever it senses that something is closer than 10 cm, it presses a button. However, it continues to press the button over and over again until the object before the sensors moves out of the way.
I would like to make the sensor press a button once when something is closer than 10cm, and then stop. Only when the object before the sensor moved out of range and came into range again, does the button get pressed again.
Also, i would like to make it so whenever all sensors are activated at the same time, they're pressing a whole different key.
Thanks in advance!
Here's the code:
#include <Keyboard.h>
int trigPin1=8;
int echoPin1=7;
int trigPin2=10;
int echoPin2=9;
int trigPin3=6;
int echoPin3=5;
int trigPin4=12;
int echoPin4=11;
int trigPin5=10;
int echoPin5=11;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
pinMode(trigPin5, OUTPUT);
pinMode(echoPin5, INPUT);
Keyboard.begin();
}
void loop() {
long duration1, distance1;
digitalWrite(trigPin1, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 * .0343) / 2;
if (distance1 <= 10){
Keyboard.press('a');
Keyboard.release('a');
}
else {
Serial.print ( "Sensor1 ");
Serial.print ( distance1);
Serial.println("cm");
}
//delay(250);
long duration2, distance2;
digitalWrite(trigPin2, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2 * .0343) / 2;
if (distance2 <= 10){
Keyboard.press('b');
Keyboard.release('b');
}
else {
Serial.print("Sensor2 ");
Serial.print(distance2);
Serial.println("cm");
}
//delay(250);
long duration3, distance3;
digitalWrite(trigPin3, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
distance3= (duration3 * .0343) / 2;
if (distance3 <= 10){
Keyboard.press('c');
Keyboard.release('c');
}
else {
Serial.print("Sensor3 ");
Serial.print(distance3);
Serial.println("cm");
}
//delay(250);
long duration4, distance4;
digitalWrite(trigPin4, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin4, LOW);
duration4 = pulseIn(echoPin4, HIGH);
distance4= (duration4 * .0343) / 2;
if (distance4 <= 10){
Keyboard.press('d');
Keyboard.release('d');
}
else {
Serial.print("Sensor4 ");
Serial.print(distance4);
Serial.print("cm");
}
//delay(250);
long duration5, distance5;
digitalWrite(trigPin5, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin5, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin5, LOW);
duration5 = pulseIn(echoPin5, HIGH);
distance5= (duration5/2) / 29.1;
if (distance5 >= 500 || distance5 <= 0){
Serial.println("Out of range");
}
else {
Serial.print("Sensor5 ");
Serial.print(distance5);
Serial.println("cm");
}
delay(100);
}