-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheremino.ino
125 lines (111 loc) · 2.42 KB
/
Theremino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
========================================================================================
Ultrassound Theremino - Andre Bahiense - May 2018
For this application you will need the Tone.h library, besides the comon Ultrassonic.h.
You can find Tone.h in: https://github.com/andrebahiense/PolyphonicPiano
And Ultrassonic.h you can add through the Arduino IDE.
Use it as you wish
=========================================================================================*/
#include <Ultrasonic.h>
#include <Tone.h>
#define trigger 4
#define echo 5
Ultrasonic ultrasonic(trigger, echo);
Tone tone1;
int flagb=0;
void setup() {
Serial.begin(9600);
tone1.begin(12);
float som = 0;
float ex=0;
pinMode(10, INPUT);
pinMode(9, OUTPUT); //Added a button to control the scale modes. Just put a button connected to pin 9 and pin 10.
}
void loop() {
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
digitalWrite(9, HIGH);
if(digitalRead(10) == HIGH && flagb == 0){
theremin();
if(digitalRead(10) == HIGH){
flagb=1;
}
}
else if(digitalRead(10) == HIGH && flagb==1){
pentatonic();
if(digitalRead(10) == HIGH){
flagb=2;
}
}
else if(digitalRead(10) == HIGH && flagb==2){
harmonic();
if(digitalRead(10) == HIGH){
flagb=0;
}
}
}
//Glissando scale
void theremin(){
float ex;
float cmMsec;
float som;
ex = pow(0.6104*cmMsec,2);
som = ex + 9.3886*cmMsec+ 215.11;
if(som >= 130 && som <= 2000){
tone1.play(som);
}
else{
tone1.stop();
}
}
//Pentatonic scale
void pentatonic(){
float cmMsec;
if(cmMsec>=4 && cmMsec<9){
tone1.play(262);
}
else if(cmMsec>9 && cmMsec<14){
tone1.play(311);
}
else if(cmMsec>14 && cmMsec<19){
tone1.play(349);
}
else if(cmMsec>19 && cmMsec<24){
tone1.play(392);
}
else if(cmMsec>24 && cmMsec<29){
tone1.play(466);
}
else if(cmMsec>29 && cmMsec<34){
tone1.play(523);
}
else{
tone1.stop();
}
}
//Harmonic scale
void harmonic(){
float cmMsec;
if(cmMsec>=4 && cmMsec<9){
tone1.play(220);
}
else if(cmMsec>9 && cmMsec<14){
tone1.play(330);
}
else if(cmMsec>14 && cmMsec<19){
tone1.play(440);
}
else if(cmMsec>19 && cmMsec<24){
tone1.play(550);
}
else if(cmMsec>24 && cmMsec<29){
tone1.play(660);
}
else if(cmMsec>29 && cmMsec<34){
tone1.play(770);
}
else{
tone1.stop();
}
}