For loop inside of a header file

Hello!

Currently, I am trying to organize my code by using classes. I am building a step sequencer with the Audio Shield and a TFT Display. What I did in the main code, before using classes was to fill the arrays of my individual Audio Shield objects. My Sequencer.h code:

 #ifndef Sequencer_h
#define Sequencer_h
#include <Arduino.h>
#include <array>
#include <Audio.h>
#include <Wire.h>

class Sequencer
{
public:
    Sequencer();
    void start();
    void stop();
    void stepLED();
    unsigned int counter();
    unsigned long m_lastMillis;
    unsigned long m_interval = 200;
    unsigned int m_step = 0;
    unsigned int m_stepnum = 4;
     std::array<float, 4> attack;
    std::array<float, 4> decay;
    std::array<float, 4> sustain;
    std::array<float, 4> release;
    AudioSynthWaveform *waveform = new AudioSynthWaveform[4];
    AudioEffectEnvelope *envelope = new AudioEffectEnvelope[4];
    
    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]);
  }
    // GUItool: begin automatically generated code
    AudioMixer4 mixer1;  //xy=439,347
    AudioOutputI2S i2s1; //xy=633,351
    AudioConnection patchCord1(waveform[1], envelope[1]);
    AudioConnection patchCord2(waveform[2], envelope[2]);
    AudioConnection patchCord3(waveform[3], envelope[3]);
    AudioConnection patchCord4(waveform[0], envelope[0]);
    AudioConnection patchCord5(envelope[3], 0, mixer1, 3);
    AudioConnection patchCord6(envelope[2], 0, mixer1, 2);
    AudioConnection patchCord7(envelope[0], 0, mixer1, 0);
    AudioConnection patchCord8(envelope[1], 0, mixer1, 1);
    AudioConnection patchCord9(mixer1, 0, i2s1, 0);
    AudioConnection patchCord10(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000 sgtl5000_1; //xy=289,51
    // GUItool: end automatically generated code

private:
};

#endif

Unfortunately I don’t know how to use the same solution with the for loop to fill the arrays inside of the header file, as it results an error. Thank you in advance! :slight_smile:

Your for loop is not contained in any function. Perhaps you could add a begin method to your class and put it in there.

Alright, thats a good idea! I just did it, now for some reason I get the error from line 29 to 33(header file): array must be initialized with a brace-enclosed initializer.

Sequencer.h:

#ifndef Sequencer_h
#define Sequencer_h
#include <Arduino.h>
#include <array>
#include <Audio.h>
#include <Wire.h>

class Sequencer
{
public:
    Sequencer();
    void start();
    void stop();
    void stepLED();
    unsigned int counter();
    unsigned long m_lastMillis;
    unsigned long m_interval = 200;
    unsigned int m_step = 0;
    unsigned int m_stepnum = 4;
    float defaultAttackValue = {50};
    float defaultDecayValue = {200};
    float defaultSustainValue = {200};
    float defaultReleaseValue = {200};
    int frequency1 = {200};
    int frequency2 = {340};
    int frequency3 = {500};
    int frequency4 = (400);
    float defaultVolume = {0.3};
    std::array<int, 4> m_stepLEDPins{3, 4, 5, 6};
    std::array<int, 4> m_stepState = {1, 1, 1, 1}; // Speichert die Länge, für for loop, Werte über die Länge hinaus nicht verwendbar
    std::array<boolean, 4> m_stepState = {true, true, true, true};
    std::array<boolean, 4> m_stepStateBefore = {true, true, true, true};
    std::array<float, 4> amplitude;
    std::array<int, 4> frequency{frequency1, frequency2, frequency3, frequency4};
    std::array<float, 4> attack;
    std::array<float, 4> decay;
    std::array<float, 4> sustain;
    std::array<float, 4> release;
    AudioSynthWaveform *waveform = new AudioSynthWaveform[4];
    AudioEffectEnvelope *envelope = new AudioEffectEnvelope[4];

private:
};

#endif

Sequencer.cpp:

#include "Sequencer.h"
#include "Arduino.h"

Sequencer::Sequencer()
{
}

void Sequencer::start()
{
    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]);
    }
     std::array<int, 4> m_stepLEDPins{3, 4, 5, 6};
    std::array<boolean, 4> m_stepState = {true, true, true, true};
    std::array<boolean, 4> m_stepStateBefore = {true, true, true, true};
    std::array<float, 4> amplitude;
    std::array<int, 4> frequency{frequency1, frequency2, frequency3, frequency4};

    // GUItool: begin automatically generated code
    AudioMixer4 mixer1;  //xy=439,347
    AudioOutputI2S i2s1; //xy=633,351
    AudioConnection patchCord1(waveform[1], envelope[1]);
    AudioConnection patchCord2(waveform[2], envelope[2]);
    AudioConnection patchCord3(waveform[3], envelope[3]);
    AudioConnection patchCord4(waveform[0], envelope[0]);
    AudioConnection patchCord5(envelope[3], 0, mixer1, 3);
    AudioConnection patchCord6(envelope[2], 0, mixer1, 2);
    AudioConnection patchCord7(envelope[0], 0, mixer1, 0);
    AudioConnection patchCord8(envelope[1], 0, mixer1, 1);
    AudioConnection patchCord9(mixer1, 0, i2s1, 0);
    AudioConnection patchCord10(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000 sgtl5000_1; //xy=289,51
    // GUItool: end automatically generated code

    AudioMemory(20);
    sgtl5000_1.enable();
    sgtl5000_1.volume(defaultVolume);
    attack.fill(defaultDecayValue);
    decay.fill(defaultDecayValue);
    sustain.fill(defaultSustainValue);
    release.fill(defaultSustainValue);
    amplitude.fill(defaultVolume);
}

unsigned int Sequencer::counter() // Returns the current position
{
    if ((millis() - m_lastMillis) > m_interval)
    {
        m_lastMillis = millis();
        m_step++;
        if (m_step == m_stepnum)
        {
            m_step = 0;
        }
    }
    return m_step;
}

void Sequencer::stepLED()
{
    counter(); //No constructor needed because you already wrote Sequencer::stepLED()
    for (unsigned int i{0}; i < m_stepLEDPins.size(); ++i)
    {
        if (counter() == 0)
        {
            digitalWrite(m_stepLEDPins[0], m_stepState[0]);
            digitalWrite(m_stepLEDPins[3], LOW);

            envelope[0].noteOn();
            waveform[0].amplitude(m_stepState[0]);
            waveform[0].frequency(frequency[0]);
        }
        else if (counter() == i)
        {
            digitalWrite(m_stepLEDPins[i], m_stepState[i]);
            digitalWrite(m_stepLEDPins[i] - 1, LOW);
            envelope[i].noteOn();
            waveform[i].amplitude(m_stepState[i]);
            waveform[i].frequency(frequency[i]);
        }
    }
}

Instead of describing the error, please cut and paste the full error listing here.

In file included from src/main.cpp:7:0:
src/Sequencer.h:29:48: error: array must be initialized with a brace-enclosed initializer
std::array<int, 4> m_stepLEDPins{3, 4, 5, 6};
^
src/Sequencer.h:29:48: error: too many initializers for ‘std::array<int, 4u>’
src/Sequencer.h:30:65: error: array must be initialized with a brace-enclosed initializer
std::array<boolean, 4> m_stepState = {true, true, true, true};
^
src/Sequencer.h:30:65: error: too many initializers for ‘std::array<bool, 4u>’
src/Sequencer.h:31:71: error: array must be initialized with a brace-enclosed initializer
std::array<boolean, 4> m_stepStateBefore = {true, true, true, true};
^
src/Sequencer.h:31:71: error: too many initializers for ‘std::array<bool, 4u>’
src/Sequencer.h:33:80: error: array must be initialized with a brace-enclosed initializer
std::array<int, 4> frequency{frequency1, frequency2, frequency3, frequency4};
^
src/Sequencer.h:33:80: error: too many initializers for ‘std::array<int, 4u>’
src/main.cpp: In function ‘void setup()’:
src/main.cpp:192:31: error: ‘stepLED’ was not declared in this scope
for (unsigned int i{0}; i < stepLED.size(); ++i)
^
src/main.cpp:196:5: error: ‘mixer1’ was not declared in this scope
mixer1.gain(i, defaultVolume);
^
src/main.cpp:197:5: error: ‘waveform’ was not declared in this scope
waveform*.begin(0); // WAVEFORM_SINE expands to 0 WAVEFORM_ARBITRARY expands to 4*

  • ^*
    src/main.cpp:198:27: error: ‘amplitude’ was not declared in this scope
    waveform_.amplitude(amplitude*);
    ^
    src/main.cpp:199:27: error: ‘frequency’ was not declared in this scope
    waveform.frequency(frequency);
    ^
    src/main.cpp: In function ‘void loop()’:_

    src/main.cpp:292:9: error: ‘m_stepState’ was not declared in this scope
    m_stepState = !m_stepState;
    _ ^
    src/main.cpp:226:16: warning: unused variable ‘bpm’ [-Wunused-variable]
    static float bpm = {120}; // wird wegen static nur einmal initialisiert*

    * ^
    src/main.cpp:227:7: warning: unused variable ‘n1’ [-Wunused-variable]
    int n1 = analogRead(A1);
    ^
    In file included from src/Sequencer.cpp:1:0:
    src/Sequencer.h:29:48: error: array must be initialized with a brace-enclosed initializer_

    std::array<int, 4> m_stepLEDPins{3, 4, 5, 6};
    _ ^
    src/Sequencer.h:29:48: error: too many initializers for ‘std::array<int, 4u>’
    src/Sequencer.h:30:65: error: array must be initialized with a brace-enclosed initializer_

    std::array<boolean, 4> m_stepState = {true, true, true, true};
    _ ^
    src/Sequencer.h:30:65: error: too many initializers for ‘std::array<bool, 4u>’
    src/Sequencer.h:31:71: error: array must be initialized with a brace-enclosed initializer_

    std::array<boolean, 4> m_stepStateBefore = {true, true, true, true};
    _ ^
    src/Sequencer.h:31:71: error: too many initializers for ‘std::array<bool, 4u>’
    src/Sequencer.h:33:80: error: array must be initialized with a brace-enclosed initializer
    std::array<int, 4> frequency{frequency1, frequency2, frequency3, frequency4};
    ^
    src/Sequencer.h:33:80: error: too many initializers for ‘std::array<int, 4u>’
    ** [.pio/build/teensy40/src/Sequencer.cpp.o] Error 1_

    *** [.pio/build/teensy40/src/main.cpp.o] Error 1

try

    std::array<int, 4> m_stepLEDPins = {3, 4, 5, 6};

According to this: std::array - cppreference.com if you don't use an '=' between the std::array name and the std::array initializer, you MAY be required to put ANOTHER set of brackets around the brackets. Try that.