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