// techDemo1-v2-5.ino - Batty Boop v2.5: Uses IR sensor to alter speed of // melody played via speaker. // RIP Batty Boop sonar T____T // Written by Jett (svaultz) // // F16 16-223 Intro to Physical Computing: Technical Demo 2 // =============== #include "smurfNotes.h" #define SPEAKER_PIN 5 #define SENSOR_PIN A0 #define BAUD_RATE 115200 // Tempo restrictions. float LONGEST = 500; // lowest melody duration reachable float SHORTEST = 31; // highest melody duration reachable // Distance restrictions. int MAX_READ = 540; int MIN_READ = 150; int input; // readings from Batty Boop 2.5 int curPos; // current position in the note sequence int curNote; // start at the beginning int curDur ; // corresponding beat length int MELODY_LEN = 22; // size of smurfs[] int tempo; // based on distance from sonar // determines speed of melody // =============== void setup() { Serial.begin(BAUD_RATE); input = analogRead(SENSOR_PIN); // Initialise speaker pin. pinMode(SPEAKER_PIN, OUTPUT); digitalWrite(SPEAKER_PIN, LOW); noTone(SPEAKER_PIN); tempo = map(constrain(input, MIN_READ, MAX_READ), MIN_READ, MAX_READ, LONGEST, SHORTEST); curPos = 0; // current position in the note sequence curNote = smurfs[curPos]; // start at the beginning curDur = (beats[curPos] * tempo); // corresponding beat length } // =============== void loop() { // Play a melody from the h-file, change the speed depending on the distance from // Batty Boop 2.5. // Melody progresses when Batty Boop senses something. // Seems get a good reading up until about 7 inches away, at which point the // input values decrease again. input = analogRead(SENSOR_PIN); // Debugging Serial.print(input); if (input > MIN_READ && input < MAX_READ) { // Play a sound if something is there. tempo = map(constrain(input, MIN_READ, MAX_READ), MIN_READ, MAX_READ, LONGEST, SHORTEST); // Update position. curPos = (curPos + 1) % MELODY_LEN; // cycle through note sequence curNote = smurfs[curPos]; curDur = (beats[curPos] * tempo); // Debugging. Serial.print("\t"); Serial.print(tempo); Serial.print("\t"); Serial.print(curPos); Serial.print("\t"); Serial.print(curNote); tone(SPEAKER_PIN, curNote, curDur); delay(curDur); Serial.println("\tsound!"); } else { // Debugging. Serial.print("\t"); Serial.print(tempo); Serial.print("\t"); Serial.print(curPos); Serial.print("\t"); Serial.print(curNote); Serial.println("\tsilence."); noTone(SPEAKER_PIN); delay(50); } delay(10); }