Tap Tempo

Status
Not open for further replies.

Antonis

Member
Hello ! I am trying to make a tap tempo with teensy 4 . I want to tap a momentary switch on pin 1 in order to control the blinking tempo of the led on pin 13 .I found this code but the tap() function is not declared . Could you help me please ? Thank you .

Code:
/*
   Tap Tempo clock
   Ronald B
   */


void setup()
{
 pinMode( 1, INPUT );   /* tap button - press it to set the tempo */
 pinMode( 13, 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 1, and only pay attention to the
    HIGH-LOW transition so that we only register when the
    button is first pressed down */
 int tapState = digitalRead( 1 );
 Serial.println(tapState);
 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() + timeoutTime;  /* this sets the time when LED 13 goes off */
   /* and reschedule the timer to keep the pace */
   rescheduleTimer();
 }
 

 if( millis() < indicatorTimeout )
   {
     digitalWrite( 13, HIGH );
     }
 else
  {
     digitalWrite( 13, 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! */
 Serial.println(lastTap);
}

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);
}
 
Your code compiled no problem for me, are you actually having a compile error or did I misunderstand?
 
Thank you very much for your reply and for taking time to check my project wecalvert. Yes today it just works ! I do not know why ? Yesterday I was trying to compile and I was receiving a message "map() is not declared". Thank you again !
 
Status
Not open for further replies.
Back
Top