const int REFRESH_INTERVAL = 8;
int button = D0;
int speaker = D2;
Servo bpmDial;
int potentiometer = A0;
int milliValue = 0;
int inputCounter = 0;
int averagePace = 0;
int totalCount = 0;
int loopTime = 0;
bool aboveThreshold = false;
int startingMillis = 0;
int timeSignature = 0;
int bpmInterval = 0;
// michael marsico attempt
bool buttonDown = false;
int lastMillis = 0;
int numberOfInputs = 0;
int totalMillis = 0;
const int threshold = 8;
// michael 2
int lastPlayedSound = 0;
int soundsPlayed = 0;
void setup() 
{
    pinMode(button, INPUT_PULLUP);
    pinMode(speaker, OUTPUT);
    pinMode(potentiometer, INPUT);
    bpmDial.attach(1);
  Serial.begin(9600); // Initialize serial communication for debugging (optional)
}
//MAKE SURE TO IMPLEMENT 8 COUNT RESET SYSTEM
void loop() 
{
    unsigned long currentMillis = millis();
    
    if(soundsPlayed >= 8)
    {
        soundsPlayed = 0;
        totalMillis = 0;
        numberOfInputs = 0;
        lastMillis = 0;
    }
    
    int buttonValue = digitalRead(button);
    if (!buttonDown && buttonValue == LOW) 
    {
        if (lastMillis == 0) 
        {
            lastMillis = currentMillis;
        } 
        else 
        {
            int difference = currentMillis - lastMillis;
            totalMillis += difference;
            numberOfInputs += 1;
            averagePace = totalMillis / numberOfInputs;
            Particle.publish("average pace", String(averagePace));
            lastMillis = currentMillis;
        }
    }
    buttonDown = buttonValue == LOW ? true : false;
    
    if(millis() >= bpmInterval + 250.0)
    {
        if(bpmInterval == 0)
        {
            bpmDial.write(0);
            bpmInterval = millis();
        }
        else
        {
            int bpm = (60000 / averagePace);
            Particle.publish("bpm", String(bpm));
            bpmDial.write(180 - (bpm / 2));
            bpmInterval = millis();
        }
    }
    
    int potValue = analogRead(potentiometer);
    if(potValue >= 2800)
    {
        if (averagePace > 100 && currentMillis > lastPlayedSound + averagePace) 
        {
            int pitch = timeSignature % 3 == 0 ? 1000 : 250;
            if(pitch == 1000)
            {
                timeSignature = 0;
            }
            playTone(pitch, 50); // Play a 1000 Hz tone for 50 ms
            timeSignature += 1;
            lastPlayedSound = currentMillis;
            soundsPlayed++;
        }
    }
    else if(potValue >= 1300 && potValue < 2800)
    {
        if (averagePace > 100 && currentMillis > lastPlayedSound + averagePace) 
        {
            int pitch = timeSignature % 4 == 0 ? 1000 : 250;
            if(pitch == 1000)
            {
                timeSignature = 0;
            }
            playTone(pitch, 50); // Play a 1000 Hz tone for 50 ms
            timeSignature += 1;
            lastPlayedSound = currentMillis;
            soundsPlayed++;
        }
    }
    else
    {
        if (averagePace > 100 && currentMillis > lastPlayedSound + averagePace) 
        {
            int pitch = timeSignature % 5 == 0 ? 1000 : 250;
            if(pitch == 1000)
            {
                timeSignature = 0;
            }
            playTone(pitch, 50); // Play a 1000 Hz tone for 50 ms
            timeSignature += 1;
            lastPlayedSound = currentMillis;
            soundsPlayed++;
        }
    }
    
    
}
void playTone(int frequency, int duration) {
    int period = 1000000 / frequency; // Calculate period in microseconds
    int pulseWidth = period / 2; // 50% duty cycle
    long endTime = millis() + duration; // Calculate end time
    while (millis() < endTime) {
        digitalWrite(speaker, HIGH); // Turn on the speaker
        delayMicroseconds(pulseWidth); // Keep it on for half the period
        digitalWrite(speaker, LOW); // Turn off the speaker
        delayMicroseconds(pulseWidth); // Keep it off for half the period
    }
}
Comments