LED sequencer using multiplexer with millis() instead of delay()

Hello!

I am trying to build an 8 step sequencer and I have to use multiplexers. I can't figure out why I can't replace the delay() function with the millis() function which determines the speed of the sequence. If I run my code, all the LEDs turn on.

Sequencer.cpp:

#include "Sequencer.h"
#include "Display.h"

Sequencer::Sequencer() : ADCFilter(60, 0),
                         Enc1(17, 18),
                         patchCord1(waveform[1], envelope[1]),
                         patchCord2(waveform[2], envelope[2]),
                         patchCord3(waveform[3], envelope[3]),
                         patchCord4(waveform[0], envelope[0]),
                         patchCord5(envelope[3], 0, mixer1, 3),
                         patchCord6(envelope[2], 0, mixer1, 2),
                         patchCord7(envelope[0], 0, mixer1, 0),
                         patchCord8(envelope[1], 0, mixer1, 1),
                         patchCord9(mixer1, 0, i2s1, 0),
                         patchCord10(mixer1, 0, i2s1, 1)

{

    for (int i{0}; i < 4; ++i)
    {
        envelope[i].attack(attack[i]);
        envelope[i].decay(decay[i]);
        envelope[i].sustain(sustain[i]);
        envelope[i].release(release[i]);
    }
    AudioMemory(20);
    sgtl5000_1.enable();
    sgtl5000_1.volume(defaultVolume);
    attack.fill(defaultDecayValue);
    decay.fill(defaultDecayValue);
    sustain.fill(defaultSustainValue);
    release.fill(defaultSustainValue);
    amplitude.fill(defaultVolume);

    for (unsigned int i{0}; i < 4; ++i)
    {
        mixer1.gain(i, defaultVolume);
        waveform[i].begin(WAVEFORM_ARBITRARY); // WAVEFORM_SINE expands to 0 WAVEFORM_ARBITRARY expands to 4
        waveform[i].amplitude(amplitude[i]);
        waveform[i].frequency(frequency[i]);
        pinMode(m_stepLEDPin[i], OUTPUT);
        pinMode(m_stepButtonPin[i], INPUT_PULLUP);
    }
}

float Sequencer::getBPMInterval()
{
    pointerToAnalogValues = analogPotiReader();
    //   Serial.println(*(pointerToAnalogValues + 1));
    int RawValue = *pointerToAnalogValues;
    ADCFilter.Filter(RawValue);
    if ((millis() - m_lastMillis) > m_BPMInterval && ADCFilterBefore != ADCFilter.Current() && (ADCFilterBefore - ADCFilter.Current() > m_AnalogThreshold))
    {
        m_lastMillis = millis();
        ADCFilterBefore = ADCFilter.Current();
    }

    // subtract the last reading:
    total = total - readings[readIndex]; // Alle analoge Inputwerte zusammenaddiert, wobei der Wert der neuen Loop abgezogen wird (zieht den nullten Wert ab)
    // read from the sensor:
    readings[readIndex] = RawValue;

    // add the reading to the total:
    total = total + readings[readIndex];
    // advance to the next position in the array:
    readIndex = readIndex + 1;

    if (readIndex >= numReadings)
    {
        // ...wrap around to the beginning:
        readIndex = 0;

        // calculate the average:
        average = total / numReadings;
        average_bpm = mapper(average, 1.0, 1023.0, minBPM, maxBPM);
    }
    if (average_bpm != average_bpm_alt)
    {
        average_bpm_alt = average_bpm;
    }
    float m_interval = (60.0 / average_bpm) * 1000.0;
    return m_interval;
}

unsigned int Sequencer::counter(float interval) // Returns the current position
{
    if ((millis() - m_lastMillis) > getBPMInterval()) // m_interval = 1000 ---> 60 bpm , m_interval = 500 ---> 120bpm
    {
        m_lastMillis = millis();
        m_step++;
        if (m_step == m_STEPNUM)
        {
            m_step = 0;
        }
    }
    return m_step;
}

void Sequencer::muxInit()
{
    pinMode(pin_Out_S0, OUTPUT);
    pinMode(pin_Out_S1, OUTPUT);
    pinMode(pin_Out_S2, OUTPUT);

    pinMode(ledMuxCom, OUTPUT);
    pinMode(buttonMuxCom, INPUT_PULLUP);
    pinMode(potiMuxCom, INPUT);
}

void Sequencer::muxUpdate()
{
    unsigned int stepCounterBefore = 0;
    unsigned int stepCounter = counter(getBPMInterval());

    digitalWrite(pin_Out_S0, bit1[stepCounter]);
    digitalWrite(pin_Out_S1, bit2[stepCounter]);
    digitalWrite(pin_Out_S2, bit3[stepCounter]);
    if (stepCounter != stepCounterBefore)
    {
        stepCounterBefore = stepCounter;
        digitalWrite(ledMuxCom, HIGH);
    }
    // delay(200);
    //buttonMuxState[stepCounter] = digitalRead(buttonMuxCom);
}

