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






