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); } }
