int *Sequencer::analogPotiReader()
{
    for (unsigned int i{0}; i < 8; ++i)
    {

        digitalWrite(pin_Out_S0, bit1[i]);
        digitalWrite(pin_Out_S1, bit2[i]);
        digitalWrite(pin_Out_S2, bit3[i]);

        potiMuxState[i] = analogRead(potiMuxCom);
        /*    Serial.print(i);
        Serial.print(":");
        Serial.print(potiMuxState[i]);
        Serial.print(", "); */
    }
    return potiMuxState;
}

void Sequencer::update(int16_t *arrayInput)
{
    analogPotiReader();
    wave1Values = arrayInput;
    muxUpdate();
    stepButtons();
    stepLED();
}

Sequencer.h:

#ifndef Sequencer_h
#define Sequencer_h
#include <Encoder.h>
#include <array>
#include <Audio.h>
#include <Wire.h>
#include "Display.h"
#include <MegunoLink.h>
#include <Filter.h>

class Sequencer
{
public:
  Sequencer();
  void start();
  void stop();
  unsigned int counter(float interval);
  void stepButtons();
  void stepLED();
  void update(int16_t *arrayInput);
  float getBPMInterval();
  void muxUpdate();
  float mapper(float x, float in_min, float in_max, float out_min, float out_max);
  void muxInit();
  int *analogPotiReader();
  int _deflectionRate;
  int frequency1 = {160};
  int frequency2 = {200};
  int frequency3 = {140};
  int frequency4 = (110);
  ExponentialFilter<long> ADCFilter;
  std::array<float, 4> attack;
  std::array<float, 4> decay;
  std::array<float, 4> sustain;
  std::array<float, 4> release;
  std::array<float, 4> amplitude;
  std::array<int, 4> frequency{{frequency1, frequency2, frequency3, frequency4}};
  std::array<int, 4> m_stepLEDPin{{3, 4, 5, 6}};
  std::array<int, 4> m_stepButtonPin{{0, 1, 14, 15}};
  std::array<boolean, 4> m_stepState{{true, true, true, true}};
  std::array<boolean, 4> digitalReadValues;
  std::array<int, 8> bit1{{0, 1, 0, 1, 0, 1, 0, 1}};
  // int bit1[8] = {0, 1, 0, 1, 0, 1, 0, 1};
  std::array<int, 8> bit2{{0, 0, 1, 1, 0, 0, 1, 1}};
  std::array<int, 8> bit3{{0, 0, 0, 0, 1, 1, 1, 1}};
  int16_t *wave1Values;
  float defaultAttackValue = {50};
  float defaultDecayValue = {200};
  float defaultSustainValue = {200};
  float defaultReleaseValue = {200};
  float defaultVolume = {0.7};
  unsigned long m_lastMillis;
  static const int waveTotal = 4;
  float m_interval = (60.0 / average_bpm) * 1000.0;
  unsigned long m_stepStateInterval = 20;
  unsigned int m_STEPNUM = 8;
  unsigned int m_step = 0;
  const int numReadings = 5; // Anzahl der Readings
  int readings[5];           // the readings from the analog input
  int readIndex = 0;         // the index of the current reading
  int total = 0;             // the running total
  float average = 0;         // the average
  int inputPin = A3;         // Analog input
  int average_bpm = 120;
  int average_bpm_alt = 120;
  unsigned long m_BPMInterval = 400;
  int m_AnalogThreshold = 3;
  int ADCFilterBefore;
  float interval;
  float minBPM = 40.0;
  float maxBPM = 800.0;
  long positionEnc1 = -999;
  Encoder Enc1;
  AudioSynthWaveform *waveform = new AudioSynthWaveform[4];
  AudioEffectEnvelope *envelope = new AudioEffectEnvelope[4];
  AudioMixer4 mixer1;
  AudioOutputI2S i2s1;
  AudioConnection patchCord1;
  AudioConnection patchCord2;
  AudioConnection patchCord3;
  AudioConnection patchCord4;
  AudioConnection patchCord5;
  AudioConnection patchCord6;
  AudioConnection patchCord7;
  AudioConnection patchCord8;
  AudioConnection patchCord9;
  AudioConnection patchCord10;
  AudioControlSGTL5000 sgtl5000_1;
  int pin_Out_S0 = 2;
  int pin_Out_S1 = 3;
  int pin_Out_S2 = 4;
  int buttonMuxState[8];
  int potiMuxState[8];

  int ledMuxCom = 14;
  int buttonMuxCom = 15;
  int potiMuxCom = 16;
  int *pointerToAnalogValues;

private:
};

#endif

main.cpp:

#include <Arduino.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Encoder.h>
#include "Display.h"
#include "Sequencer.h"

Display display;
Sequencer sequencer;
AudioControlSGTL5000 sgtl5000_1;

void setup()
{
  sequencer.muxInit();
  display.init();
}

void loop()
{
  display.update();
  sequencer.update(display.wave1Values);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.