Arduinemin - An Arduino Theremin
A theremin-esque project that uses an ultrasonic sensor to play music.
Components and supplies
1
Ultrasonic Sensor
1
10kOhm potentiometer
1
Piezo Buzzer
1
Breadboard 100x160
2
IR Obstacle Sensor
Tools and machines
1
Box
1
Box Cutter
Apps and platforms
1
Arduino IDE
Project description
Code
Code
cpp
code
1int C = 262; 2int D = 294; 3int E = 330; 4int F = 349; 5int G = 392; 6int A = 440; 7int B = 494; 8int C1 = 523; 9 10int trigPin = 9; 11int echoPin = 11; 12int flatPin = 7; 13int sharpPin = 13; 14int buzzer = 10; 15int potentimoter = A4; 16 17float duration, distance; 18 19void setup() { 20 pinMode(trigPin, OUTPUT); 21 pinMode(echoPin, INPUT); 22 pinMode(flatPin, INPUT); 23 pinMode(sharpPin, INPUT); 24 pinMode(buzzer, OUTPUT); 25 pinMode(potentimoter, INPUT); 26 Serial.begin(9600); 27} 28 29int prevNote = 1; 30 31void loop() { 32 int note = findNote(prevNote); 33 Serial.println(note); 34 35 int freq = getNote(note); 36 //Serial.println(freq); 37 38 int semitone = semiTone(freq); 39 //Serial.println(semitone); 40 41 int range = findRange(); 42 //Serial.println(range); 43 44 int octave = convertRange(range, semitone); 45 Serial.println(octave); 46 47 if (note != 0) { 48 tone(buzzer, octave); 49 } else { 50 noTone(buzzer); 51 } 52 53 prevNote = note; 54 55 Serial.println(""); 56 delay(100); 57} 58 59int findNote(int prevNote) { 60 digitalWrite(trigPin, LOW); 61 delayMicroseconds(2); 62 digitalWrite(trigPin, HIGH); 63 delayMicroseconds(10); 64 digitalWrite(trigPin, LOW); 65 66 duration = pulseIn(echoPin, HIGH); 67 distance = (duration * (0.0135039) / 2) / 2; 68 Serial.println(distance); 69 70 float low = prevNote - 0.1; 71 float high = prevNote + 0.1; 72 73 int note = 0; 74 75 if (distance >= 50) { 76 distance = 9; 77 } 78 79 if ((distance < high) && ((distance + 1) > low)) { 80 note = prevNote; 81 return note; 82 } else { 83 if ((distance < 8) && (distance > 0)) { 84 note = int(distance) + 1; 85 return note; 86 } else { 87 return 0; 88 } 89 } 90} 91 92int getNote(int num) { 93 int note = 0; 94 if (num == 1) { 95 note = C; 96 } else if (num == 2) { 97 note = D; 98 } else if (num == 3) { 99 note = E; 100 } else if (num == 4) { 101 note = F; 102 } else if (num == 5) { 103 note = G; 104 } else if (num == 6) { 105 note = A; 106 } else if (num == 7) { 107 note = B; 108 } else if (num == 8) { 109 note = C1; 110 } 111 return note; 112} 113 114int semiTone(int note) { 115 if ((digitalRead(sharpPin) == LOW) && (digitalRead(flatPin) == LOW)) { 116 note = note; 117 } else if (digitalRead(sharpPin) == LOW) { 118 note *= 1.06; 119 } else if (digitalRead(flatPin) == LOW) { 120 note *= (1 / 1.06); 121 } else { 122 note = note; 123 } 124 return note; 125} 126 127int findRange() { 128 int ana = analogRead(A4); 129 int range = map(ana, 0, 255, 0, 2); 130 return range; 131} 132 133int convertRange(int range, int freq) { 134 if (range <= 2) { 135 freq /= 2; 136 } else if (range <= 5) { 137 freq = freq; 138 } else if (range <= 8) { 139 freq *= 2; 140 } 141 return freq; 142}
Comments
Only logged in users can leave comments