Last active
November 20, 2019 14:22
-
-
Save michaelforrest/b9a81a6049161f28315e3778ddd5bbf7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const int triggerDuration = 20; // how long the pulses last in ms | |
| float bpm = 120.0; // change this value byr rotating the tempo encoder | |
| int startTime = millis(); // can be reset to millis() if a reset button is pressed | |
| // set up your pins here | |
| #define everyBeatPin 3 | |
| #define everyFourBeatsPin 4 | |
| #define quarterBeatPin 5 | |
| #defined sixteenthBeatPin 6 | |
| void setup() { | |
| pinMode(everyBeatPin, OUTPUT); | |
| pinMode(everyFourBeatsPin, OUTPUT); | |
| pinMode(quarterBeatPin, OUTPUT); | |
| pinMode(sixteenthBeatPin, OUTPUT); | |
| } | |
| // call this when the reset button is pressed | |
| void resetClock(){ | |
| startTime = millis(); | |
| } | |
| void loop() { | |
| int elapsedMillis = millis() - startTime; | |
| float beatsPerSecond = bpm / 60.0; | |
| int millisecondsPerBeat = int(1000.0 / beatsPerSecond); | |
| int millisecondsInFourBeats = millisecondsPerBeat * 4; | |
| int millisecondsPerQuarterBeat = millisecondsPerBeat / 4; | |
| int millisecondsPerSixteenthBeat = millisecondsPerBeat / 16; | |
| bool isOnBeat = elapsedMillis % millisecondsPerBeat < triggerDuration; | |
| bool isOnFourthBeat = elapsedMillis % millisecondsInFourBeats < triggerDuration; | |
| bool isOnQuarterBeat = elapsedMillis % millisecondsPerQuarterBeat < triggerDuration; | |
| bool isOnSixteenthBeat = elapsedMillis % millisecondsPerSixteenthBeat < triggerDuration; | |
| digitalWrite(everyBeatPin, isOnBeat ? HIGH : LOW); | |
| digitalWrite(everyFourBeatsPin, isOnFourthBeat ? HIGH : LOW); | |
| digitalWrite(quarterBeatPin, isOnQuarterBeat ? HIGH : LOW); | |
| digitalWrite(sixteenthBeatPin, isOnSixteenthBeat ? HIGH : LOW); | |
| updateVisuals(100, isOnBeat); | |
| updateVisuals(25, isOnQuarterBeat); | |
| // etc... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment