Why not eh? (Note the very fitting choice of book ;p)
The code is quite clunky but it looks like this:
// --------------------------------------------------------------------------- //This is a sketch which develops the NewPing with the range sensor and the arduino tone function for an 'air keyboard' in the key of C, starting with C4. //(code is reasonably long and clunky until I figure out how to simplify it!) //pitches.h includes a library for tuned frequencies for the tone generator
#include <NewPing.h> #include <pitches.h>
#define TRIGGER_PIN Â 8 Â // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN Â Â 7 Â // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int note1 = NOTE_C4;
int note2 = NOTE_D4;
int note3 = NOTE_E4;
int note4 = NOTE_F4;
int note5 = NOTE_G4;
int note6 = NOTE_A4;
int note7 = NOTE_B4;
int note8 = NOTE_C5;
void setup() { Â Serial.begin(115200); }
void loop(){ { Â delay(50); Â Â Â Â Â Â Â Â Â Â // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay 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"); }
{ while ((sonar.ping_cm() >= 2) &&(sonar.ping_cm() <=5)){ Â tone (12, note1); Â Serial.println("C4 is being played"); //send message to serial monitor and play the note C4 via tone function } } delay(25); // mandatory delay to prevent glitching between pings { while ((sonar.ping_cm() > 5 ) &&(sonar.ping_cm() <= 10)){ Â tone (12, note2); Â Serial.println("D4 is being played"); // same as above, one tone up } } delay(25); // same as previous { while ((sonar.ping_cm() > 10) && (sonar.ping_cm() <= 15)){ Â tone (12, note3); Â Serial.println("E4 is being played"); // same as above, one tone up } Â } delay(25); // same as previous { while ((sonar.ping_cm() > 15) && (sonar.ping_cm() <=20)){ Â tone (12, note4); Â Serial.println("F4 is being played"); // same as above, one tone up } Â } delay(25); // same as previous { while ((sonar.ping_cm() > 20) && (sonar.ping_cm() <= 25)){ Â tone (12, note5); Â Serial.println("G4 is being played"); // same as above, one tone up } Â } delay(25); // same as previous { while ((sonar.ping_cm() > 25) && (sonar.ping_cm() <=30)){ Â tone (12, note6); Â Serial.println("A5 is being played"); // same as above, one tone up } Â } delay(25); // same as previous { while ((sonar.ping_cm() > 30) && (sonar.ping_cm() <= 35)){ Â tone (12, note7); Â Serial.println("B5 is being played"); // same as above, one tone up } Â } delay(25); // same as previous { while ((sonar.ping_cm() > 35) && (sonar.ping_cm() <= 40)){ Â tone (12, note8); Â Serial.println("C5 is being played"); // same as above, one tone up } Â } delay(25); // same as previous { Â if (sonar.ping_cm() > 40) // stops tones when readings are above stated number in brackets, otherwise they play continuously when object is moved away (This can probably be fixed in the future!) Â noTone(12); } }








