Midi & Array

Hello, I'm having trouble with this code

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250);


  
}
  int note[] = {72,77,81,84}; // C4, F4, A4, C5


void loop() {
  

  for (int note = 0; note < 4; note++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(500);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00);   
    delay(500);
  }
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(int cmd, int pitch, int velocity) {
  Serial.print(cmd, BYTE);
  Serial.print(pitch, BYTE);
  Serial.print(velocity, BYTE);
}

The notes in the array are not being selected. Instead the midi note value is selected from the "for" statement itself I think. I'm getting values 0-4. How do I correct this so that "note" noteOn(0x90, [glow]note[/glow], 0x45); reads from the array? :slight_smile:
Thanks

int [glow]note[/glow][] = {72,77,81,84};
for (int [glow]note [/glow]= 0; note < 4; note++)

You have a global variable called note. You also have a local variable called note that hides the global variable of the same name.

Having global and local variables with the same name is rarely a good idea, as you've seen. Change one of them...

[edit]By the way, the for loop variable should be used as in index into the array in the body of the loop, not the value.

for (int n = 0; n < 4; n++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note[n], 0x45);

[/edit]

Thanks PaulS but when I do, I end up having to declare "note" and I'm afraid I don't know what to declare "note" as in order to read from the array.

  int mynote[] = {72,77,81,84}; // C4, F4, A4, C5
int note = 0;

void loop() {
  

  for (int mynote = 0;  mynote < 4;  mynote++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(500);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00);   
    delay(500);

Here I have it as "0" so that the code compiles but I'm quite clueless sorry :cry:

Just saw your edit!! I'll mess with that, thanks :slight_smile:

:slight_smile: Thanks PaulS, I have it working now :slight_smile:

Ok, I have added the ability to output the midi notes from the chosen array to my original tinkered sketch. This works fine with the audible notes and the midi notes playing for each of the steps in the sequence. However, while I am able to change the pitch of the audible notes through the AnalogInFrequency pot for each of the steps within the "if" statements, I can't seem to do so for the midi notes. What have I missed? I really appreciate all this help, I swear I'm learning but I'm stumped again! :slight_smile:

/* ======================================================================
 Arduino Punk Console
 A simple programmable 8 step tone sequencer 
 by dano/beavisaudio.com

 Revs
 -----------------------------------
 15 Sept  djh  initial version
======================================================================*/

// Map all the input and output pins

#define AnalogInFrequency 1
#define AnalogInTempo 2
#define AnalogInDuration 0
#define DigitalOutSignal 11
#define DigitalInSwitch0 2
#define DigitalInSwitch1 3
#define DigitalInSwitch2 4
#define DigitalInSwitch3 5


#define DigitalInStartStop 10
#define DigitalOutLED 12

// Set up the array for each step
int steps[] = {49,55,66,74};

int mynotes[] = {49,55,66,74,82,98,110,131,147,165,196,220,262,294,330,
                  392,440,523,587,659,784,880,1047,1175,1319};
int mymidi[] = {31,33,36,38,40,43,45,48,50,52,55,57,60,62,64,67,69,72,74,
              76,79,81,84,86,88};

  // misc housekeeping
int duration = 50;
int pitchval = 1;
int fPlayMode = true;
int lastPushedStep = -1;

// Initialize the tempo
int tempo = 100;

void setup()
{ 
   // initialize the digital pin as an output:
 for(int i = 0; i <= 4; i++)

     
  // setup pin modes (Digital pins are input by default, but
  // I like to set 'em explicitly just so the code is clear.
  pinMode (DigitalInSwitch0, INPUT);
  pinMode (DigitalInSwitch1, INPUT);
  pinMode (DigitalInSwitch2, INPUT);
  pinMode (DigitalInSwitch3, INPUT);
 
  pinMode (DigitalInStartStop, INPUT);
  pinMode (DigitalOutSignal, OUTPUT);  
  pinMode (DigitalOutLED, OUTPUT);
  
  Serial.begin(31250);

  } 

  void loop()


{
  // Main sequence loop  
  for (int i=0; i<4; i++)
 
   {   
    // Are we playing or stopping?
    fPlayMode = digitalRead (DigitalInStartStop);
    digitalWrite (DigitalOutLED, HIGH);
     noteOn(0x90, mymidi[i], 0x40);

    // Check the Hardware
     readSwitches();
     readPots();


    // Make the noise
    if (fPlayMode)
    {
      freqout (steps[i], duration);
      noteOn(0x90, mymidi[i], 0x00);
    }
    digitalWrite (DigitalOutLED, LOW);
    //digitalWrite(ledpin[i], LOW);
    // Pause between steps
    delay (tempo);       
  }
}



// Read the current values of the pots, called from the loop.
void readPots ()
{
    tempo = (analogRead (AnalogInTempo) * 1.9);
    duration = (analogRead (AnalogInDuration));
}

// Read the current values of the switches and
// if pressed, replace the switch's slot frequency
// by reading the frequency pot.
void readSwitches()

{
  // reset last pushed button number
  lastPushedStep = -1;

  // check switch 0, if pressed, get the current freq into step 0, etc. etc.
    int val = analogRead(AnalogInFrequency);
    int index = map(val, 0, 1024, 0, 20);
    int myNote = mynotes[index]; 
    
      int val2 = analogRead(AnalogInFrequency);
      int value = map(val2, 0, 1024, 0, 20);
      int myMidi = mymidi[value]; 
    

 
    if (digitalRead (DigitalInSwitch0) == HIGH)  
  {
     
    
   
    steps[0] = myMidi;
     steps[0] = myNote;
      lastPushedStep = 1;
  }

  else if (digitalRead (DigitalInSwitch1) == HIGH)
  { 
    
    
    steps[1] = myMidi;
     steps[1] = myNote;
     lastPushedStep = 2;
  }

  else if (digitalRead (DigitalInSwitch2) == HIGH)
  { 
     
     
     steps[2] = myMidi;
     steps[2] = myNote;
     lastPushedStep = 3;
  }
  else if (digitalRead (DigitalInSwitch3) == HIGH)
  { 
     
     steps[3] = myMidi;
     steps[3] = myNote;
     lastPushedStep = 4;
  }
 
    
  }




//freqout code by Paul Badger 
// freq - frequency value
// t - time duration of tone
void freqout(int freq, int t) 
{ 
  int hperiod;     //calculate 1/2 period in us 
  long cycles, i; 

  // subtract 7 us to make up for digitalWrite overhead - determined empirically 
  hperiod = (500000 / ((freq - 0) * pitchval));             

  // calculate cycles 
  cycles = ((long)freq * (long)t) / 1000;    // calculate cycles 

  for (i=0; i<= cycles; i++)
  {              // play note for t ms  
    digitalWrite(DigitalOutSignal, HIGH);  
    delayMicroseconds(hperiod); 
    digitalWrite(DigitalOutSignal, LOW);  
    delayMicroseconds(hperiod - 1);     // - 1 to make up for fractional microsecond in digitaWrite overhead 
  } 
}
     //  plays a MIDI note.  Doesn't check to see that
 //  cmd is greater than 127, or that data values are  less than 127:
 void noteOn(byte cmd, byte data1, byte  data2) {
   Serial.print(cmd, BYTE);
   Serial.print(data1, BYTE);
   Serial.print(data2, BYTE);
 }

Thanks, :slight_smile:

1 Like