cfredisded
Active member
Hi all,
My goal with this sketch is to have two buttons. Holding one button while tapping on the other creates an array full of millisecond intervals. I can't for the life of me figure out why the array 'slot' starts on 2 instead of 1 and seems to switch between 2 and 3 instead of incrementing all the way to 40. I'm pretty new to arrays so I'm hoping its just something simple I'm overlooking. Any help would be much appreciated.
Pic of what the serial monitor looks like while holding rndmButton and pressing tapTEMPObutton.
https://imgur.com/HCMp6C7
My goal with this sketch is to have two buttons. Holding one button while tapping on the other creates an array full of millisecond intervals. I can't for the life of me figure out why the array 'slot' starts on 2 instead of 1 and seems to switch between 2 and 3 instead of incrementing all the way to 40. I'm pretty new to arrays so I'm hoping its just something simple I'm overlooking. Any help would be much appreciated.
Code:
#include <Bounce.h>
const int tapTEMPOPin = 49;
const int rndmBUTTONpin = 8;
bool rndmBUTTONheld;
unsigned long currentMILLIS;
unsigned long fallingEDGEtime;
unsigned long risingEDGEtime;
int currentPATTERNarraySLOT;
Bounce tapTEMPObutton = Bounce(tapTEMPOPin, 40); // 40 ms debounce
Bounce rndmBUTTON = Bounce(rndmBUTTONpin, 40); // 40 ms debounce
float tappedPATTERNarray[40];
int LFOled = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LFOled, OUTPUT);
pinMode(tapTEMPOPin, INPUT_PULLUP);
pinMode(rndmBUTTONpin, INPUT_PULLUP);
rndmBUTTONheld = false;
currentPATTERNarraySLOT = 1;
}
void loop() {
// put your main code here, to run repeatedly:
currentMILLIS = millis();
if (tapTEMPObutton.update()) {
if (tapTEMPObutton.fallingEdge()==true && rndmBUTTONheld == true) {
fallingEDGEtime = millis();
if (currentPATTERNarraySLOT > 1){
float MILLIStoPLACEinARRAYslot = fallingEDGEtime - risingEDGEtime;
tappedPATTERNarray[currentPATTERNarraySLOT] = MILLIStoPLACEinARRAYslot;
currentPATTERNarraySLOT = (currentPATTERNarraySLOT+1);
Serial.print(MILLIStoPLACEinARRAYslot);
Serial.print(" MILLIS ADDED TO SLOT ");
Serial.print(currentPATTERNarraySLOT);
Serial.println();
}
}
if (tapTEMPObutton.risingEdge()==true && rndmBUTTONheld == true) {
risingEDGEtime = millis();
if (currentPATTERNarraySLOT = 1 || currentPATTERNarraySLOT <= 39) {
float MILLIStoPLACEinARRAYslot = risingEDGEtime - fallingEDGEtime;
tappedPATTERNarray[currentPATTERNarraySLOT] = MILLIStoPLACEinARRAYslot;
currentPATTERNarraySLOT = (currentPATTERNarraySLOT+1);
Serial.print(MILLIStoPLACEinARRAYslot);
Serial.print(" MILLIS ADDED TO SLOT ");
Serial.print(currentPATTERNarraySLOT);
Serial.println();
}
}
}
//if array is more the 40 go back to slot 1
if (currentPATTERNarraySLOT > 40){
currentPATTERNarraySLOT = 1;
}
if (rndmBUTTON.update()) {
if (rndmBUTTON.fallingEdge()) {
rndmBUTTONheld = true;
currentPATTERNarraySLOT = 1;
}
if (rndmBUTTON.risingEdge()) {
rndmBUTTONheld = false;
//fill rest of array with zeros
for (int i= currentPATTERNarraySLOT; i < 40; i ++) {
tappedPATTERNarray[currentPATTERNarraySLOT] = 0;
currentPATTERNarraySLOT = currentPATTERNarraySLOT + 1;
}
}
}
}
Pic of what the serial monitor looks like while holding rndmButton and pressing tapTEMPObutton.
https://imgur.com/HCMp6C7