Open L4g0 opened 6 years ago
Done! ... (?) I used this library (https://github.com/dxinteractive/ArduinoTapTempo) and a button. It works when the metronome stops...
It works ONLY if metronome is stopped? Maybe you're using global delays for metronome, so the entire board is halted all time and can't check for button presses.
El sáb., 14 abr. 2018 2:40, Tomás Criado notifications@github.com escribió:
Done! ... (?) I used this library (https://github.com/dxinteractive/ArduinoTapTempo) and a button. It works when the metronome stops...
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/UC3Music/MetroGnome/issues/3#issuecomment-381290887, or mute the thread https://github.com/notifications/unsubscribe-auth/AKgbgABp3OT82VAFi47Tp9xlhED-tzYQks5toUV6gaJpZM4TRnSx .
Things to check out to solve this (proposed in our last meeting):
Instead of using delay(), use millis() to keep track of the elapsed time. This way the arduino won't be blocked and you will be able to check the buttons. If the period of the tempo is 7ms (140 bpm), instead of using delay(7) to wait until the next measure, store the millis() value of the last measure and compare it to the elapsed time returned by millis():
time = millis();
while(true) // or another condition
{
if (millis()-time = 7)
{
time += 7; // update current measure time
// blink or beep or whatever
}
// do other stuff here, such as checking the buttons
}
The other option, more efficient but also more complex is to use the timers to automatically keep the beat without using the cpu. Just get the period required for a given tempo and set is as the timer's period. In the same line as this, one could get the tap by using interruptions (checking the millis() for each interruption and subtracting them).
Little correction about @David-Estevez's last comment:
Things to check out to solve this (proposed in our last meeting):
Instead of using delay(), use millis() to keep track of the elapsed time. This way the arduino won't be blocked and you will be able to check the buttons. If the period of the tempo is 7ms (140 bpm), instead of using delay(7) to wait until the next measure, store the millis() value of the last measure and compare it to the elapsed time returned by millis():
bpm = 60/T (T in seconds)
So at 140bpm => T = 0,4285 s = 428,5 ms
So just replace the "7"s with 428. This doesn't affect the algorithms, but may cause some confusion.
You are absolutely right, my bad 😅
I did the next code in order to check the propose idea and I think that it's works well.
#define LED 10 // LED pin
unsigned long time = 0;
int bpm = 40;
void setup(){
Serial.begin(9600);
pinMode(LED, OUTPUT);
time = millis();
}
void loop(){
if (millis()-time == 6000/bpm)
{
digitalWrite(LED, LOW);
}
if (millis()-time == (54000/bpm))
{
digitalWrite(LED, HIGH);
Serial.print(bpm);
Serial.println(" BPM Click!");
time = millis(); // update current measure time
}
// do other stuff here, such as checking the buttons
}
The next step is redesign the main part of the code and implement that in the loop ir order to test the buttons all the time.
If you see any problem or suggestion, tell me.
This could be done with a piezo sensor and a few diodes (to avoid damage to the Arduino). We have some piezos available in UC3Music, and I can give you some more diodes if you need them.
This is quite similar to Shiva, so if you have any doubts please ask! :smile: