-
Notifications
You must be signed in to change notification settings - Fork 0
/
Musical Instrument.ino
57 lines (48 loc) · 1.81 KB
/
Musical Instrument.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
int analogSensorValue;
bool prevSwitchState = false;
int outputPattern=0; // different sound making modes
int frequency = 0; // pitch
int bps = 1; // Beat per second
long previousMillis = 0; // signed long to store last time when made sound
int piezo = 8;
int switchSensor = A0;
int potentiometer = A2;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read values from potentiometer
int sensorValue = analogRead(potentiometer)/5*5; // <- to mitigate unintended frequency change caused by the sensor
delay(1); // delay in between reads for stability
frequency = map(sensorValue,0,1024,0,2000); // limit frequency range to avoid unpleasant sound
manageOutputPattern(); // determines which sound mode will be played
if (!!outputPattern) { // if outputPattern is 0, idle(). Else make sound.
makeSound();
} else {
idle();
}
}
void manageOutputPattern() { // button press -> change output pattern
bool switchState = digitalRead(switchSensor);
if (switchState && !prevSwitchState) { // !prevSwitchState prevent undesirable changes
Serial.println(outputPattern);
outputPattern = (outputPattern + 1) % 6;
bps = pow(2,outputPattern+1); // 2 4 8 16 32 bps
}
prevSwitchState = switchState;
}
void makeSound() {
unsigned long currentMillis = millis();
// to calculate the note duration, take one second divided by a given bps value.
int cycleLength = 1000 / bps; // <- cycle length = tone duration + rest
if (currentMillis - previousMillis > cycleLength) {
previousMillis = currentMillis;
tone(piezo, frequency, cycleLength/1.3); // Similar to "ToneMelody" Example, delay is *1.3
}
}
void idle() {
noTone(piezo); // turn off sound immediately
delay(50);
}