// Set pin number for kick drum
const int kickPin = 2;
// Set pin number for tempo potentiometer
const int tempoPotPin = A0;
// Set number of beats per measure
const int beatsPerMeasure = 16;
// Set time signature (beats per measure, note value)
const int timeSignature[2] = {beatsPerMeasure, 16};
// Set pattern of beats for one measure (0 = rest, 1 = kick drum)
const int beatPattern[] = {1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1};
// Set current beat and measure
int currentBeat = 0;
int currentMeasure = 0;
void setup() {
// Set kick drum pin as output
pinMode(kickPin, OUTPUT);
// Set tempo potentiometer pin as input
pinMode(tempoPotPin, INPUT);
}
void loop() {
// Read tempo from potentiometer and calculate beat duration
int tempoPotValue = analogRead(tempoPotPin);
int tempo = map(tempoPotValue, 0, 1023, 60, 200);
int beatDuration = 30000 / tempo;
// Check if it's time to play the next beat
if (millis() % beatDuration == 0) {
// Play the kick drum if the beat pattern is 1
if (beatPattern[currentBeat] == 1) {
digitalWrite(kickPin, HIGH);
delay(50);
digitalWrite(kickPin, LOW);
}
// Move to the next beat
currentBeat++;
if (currentBeat >= timeSignature[0]) {
currentBeat = 0;
currentMeasure++;
if (currentMeasure >= 16) { // change here to change the number of measures per loop
currentMeasure = 0;
}
}
}
// Delay a short amount of time to prevent overwhelming the loop
delay(1);
}