Loading...
Pages: [1]   Go Down
Author Topic: Output MIDI from For Loop  (Read 450 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a project that uses digital switches to output MIDI, I used a very long piece of code to do the note output part, it was basically this for all of the notes:
Code:
if(digitalRead(20)==LOW){
      if(note50prev == 0){
        MIDI.sendNoteOn(50, velocity, MIDIchan);
        note50prev = 1;
      }
    } else {
      MIDI.sendNoteOff(50, 0, MIDIchan);
      note50prev = 0;
    }
But I want to do this using a for loop, so this is my new code, now I get all of the control changes, but no notes through MIDI:
Code:
#include <MIDI.h>

#define MIDIchan 1
#define PBzero 200
int velocity;

int noteCount = 5;
int notes[] = {50, 55, 60, 65, 70};
int pins[]  = {40, 41, 42, 43, 44};
int nPrev[] = {0, 0, 0, 0, 0};

void setup() {
  MIDI.begin(4);
  pinMode(40, INPUT);
  pinMode(41, INPUT);
  pinMode(42, INPUT);
  pinMode(43, INPUT);
  pinMode(44, INPUT);
  digitalWrite(40, HIGH);
  digitalWrite(41, HIGH);
  digitalWrite(42, HIGH);
  digitalWrite(43, HIGH);
  digitalWrite(44, HIGH);
}

void loop() {
  getVelocity();
  getPitchBend();
  getModulation();
  getNotes();
}

void getVelocity() {
  int vel = analogRead(0);
  velocity = vel / 8;
}

void getPitchBend() {
  int pb = analogRead(1);
  int pitchbend = (pb - 512) * 16;
  if(-PBzero < pitchbend && pitchbend < PBzero) {
    pitchbend = 0;
  }
  MIDI.sendPitchBend(pitchbend, MIDIchan);
}

void getModulation() {
  int mod = analogRead(2);
  int modulation = mod / 8;
  MIDI.sendControlChange(1, modulation, MIDIchan);
}

void getNotes() {
  for(int thisNote = 0; thisNote < noteCount; thisNote++)
  {
    int prevVal = nPrev[thisNote];
    int noteVal = notes[thisNote];
    int notePin = pins[thisNote];
    if(digitalRead(notePin)==LOW){
      if(prevVal == 0){
        MIDI.sendNoteOn(noteVal, velocity, MIDIchan);
        nPrev[thisNote] = 1;
      }
    } else {
      MIDI.sendNoteOff(noteVal, 0, MIDIchan);
      nPrev[thisNote] = 0;
    }
  }
}

Have tried the code in a simple sketch using Serial to check if the buttons are working properly and the code still works in the for loop, that worked, also changed the note to the same note to see if it was a problem with the MIDI library, it wasn't.

Any help would be greatly appreciated.
« Last Edit: November 05, 2011, 03:35:46 pm by technoducky » Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 277
Posts: 25546
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see any recursion in this sketch, where is it.
You have several variable definitions inside a for loop. That is wrong.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My Bad, really didn't know where I got recursion from.

But I don't think that's the problem as replacing the
Code:
MIDI.SendNoteOn()
with
Code:
Serial.println()
gives the intended functionality, it just seems to be a problem with using MIDI with this.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 277
Posts: 25546
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    int prevVal = nPrev[thisNote];
    int noteVal = notes[thisNote];
    int notePin = pins[thisNote];
This bit is totally redundant, just use the variables from the array.
If it works with serial print it works with sending MIDI so it must be the numbers that are wrong.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Fixed now, the problem was that the MIDIchan was defined using
Code:
# DEFINE MIDIchan 1;
Replacing it with an integer variable worked fine.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 277
Posts: 25546
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But you had that in the original post didn't you?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, but I replaced it with
Code:
int MIDIchan = 1;
and this worked fine.
Logged

Pages: [1]   Go Up
Print
 
Jump to: