-
Notifications
You must be signed in to change notification settings - Fork 0
/
solenoid_drum.ino
51 lines (30 loc) · 1.33 KB
/
solenoid_drum.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
byte BPM = 120; // beats per minute
// calculate how many milliseconds long a sixteenth note is at this tempo
long BeatDuration16 = round((60000 / BPM)/4);
long lastBeat = 0;
int sixteenths = 0;
const int ledPin = 13; // this is de pin for the LED onboard the Arduino
const int drum1 = 12; // this is the pin which connects to the solenoid
void setup() {
// wait for the button to be pressed before starting making noise
const byte buttonPin = 8;
pinMode(buttonPin, INPUT_PULLUP);
while (digitalRead(buttonPin) == HIGH);
pinMode(ledPin, OUTPUT); // define the LED pin as an output
pinMode(drum1, OUTPUT); // define the solenoid pin as an output
digitalWrite(drum1, LOW);
lastBeat = millis(); // start the timer
}
void loop() {
// only do something on every sixteenth note
if( (millis() - lastBeat) >= BeatDuration16 ) {
lastBeat = millis( );
// keep track of on which 16th note of the bar we are
if (++sixteenths > 16) { sixteenths = 1; }
// do something every quarter note (that's every beat)
if (!(sixteenths % 4)) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }
// turn the solenoid on on every other quarter note (so half as fast)
if ( sixteenths == 1 || sixteenths == 9 ) { digitalWrite(drum1, HIGH); }
else { digitalWrite(drum1, LOW); }
}
}