hello , I am doing an installation for a local arts festival which involves a portaloo rigged up with 4 lv-ez1 sonars , 3 facing externally to trigger single sounds and one very basic "air therimin" inside. the readings in the serial monitor are pretty accurate but the desired actions : Keybaord.printing some letters which will play notes on a sample player are not occurring except in the case of the internal sonar module. here is the code I have.
const int pingPin = 4;
const int pingPin1 = 2;
const int pingPin2 = 13;
const int pingPin3 = 7;
int newinchvalue;
int oldinchvalue;
int newinchvalue1;
int oldinchvalue1;
int newinchvalue2;
int oldinchvalue2;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm , duration1, inches1, cm1, duration2, inches2, cm2, duration3, inches3, cm3;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
delay (20);
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin1, OUTPUT);
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);
pinMode(pingPin1, INPUT);
duration1 = pulseIn(pingPin1, HIGH);
newinchvalue = inches;
if (abs(oldinchvalue - newinchvalue) > 2.00)//the value has changed
{
if (newinchvalue > 2.00 && newinchvalue <= 30.00) //and it is in range
{
Keyboard.print ('d');
oldinchvalue = newinchvalue;
}
}
delay (20);
pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
pinMode(pingPin2, INPUT);
duration2 = pulseIn(pingPin2, HIGH);
newinchvalue = inches2;
if (abs(oldinchvalue1 - newinchvalue1) > 2.00)//the value has changed
{
if (newinchvalue1 > 2.00 && newinchvalue1 <= 30.00) //and it is in range
{
Keyboard.print ('f');
oldinchvalue1 = newinchvalue1;
//save value for next time
}
}
delay (20);
pinMode(pingPin3, OUTPUT);
digitalWrite(pingPin3, LOW);
delayMicroseconds(2);
digitalWrite(pingPin3, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin3, LOW);
pinMode(pingPin3, INPUT);
duration3 = pulseIn(pingPin3, HIGH);
newinchvalue2 = inches3;
if (abs(oldinchvalue2 - newinchvalue2) > 2.00)//the value has changed
{
if (newinchvalue2 > 2.00 && newinchvalue2 <= 30.00) //and it is in range
{
Keyboard.print ('g');
oldinchvalue2 = newinchvalue2;
//save value for next time
}
}
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
inches1 = microsecondsToInches(duration1);
cm1= microsecondsToCentimeters(duration1);
inches2 = microsecondsToInches(duration2);
cm2= microsecondsToCentimeters(duration2);
inches3 = microsecondsToInches(duration3);
cm3= microsecondsToCentimeters(duration3);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
Serial.print(inches1);
Serial.print("in, ");
Serial.print(cm1);
Serial.print("cm");
Serial.println();
Serial.print(inches2);
Serial.print("in, ");
Serial.print(cm2);
Serial.print("cm");
Serial.println();
Serial.print(inches3);
Serial.print("in, ");
Serial.print(cm3);
Serial.print("cm");
Serial.println();
if (inches >16.00 && inches <=10.00) {
Keyboard.print ('h');
}
if (inches >17.00 && inches <=20.00) {
Keyboard.print ('j');
}
if (inches >21.00 && inches <=30.00) {
Keyboard.press(KEY_CAPS_LOCK);
Keyboard.print ('k');
}
delay(100);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
.
don't really know why this is the case the abs statements work fine in tests where I have used them with just one sensor also the pingpin 4 air therminin seems to print only k and j. To explain the function of the if statements using abs is to insure the sound is triggered only once. it is an audiosample of someone singing a long note so it being repeated qiukly would sound odd ; we assume people will not be too pedantic in their interaction with this piece and will break the beam at a point more than two inches away from the previous brake as it is really designed to trigger when people walk past.
alternately I would be very grateful if anyone could explain how to code a keyboard.press keyboard.release type functionality. I orginally wanted to use this instead of the value comparison abs methodolgy but found that statements like
if (inches >0.00 && inches <=20.00) {
Keyboard.press ('j');
}
if (inches >21.00 && inches <=30.00) {
Keyboard.release ('j');
would simply result in j being pressed down the first time the former condition was met and not being released when the latter condition was met? at first I thought this was a peculiarity particular to sonar (not a very considered opinion but it was the one I held).
but found the same problem with a reed switch , substituting inch ranges for LOW and HIGH? this would actually be preferable to getting the abs if statements to work so i would be very grateful to anyone who could point out what I'm doing wrong here.
cheers
sam