Hello all, first post so hopefully i'm in the right place.
I am new to coding, i understand the basics but may have bitten off more than i can chew.
I want to use arduino (eventually an ATtiny85) to read the tempo coming from a tap tempo circuit, then apply a delay to that tempo so that i can output the same tempo but on the off beat. I hope that makes sense.
At the moment i am using some code from another forum, which is itself a tap tempo. I thought this would be the easiest route. Instead of reading a footswitch though i am reading a squarewave output. The code works at slow speeds but when the tempo reaches perhaps 4 pulses per second it starts going out of time and i can't work out why. I have an LED hooked up to so i can see the tempo. Is there something in this code which is causing the output to not exactly follow the input?
I actually don't need the button state display on pin 13 but don't want to delete it in case i screw something else up.
I can worry about getting the output on the off-beat later, for now i just want the output to follow the input precisely.
Thank you!
void setup()
{
pinMode( 12, INPUT_PULLUP ); /* /* tap button - press it to set the tempo */
pinMode( 13, OUTPUT ); /* button state display - not necessary */
for ( int i = 2 ; i < 12 ; i++ ) {
pinMode( i, OUTPUT ); /* tempo display light - shows the current tempo */
}
}
int lastTapState = LOW; /* the last tap button state */
unsigned long currentTimer[2] = { 500, 500 }; /* array of most recent tap counts */
unsigned long timeoutTime = 0; /* this is when the timer will trigger next */
unsigned long indicatorTimeout; /* for our fancy "blink" tempo indicator */
void loop()
{
/* read the button on pin 12, and only pay attention to the
HIGH-LOW transition so that we only register when the
button is first pressed down */
int tapState = digitalRead( 12 );
if ( tapState == LOW && tapState != lastTapState )
{
tap(); /* we got a HIGH-LOW transition, call our tap() function */
}
lastTapState = tapState; /* keep track of the state */
/* check for timer timeout */
if ( millis() >= timeoutTime )
{
/* timeout happened. clock tick! */
indicatorTimeout = millis() + 30; /* this sets the time when LED 13 goes off */
/* and reschedule the timer to keep the pace */
rescheduleTimer();
}
/* display the button state on LED 2 */
digitalWrite( 13, tapState );
/* display the tap blink on LED 13 */
for ( int i = 2 ; i < 12 ; i++ ) {
if ( millis() < indicatorTimeout ) {
digitalWrite( i, HIGH );
} else {
digitalWrite( i, LOW );
}
}
}
unsigned long lastTap = 0; /* when the last tap happened */
void tap()
{
/* we keep two of these around to average together later */
currentTimer[1] = currentTimer[0];
currentTimer[0] = millis() - lastTap;
lastTap = millis();
timeoutTime = 0; /* force the trigger to happen immediately - sync and blink! */
}
void rescheduleTimer()
{
/* set the timer to go off again when the time reaches the
timeout. The timeout is all of the "currentTimer" values averaged
together, then added onto the current time. When that time has been
reached, the next tick will happen...
*/
timeoutTime = millis() + ((currentTimer[0] + currentTimer[1]) / 2);
}