Drum elements in an array - Dynamic variable type confusion

Status
Not open for further replies.

nubie

Active member
Please how do you formulate this in C code for Arduino?

If I have this inside a loop:
Code:
if(millis() == next)  {
    next = millis() + 1000;
    playDrum ();
}
and a function
Code:
void playDrum () {
  drum1.noteOn();
}
then I'll hear my drum1 played. drum1 being an element from the Audio Library
I have 4 drums elements drum1, drum2, drum3, drum4, like in SimpleDrum.ino tutorial example.

The question is how can drum1 be dynamic. I mean replaced by a variable so it could be drum1, drum2, drum3 etc that I have for instance placed in an array, so I can call dynamically. I have to know so my code can look way simpler and not with plenty switch/break lines of code (when I use for something else with more elements in the array, which also not stirngs or numbers).

WRONG:
const char drum = "drum1";
const char drum = drum1;

OK:
AudioSynthSimpleDrum drum = drum1;
I have no error, but then
drum.noteOn();
will do nothing.

If I write
AudioSynthSimpleDrum myDrums[] = {drum1, drum2, drum3, drum4};
I got no error but then
myDrums[].noteOn();
will do nothing.

This also won't work:

Code:
AudioSynthSimpleDrum myDrums[] = {drum1, drum2, drum3, drum4};
AudioSynthSimpleDrum theDrum = myDrums[0];

void playDrum () {
  theDrum.noteOn();
}
I have an issue with declaring myDrums[] and currentDrum.

EDIT: adding code tags
 
Last edited:
Just use an array of drums:

Code:
AudioSynthSimpleDrum myDrums[4];
uint8_t currentDrum;


void playDrum () {
	myDrums[currentDrum].noteOn();
}
 
Just use an array of drums:

Code:
AudioSynthSimpleDrum myDrums[4];
uint8_t currentDrum;


void playDrum () {
	myDrums[currentDrum].noteOn();
}
Thanks for trying to help!

I don't understand the:
Code:
AudioSynthSimpleDrum myDrums[4];
How does the code know I have to play drum1 or drum2 etc. ?

If I do this, it works:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>

#include <synth_simple_drum.h>
 
AudioSynthSimpleDrum     drum2;          //xy=142,180
AudioSynthSimpleDrum     drum3;          //xy=167,246
AudioSynthSimpleDrum     drum1;          //xy=174,133
AudioSynthSimpleDrum     drum4;          //xy=207,310
AudioMixer4              mixer1;         //xy=480,201
AudioOutputAnalog        dac1;           //xy=647,197
AudioConnection          patchCord1(drum2, 0, mixer1, 1);
AudioConnection          patchCord2(drum3, 0, mixer1, 2);
AudioConnection          patchCord3(drum1, 0, mixer1, 0);
AudioConnection          patchCord4(drum4, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, dac1);

AudioControlSGTL5000     sgtl5000_1;     //xy=930,518

// GATE IN input
#define gateIn 12

int gateState = 0;  // for the gate in

// bounce pour Select switch
Bounce selectSwitch = Bounce(3, 15);

AudioSynthSimpleDrum myDrums[] = {drum1, drum2, drum3, drum4};
//AudioSynthSimpleDrum myDrums[4];
uint8_t currentDrum = 0;

void setup() {
  Serial.begin(115200);

  // audio library init
  AudioMemory(15);

  AudioNoInterrupts();

  pinMode(gateIn, INPUT);   // GATE IN
  pinMode(3, INPUT_PULLUP); // Select switch

  drum1.frequency(60);
  drum1.length(1500);
  drum1.secondMix(0.0);
  drum1.pitchMod(0.55);
  
  drum2.frequency(60);
  drum2.length(300);
  drum2.secondMix(0.0);
  drum2.pitchMod(1.0);
  
  drum3.frequency(550);
  drum3.length(400);
  drum3.secondMix(1.0);
  drum3.pitchMod(0.5);

  drum4.frequency(1200);
  drum4.length(150);
  drum4.secondMix(0.0);
  drum4.pitchMod(0.0);
  
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);

  AudioInterrupts();

}

