Tap tempo led speed reversed

the sketch prints correct bpm to serial monitor
but if i tap fast the leds blink slow and vice versa
anyone know how to fix it?

// fix, if tapped tempo is fast, led blinks slow & vice versa
// make runSeq start/stop from start/stop fsw

#include "TapTempoButton.h"
#include "Adafruit_NeoTrellis.h"

// ----------------------------------------FSW NEOPIXELS---------------------------------------------
#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN  5 // (Led Pin)
#define PIXEL_COUNT   2
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// ----------------------------------------FSW NEOPIXELS---------------------------------------------

#define TAP_TEMPO_BUTTON_PIN    3
#define LED_COUNT 16

TapTempoButton tapTempo;
Adafruit_NeoTrellis trellis;

int ssLedState = -1; //this variable tracks the startStop LED, negative if off, positive if on
int ttLedState = LOW; // ledState used to set the tempo LED
int pad = -1;
int FSW2State = HIGH;
const int FSW2 = 2;  // Start/stop footswitch
const long interval = 100; // interval at which to blink (milliseconds) approx 80 bpm
unsigned long previousMillis = 0; // will store last time LED was updated
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 500;    // the debounce time; increase if the output flickers


void setup() {
  pinMode(FSW2, INPUT_PULLUP);
  trellis.begin();
  trellis.pixels.setBrightness(50);
  tapTempo.begin(TAP_TEMPO_BUTTON_PIN);
  Serial.begin(9600);
  pixels.begin(); // for start/stop and tempo LEDs
  pixels.setBrightness(50);
  pixels.setPixelColor(1, 220, 20, 60);
  pixels.show();
}




void loop() {
  startStopFSW();
  runSeq();
  if (tapTempo.hasTempoChanged()) {
    Serial.print("New tempo: ");
    Serial.println(tapTempo.getTempo());
  }
}



void runSeq() {

  unsigned long currentMillis = millis();
  //if (currentMillis - previousMillis >= interval) {
  if (currentMillis - previousMillis >= tapTempo.getTempo()) {
    previousMillis = currentMillis;     // save the last time you blinked the LED

    if (ttLedState == LOW) {     // if the LED is off turn it on and vice-versa:
      ttLedState = HIGH;
    } else {
      pad++;
      pad %= LED_COUNT;
      trellis.pixels.setPixelColor(pad, 0, 255, 255); // led on
      trellis.pixels.show();
      pixels.setPixelColor(0, 0, 255, 255); // Tempo LED on aqua
      pixels.show();
      ttLedState = LOW;
    }
    trellis.pixels.setPixelColor(pad, 0); // led off
    pixels.setPixelColor(0, 0, 0, 0);  // Tempo LED off
    pixels.show();
  }
}




void startStopFSW() {
  FSW2State = digitalRead(FSW2);   //sample the state of the button - is it pressed or not?

  if ( (millis() - lastDebounceTime) > debounceDelay) {   //filter out any noise by setting a time buffer
    if ( (FSW2State == LOW) && (ssLedState < 0) ) {     //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
      pixels.setPixelColor(1, 124, 252, 0);       // start LED lawn green:
      pixels.show();
      Serial.println("Start");
      ssLedState = -ssLedState; //now the LED is on, we need to change the state
      lastDebounceTime = millis(); //set the current time
    }
    else if ( (FSW2State == LOW) && (ssLedState > 0) ) {
      pixels.setPixelColor(1, 220, 20, 60);       // stop LED crimson
      pixels.show();
      Serial.println("Stop");
      ssLedState = -ssLedState; //
      lastDebounceTime = millis(); //set the current time
    }

  }//close if(time buffer)
}
#include "Button.h"

Button::Button() :
  lastReadState(LOW),
  currentState(LOW),
  pin(3),
  timeButtonChange(0) {
  
}

void Button::begin(byte pin) {  
  pinMode(pin, INPUT_PULLUP);
  byte st = digitalRead(pin);
  this->lastReadState = st;
  this->currentState = st;
  this->pin = pin;
}

bool Button::hasBeenPressed() {
  byte newState = digitalRead(this->pin);
  bool returnVal = false;
  unsigned long currentTime = millis();
  if (newState != this->lastReadState) { // has changed - because of noise or because the button is being pressed
    this->timeButtonChange = currentTime;
    this->lastReadState = newState;
  }
  else if ((currentTime - this->timeButtonChange) > long(BUTTON_DEBOUNCE_TIME)) { // state has been stable for x ms
    if (this->currentState == HIGH && newState == LOW) // HIGH to LOW = pressed (with pullup resistor)
      returnVal = true;
    this->currentState = newState;
  }
  return returnVal;
}
#ifndef __Button_h__
#define __Button_h__

#include <Arduino.h>

#define BUTTON_DEBOUNCE_TIME   30   // x ms after button has changed to avoid double clicks


class Button {
  private:
    byte pin;
    byte lastReadState;
    byte currentState;
    unsigned long timeButtonChange;

  public:
    Button();
    void begin(byte pin); 
    bool hasBeenPressed(); // has to be call regularly to check the button status
};

#endif
#include "TapTempoButton.h"


TapTempoButton::TapTempoButton() :
    tempo(80),
    lastTapTime(0),
    tapTimes{0} {
     
}

word TapTempoButton::getTempo() {
  return this->tempo;
}

