My tap tempo code works correctly apart from the first start up
led flashing speed is correctly determined by the analog input pot 100-1000ms
but my digipot output is always 500ms
how can I get the led to blink in sync with the 500ms default_delay at startup?
or make the default_delay read the analog input pot, which ever is easier
I tried removing the default_delay parts and adding the if startup = HIGH part to do this
it seemed to work but my tap input stopped working, and the led would randomly stop flashing
#include <SPI.h>
#include <EEPROM.h>
#include "EEPROMAnything.h" //...to be able to store values higher than 255
#define TOLERANCE 10
int oldVal = 0;
//Variables for RGB-LED
int bluepin = 10; //analogwrite enabled without PWM interference
int redpin = 3; //analogwrite enabled without PWM interference
int greenpin = 6;
int led_lit_time = 30;
int currentred = 55;
int currentgreen = LOW;
int currentblue = 0;
float colorarray[] = {55, LOW, 0, 0, HIGH, 0, 0, LOW, 55, 80, LOW, 100}; // Array with subdivision factors for 1/4, 1/8, triplets etc. Values as float
//Variables for the repeat rate of the delay
int digipot_value = 100; // Digipot Value (0-254)
const int min_delay = 100; // very short delays are not audible, increase min_delay to audible levels
const int max_delay = 1000; // longer PT2399 delays create artefacts, reduce the max_delay to prevent this
int default_delay = 500;
//Variables für subdivision
const int subdivisionpin = A5;
int readingsubdivisionbin = LOW;
int subdivisionstate = LOW;
int readingsubdivision;
int lastsubdivisionstate = LOW;
int arrayid = 0;
float subdivisionfactor[] = {1, 0.5, 0.75, 0.33}; // Array with subdivision factors for 1/4, 1/8, triplets etc. Values as float
const int number_of_subdivfactors = sizeof(subdivisionfactor) / 4;
int potVal = analogRead(A4); // A4 is where the wiper of the pot is connected
int potval_ms = map(potVal, 0, 1023, min_delay, max_delay);
int diff = abs(potVal - oldVal);
int current_ms = default_delay;
int current_ms_subdiv = current_ms * subdivisionfactor[arrayid];
/*
Table for factors you may use for subdivisionfactor
| .1/4 | 1/4 | 1/4T | .1/8 | 1/8 | 1/8T | .1/16 | 1/16 | 1/16T |
| 1.5 | 1 | 0.67 | 0.75 | 0.5 | 0.33 | 0.375 | 0.25 | 0.17 |
*/
//Variables for "determine pot value"
int value;
int pot_low;
int pot_high;
int pot_value_low;
int pot_value_high;
int interpolated_ms;
int potchangerequired = HIGH; // POT will only be accessed, when this value is HIGH. This reduces acoustic interference! Default is HIGH to change pot ONCE, when the system is rebooted
//int startup = HIGH;
//Variablen for tap-Button
const int tapbuttonpin = 12; // Pin of the button on the arduino
int tapbuttonstate = LOW; // the current reading from the input pin
int readingtapbutton; // Variable to read Button
int lasttapbuttonstate = LOW; // Variable for previous value of the button
//Variables for Tap-Tempo
long firsttapmillis = 0; // variable for the recorded first tap
long secondtapmillis = 0; // variable for the recorded second tap
long tappeddelta = 0; // delta of secondmillis and firstmillis
int firsttap = LOW; // will be toggled with every tap
//Variables for LED-blink-freq.
int ledpin = 2; // Pin for the LED
int blinkstate = LOW; // remember, what current the LED-state is
long lastblinktoggle; // variable to remember, when the last led-change happend
int blinkfreq; // Blink Frequency
const int slaveDelayPin = A0; // Pin that connects from Arduino to MCP digital Pot for DELAY
//Variables and constants for debounce (global)
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 5; // the debounce time; increase if the output flickers
//Other global variables
unsigned long currentMillis;
void setup()
{
// Serial.begin(9600); // You can activate this for debugging, but it influences the programm (e.g. the potentiometer changes may not be written, when this is activated)
SPI.begin();
pinMode (slaveDelayPin, OUTPUT);
pinMode(ledpin, OUTPUT); // Pin for the LED declared as output
pinMode(bluepin, OUTPUT); // Pin for Tap Tempo LED
pinMode(redpin, OUTPUT); // Pin for Tap Tempo LED
pinMode(greenpin, OUTPUT); // Pin for Tap Tempo LED
pinMode(tapbuttonpin, INPUT_PULLUP); // Pin for the downbutton declared as input
pinMode (subdivisionpin, INPUT); // Pin for the subdivision button declared as input, A5 is analog input w/o internal pullup
setPwmFrequency(redpin, 1);
setPwmFrequency(bluepin, 1);
lastblinktoggle = millis();
}
void loop()
//############### SUBDIVISION-BUTTON ##########################
{
readingsubdivision = analogRead(subdivisionpin); // read the state of the button into a local variable; analog read, as A5 on the 328p is analog only!
if (readingsubdivision > 1000)
{
readingsubdivisionbin = HIGH;
}
else
{
readingsubdivisionbin = LOW;
}
if (readingsubdivisionbin != lastsubdivisionstate)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (readingsubdivisionbin != subdivisionstate)
{
subdivisionstate = readingsubdivisionbin;
if (subdivisionstate == HIGH)
{
if (arrayid < number_of_subdivfactors - 1)
{
if (current_ms * subdivisionfactor[arrayid + 1] <= max_delay && current_ms * subdivisionfactor[arrayid + 1] >= min_delay)
{
arrayid = arrayid + 1;
}
}
else
{
if (current_ms * subdivisionfactor[0] <= max_delay && current_ms * subdivisionfactor[0] >= min_delay)
{
arrayid = 0;
}
}
if (current_ms * subdivisionfactor[arrayid] <= max_delay && current_ms * subdivisionfactor[arrayid] >= min_delay)
{
current_ms_subdiv = current_ms * subdivisionfactor[arrayid];
potchangerequired = HIGH;
currentred = colorarray[arrayid * 3];
currentgreen = colorarray[arrayid * 3 + 1];
currentblue = colorarray[arrayid * 3 + 2];
}
}
}
}
lastsubdivisionstate = readingsubdivisionbin; // save the reading to determine whether the button state has changed next time
//############### NORMAL POT (DELAY TIME) ######################
int potVal = analogRead(A4); // A4 is where the wiper of the pot is connected
int potval_ms = map(potVal, 0, 1023, min_delay, max_delay);
int diff = abs(potVal - oldVal);
if (diff > TOLERANCE)
{
current_ms = potval_ms;
current_ms_subdiv = current_ms * subdivisionfactor[arrayid];
// Serial.println(current_ms);
potchangerequired = HIGH;
oldVal = potVal;
}
// if (startup = HIGH)
// {
// current_ms = potval_ms;
// current_ms_subdiv = current_ms * subdivisionfactor[arrayid];
// potchangerequired = HIGH;
// oldVal = potVal;
// startup = LOW;
// }