Go Down

Topic: TapTempo Startup (Read 80 times) previous topic - next topic

Leetut

May 23, 2020, 10:47 pm Last Edit: May 24, 2020, 11:38 am by Leetut
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
 
Code: [Select]

#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;
  //    }

aarg

#1
May 23, 2020, 10:50 pm Last Edit: May 23, 2020, 10:55 pm by aarg
Code: [Select]
          potchangerequired = HIGH;

HIGH and LOW are input/output states. For internal boolean logic you should use the predefined constants 'true' and 'false'. You may ask, "what does this have to do with my question?". It's because it makes your program easier to read and understand. That will encourage people to help you.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Leetut


Leetut

I think I need to change this to get the startup time in sync with the pot and blinking led
Code: [Select]
int default_delay = 500;
int current_ms = default_delay;
int current_ms_subdiv = current_ms * subdivisionfactor[arrayid];


tried and failed with
Code: [Select]
int default_delay =
oldVal
potVal
value
current_ms_subdiv
current_ms * subdivisionfactor[arrayid]
potval_ms
blinkfreq

int current_ms =
oldVal
potVal
value
current_ms_subdiv
current_ms * subdivisionfactor[arrayid]
potval_ms
blinkfreq


any ideas?

Leetut

int current_ms = read the pot input once at startup

Leetut

it mysteriously started booting up correctly
led now blinks @ 500ms at start up

Leetut

When I first upload my code things appear to work,
but if I remove power and reboot it doesn't work correctly (led flashes at 500ms correctly but digital pot is at its lowest setting)

Can anyone tell me how to get 500ms from the digipot at start up?

Code: [Select]

#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 for subdivision
const int subdivisionpin = A5;                                       // Pin of the button on the arduino
int readingsubdivisionbin = LOW;                                     // binary variable for state of subdivisionbutton
int subdivisionstate = LOW;                                          // the current reading from the input pin
int readingsubdivision;                                              // Variable to read Button
int lastsubdivisionstate = LOW;                                      // Variable for previous value of the button
int arrayid = 0;                                                     // will be counted up for indication of the current arry-value; during switch-on, the first value will be taken (usually no subdivision i.e. quarters)
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;   // How many different subdivfactors are used in the array above. /4, as array contains float numbers


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

//Variables 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()

  //############### NORMAL POT (DELAY TIME) ######################

  // IMPORTANT !!!!
  // don't forget to connect aref (Analog Reference) to VCC (or the max voltage value the pot is going to have)

  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;
  }
 // ############### BLINK according to blinkfreq ########################
  blinkfreq = current_ms;                                   // current_ms_subdiv / subdivisionfactor[arrayid];
  currentMillis = millis();
  digitalWrite(greenpin, currentgreen);
  analogWrite(redpin, currentred);
  analogWrite(bluepin, currentblue);
  if (currentMillis - blinkfreq > lastblinktoggle)
  {
    digitalWrite(ledpin, HIGH);
    lastblinktoggle = currentMillis;
  }

  if (currentMillis - lastblinktoggle > led_lit_time)
  {
    digitalWrite(ledpin, LOW);
  }

  // ############### WRITE TO DELAY-MCP AND MODULATE-MCP (POTCHANGEREQUIRED) ########################
  if (potchangerequired == HIGH)
  {
    for (int i = 0; i < 256; i = i + 5)                                               // The loop tries to find two values from the EEPROM, that "surround" current_ms
    {
      pot_low = i;                                                                    // pot_low will be the MCP-value [byte] with a lower delay time than current_ms (e.g. 50)
      pot_high = i + 5;                                                               // pot_high will be the MCP-value [byte] with a higher delay time than current_ms (e.g. 55)
      EEPROM_readAnything(i, pot_value_low);                                          // pot_value_low is the recorded delay time in [ms] for pot_low
      EEPROM_readAnything(i + 5, pot_value_high);                                     // pot_value_high is the recorded delay time in [ms] for pot_high
      if (current_ms_subdiv >= pot_value_low && current_ms_subdiv < pot_value_high)   // if "surrounding" values are found...
      {
        digipot_value = pot_low + (((current_ms_subdiv - pot_value_low) * 100) / (pot_value_high - pot_value_low) * (pot_high - pot_low)) / 100;                    // this is not ultra-precise, as some int-values are in the formula
        digitalPotWrite(slaveDelayPin, digipot_value);                                                                                                              // Write the value to MCP digital potentiometer
        potchangerequired = LOW;
      }
    }
  }
}    // END of LOOP()

Leetut

I added digitalPotWrite(slaveDelayPin, default_delay); to setup,
it seems to have fixed it

Go Up