void loop() {
  selectSwitch.update();  // listen to Select switch
  
  if (selectSwitch.fallingEdge()) {
    changeDrum();
  }
  
  int knobFreq = analogRead(A11);   // tune
  int knobLength = analogRead(A9);  // fine tune

  drum1.frequency(knobFreq*2);
  drum2.frequency(knobFreq*2);
  drum3.frequency(knobFreq*2);
  drum4.frequency(knobFreq*2);

  drum1.length(knobLength*2);
  drum2.length(knobLength*2);
  drum3.length(knobLength*2);
  drum4.length(knobLength*2);
  
  // listen to GATE IN signals
  attachInterrupt(gateIn, gateISR, CHANGE);

}

void changeDrum () {    
  if (currentDrum<4) {
    currentDrum++;
  } else {
    currentDrum = 0;
  }
  Serial.print(currentDrum);
}

// handle GATE IN signals
void gateISR() {
  if (digitalReadFast(gateIn)) {
    drum1.noteOn();  
    //myDrums[currentDrum].noteOn(); 
  } 
}

If I do this, it doesn't:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>

#include <synth_simple_drum.h>
 
AudioSynthSimpleDrum     drum2;          //xy=142,180
AudioSynthSimpleDrum     drum3;          //xy=167,246
AudioSynthSimpleDrum     drum1;          //xy=174,133
AudioSynthSimpleDrum     drum4;          //xy=207,310
AudioMixer4              mixer1;         //xy=480,201
AudioOutputAnalog        dac1;           //xy=647,197
AudioConnection          patchCord1(drum2, 0, mixer1, 1);
AudioConnection          patchCord2(drum3, 0, mixer1, 2);
AudioConnection          patchCord3(drum1, 0, mixer1, 0);
AudioConnection          patchCord4(drum4, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, dac1);

AudioControlSGTL5000     sgtl5000_1;     //xy=930,518

// GATE IN input
#define gateIn 12

int gateState = 0;  // for the gate in

// bounce pour Select switch
Bounce selectSwitch = Bounce(3, 15);

AudioSynthSimpleDrum myDrums[] = {drum1, drum2, drum3, drum4};
//AudioSynthSimpleDrum myDrums[4];
uint8_t currentDrum = 0;

void setup() {
  Serial.begin(115200);

  // audio library init
  AudioMemory(15);

  AudioNoInterrupts();

  pinMode(gateIn, INPUT);   // GATE IN
  pinMode(3, INPUT_PULLUP); // Select switch

  drum1.frequency(60);
  drum1.length(1500);
  drum1.secondMix(0.0);
  drum1.pitchMod(0.55);
  
  drum2.frequency(60);
  drum2.length(300);
  drum2.secondMix(0.0);
  drum2.pitchMod(1.0);
  
  drum3.frequency(550);
  drum3.length(400);
  drum3.secondMix(1.0);
  drum3.pitchMod(0.5);

  drum4.frequency(1200);
  drum4.length(150);
  drum4.secondMix(0.0);
  drum4.pitchMod(0.0);
  
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);

  AudioInterrupts();

}

void loop() {
  selectSwitch.update();  // listen to Select switch
  
  if (selectSwitch.fallingEdge()) {
    changeDrum();
  }
  
  int knobFreq = analogRead(A11);   // tune
  int knobLength = analogRead(A9);  // fine tune

  drum1.frequency(knobFreq*2);
  drum2.frequency(knobFreq*2);
  drum3.frequency(knobFreq*2);
  drum4.frequency(knobFreq*2);

  drum1.length(knobLength*2);
  drum2.length(knobLength*2);
  drum3.length(knobLength*2);
  drum4.length(knobLength*2);
  
  // listen to GATE IN signals
  attachInterrupt(gateIn, gateISR, CHANGE);

}