bool TapTempoButton::hasTempoChanged() { // to call once per loop()
  if (!this->hasBeenPressed())
    return false;

  unsigned long now = millis();
  if (now - this->tapTimes[this->lastTapTime] > TAPTEMPO_TIMEBETWEEN) { // start over
    this->tapTimes[0] = now;
    this->lastTapTime = 0;
    return false;
  }
 
  // has been tapped at least once
  if (this->lastTapTime < TAPTEMPO_AMOUNT_OF_TAPS-1) {
    this->lastTapTime++;
    this->tapTimes[this->lastTapTime] = now;
  }
  else { // we have TAPTEMPO_AMOUNT_OF_TAPS taps, we need to shift all the values
    for (byte i = 1 ; i <= this->lastTapTime ; i++)
      this->tapTimes[i-1] = this->tapTimes[i];
    this->tapTimes[this->lastTapTime] = now;
  }

  word timeDifferences = 0;
  for (byte j = 1 ; j <= this->lastTapTime ; j++)
    timeDifferences += this->tapTimes[j] - this->tapTimes[j-1];

  this->tempo = (60000*this->lastTapTime) / timeDifferences; // timeDiff / lastTapTime = average of the time diff between taps
  if (this->tempo > TAPTEMPO_MAXTEMPO)
    this->tempo = TAPTEMPO_MAXTEMPO;
  return true;
}
#ifndef __TapTempoButton_h__
#define __TapTempoButton_h__

#include "Button.h"

#define   TAPTEMPO_AMOUNT_OF_TAPS       3     // tempo will be averaged over x taps
#define   TAPTEMPO_TIMEBETWEEN          2000  // in ms  (2s => 0.5bps = 30bpm is the min tempo)
#define   TAPTEMPO_MAXTEMPO             400   // bpm

class TapTempoButton : public Button {
  private:
    word tempo;
    byte lastTapTime;
    unsigned long tapTimes[TAPTEMPO_AMOUNT_OF_TAPS];

  public:
    TapTempoButton();
    bool hasTempoChanged(); // has to be call regularly to check the button status
    word getTempo();
};

#endif

Code originally used interval=100 to set the startup blink time, increasing that value makes the LEDs blink slower, explains why when I tap a higher bpm LEDs blink slower

Think I need to convert bpm into something before
if (currentMillis - prevMillis = something)

You are using a frequency where a period is warranted.

There is a plausible method that returns the period, use that in your millis() timing mechanism

    timeBetweenBeats = tapTempo.getBeatLength();

Declare timeBetweenBeats as unsigned long.

HTH

a7

error: 'class TapTempoButton' has no member named 'getBeatLength'
unsigned long timeBetweenBeats = tapTempo.getBeatLength();

  unsigned long currentMillis = millis();
  unsigned long timeBetweenBeats = tapTempo.getBeatLength();

  if (currentMillis - previousMillis >= timeBetweenBeats) {
    previousMillis = currentMillis;     // save the last time you blinked the LED

Sry, I found without noticing a different tap tempo library.

Please post a link to the exact library you are using.

You posted a few files. One must be your code, but if the others are from libraries or a library, you needn't have posted them.

So where did those additional files come from? It might help if you posted their names as well.


Now if you stick with what you have, you should be able to mathematically turn a frequency into a period.

If N is beats per minute, then N/60.0 is beats per second, and 1000/(N/60.0) should be the period in milliseconds.

BPM period in ms
40 1500
80 750
120 500
200 300

Like:
    timeBetweenBeats = 1000.0 * (tapTempo.getTempo() / 60.0);

Declare timeBetweenBeats as unsigned long.

HTH

a7

that just slowed the blinking, still blinks in reverse, tap fast = blinks slow, tap slow = blinks fast
i am not using a library, just those files in the sketch folder
Main.ino
Button.h
Button.cpp
TapTempoButton.h
TapTempoButton.cpp

void runSeq() {

  unsigned long currentMillis = millis();
  unsigned long timeBetweenBeats = 1000.0 * (tapTempo.getTempo() / 60.0);

  if (currentMillis - previousMillis >= timeBetweenBeats) {
    previousMillis = currentMillis;     // save the last time you blinked the LED

    if (ttLedState == LOW) {     // if the LED is off turn it on and vice-versa:
      ttLedState = HIGH;
    } else {
      pad++;
      pad %= LED_COUNT;
      trellis.pixels.setPixelColor(pad, 0, 255, 255); // led on
      trellis.pixels.show();
      ttLedState = LOW;
      
      //if (pad == 0 || 4) { // or doesnt work?
        pixels.setPixelColor(0, 0, 255, 255); // Tempo LED on aqua
        pixels.show();
      //}
    }
    trellis.pixels.setPixelColor(pad, 0); // led off
    pixels.setPixelColor(0, 0, 0, 0);  // Tempo LED off
    pixels.show();
  }
}

Sry, I wrote the equation wrong, and obvsly you used it without the least critical eye or understanding, so neither did you catch the error.

Try

    timeBetweenBeats = 1000.0 / (tapTempo.getTempo() / 60.0);

also, I would be interested in the numbers returned by the getTempo() method. Print some out and let's see if they are plausible and are in fact in terms of BPM at all corresponding to the tapping rate.

Where did all those other files come from? Did you take them away from a library, if so why?

a7

it worked but was half speed so i added *2 to the end, perfect now thank you,
cant remember where i got the sketch from, other files were already included with it, getTempo() returns bpm

Haha, half speed maybe because... you have the LED on for the entire period (timeBetweenBeats) it should only be half on for which.

So 2* (if you put it in the right place, seems like divided by 2 makes more sense) is a fix for sure.

a7

Original sketch is from here