void changeDrum () {    
  if (currentDrum<4) {
    currentDrum++;
  } else {
    currentDrum = 0;
  }
  Serial.print(currentDrum);
}

// handle GATE IN signals
void gateISR() {
  if (digitalReadFast(gateIn)) {
    //drum1.noteOn();  
    myDrums[currentDrum].noteOn(); 
  } 
}
 
In short, after having of course declared:
Code:
AudioSynthSimpleDrum currentDrum = drum1;

This works:
Code:
void gateISR() {
  if (digitalReadFast(gateIn)) {
     drum1.noteOn(); 
  } 
}

This does not work:
Code:
void gateISR() {
  if (digitalReadFast(gateIn)) {
    currentDrum = drum1;
    currentDrum.noteOn();    
  } 
}
 
You need to learn about arrays. They are a basic and essential element of the language. There are numerous references online. Here' Arduino's: https://www.arduino.cc/reference/tr/language/variables/data-types/array/

In the variable definition section, this:
Code:
AudioSynthSimpleDrum myDrums[4];
will create an array of 4 AudioSynthSimpleDrum objects. Use that and get rid of your multiple AudioSynthSimpleDrum objects:
Code:
AudioSynthSimpleDrum     drum2;
AudioSynthSimpleDrum     drum3;
AudioSynthSimpleDrum     drum1;
AudioSynthSimpleDrum     drum4;
To access any element of the array you use the '' notation where 'i' is an integer between 0 and 3 (not 4 because array indices start at 0) i.e.:
Code:
myDrums[2].frequency(60);
or:
Code:
myDrums[currentDrum].noteOn();

Also, I don't know what you're trying to accomplish with 'gateIn' and the interrupt, but it's definitely wrong. You should not be calling 'attachInterrupt()' every time the loop() function executes. In fact, I'm 99.99% sure that you shouldn't be using interrupts at all.
 
Thank you so much.
The mistake I was making is to hang on drum1, drum2, drum 3, drum4 and try to assign a dynamic variable instead of making from start on an array with these with 4 elements, no need to name them.
I actually thought the ID was fix/given, as I did this viaAudio System Deisgn Tool and was looking at the IDs.
My mistake. Now I understand.

It works now. I can trigger events via external gate signals and change the drum sound with the switch.
Awesome.
Goind forward with Arduino coding learning.

Also, I don't know what you're trying to accomplish with 'gateIn' and the interrupt, but it's definitely wrong. You should not be calling 'attachInterrupt()' every time the loop() function executes. In fact, I'm 99.99% sure that you shouldn't be using interrupts at all.
I am using external gates (0/+5V) to trigger events.
Code:
attachInterrupt(gateIn, gateISR, CHANGE);
It's a line of code I found in an open source project called Orgone Accumulator.
I changed it btw to:
Code:
attachInterrupt(gateIn, gateISR, RISING);
It's a line of code I found in an open source project called Radio Music.

Now I can move on to the next problem.
Thanks again.
 
Glad it's working. But, your use of the interrupt is still wrong and unnecessary.

What are "gates"? Why can't you just read the input line in the loop() function?

Also, which Teensy board are you using. Some are NOT 5V tolerant and you will destroy them.
 
Glad it's working. But, your use of the interrupt is still wrong and unnecessary.

What are "gates"? Why can't you just read the input line in the loop() function?

Also, which Teensy board are you using. Some are NOT 5V tolerant and you will destroy them.
The Teensy is used on a circuit used in a modular system. So you have gate signals or trigger signals. 0V is low, +5V is high - or even +10V depending the circuit I am using.
I am also using external voltages to increase, decrease pot values. All work fine :)
Of course I am limitating in the circuit I am using the voltage which goes to the Teensy pins to +3.3V max.
I use the Teensy with external voltages already on different modules. It's just that now I wanted to go a bit deeper. And also optimize the code (using arrays and so on).

I'll take time asap to use another method like you mention to avoid interrupt.
 
Status
Not open for further replies.
Back
Top