Strange blinks in code

Hi all,

I am doing a project on sending MIDI messages from 15 IR sharp sensors. Ones a while i get strange blinks and the piezo that i use for testing suddenly bleeps (in this case 14) a sensor. cant understand why as the value is so that it shouldnt. Any ideas what am doing wrong ?

#include <MIDI.h>
#include <DmxSimple.h>
const byte channel[15] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
const byte playingNote[15] = {49,51,54,56,58,61,63,66,68,70,73,75,78,80,82};
int i;
int piez = 8;
int tones[15]={2000,2200,2500,2700,2900,3100,3300,3500,3600,3800,4200,4400,4600,5000,5200};
const int input[15]={A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14};
int sensing[15];
int pitch[15];
MIDI_CREATE_DEFAULT_INSTANCE();
int noteStatus[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void setup() {
  Serial.begin(9600);
 // MIDI.begin();
  DmxSimple.usePin(3);
  DmxSimple.maxChannel(4);
}

void loop(){
    for(i=0;i<15;i++){
    sensing[i] = map(analogRead(input[i]), 0, 550, 150, 0);
    pitch[i] = map(analogRead(input[i]), 0, 550, -8190, 8190);
      Serial.print("Sensor ");
      Serial.print(i);
      Serial.print(" : ");
      Serial.println(sensing[i]);
      if(sensing[i]<127){
      MIDI.sendControlChange(1, sensing[i], channel[i]); 
      MIDI.sendPitchBend(pitch[i], channel[i]);
       if(noteStatus[i]==0){
          MIDI.sendNoteOn(playingNote[i], sensing[i], channel[i]);
            tone(piez, tones[i], 500);
          noteStatus[i]=1;
        }
     }else{
          if(noteStatus[i]!=0){
          MIDI.sendNoteOff(playingNote[i], 0, channel[i]);
          noteStatus[i]=0;
              }
     }
 delay(200);
  }
}

// DmxSimple.write(1, brightness);

It seems to coincide with this part of the code running for sensor 13:

      {
          MIDI.sendControlChange(1, sensing[i], channel[i]); 
          MIDI.sendPitchBend(pitch[i], channel[i]);
          if(noteStatus[i]==0)
          {
              MIDI.sendNoteOn(playingNote[i], sensing[i], channel[i]);
              tone(piez, tones[i], 500);
              noteStatus[i]=1;
          }
    }

Is this the correct reference for the MIDI library you’re using?

http://arduinomidilib.sourceforge.net/a00001.html#a3a80e9bd2cfdb619be91d80e8f3b7825

If so, check it. For example, sendControlChange takes three bytes:

Parameters:
ControlNumber	The controller number (0 to 127).
ControlValue	The value for the specified controller (0 to 127).
Channel	The channel on which the message will be sent (1 to 16).

Your second parameter is sensing[i] which is:

sensing[i] = map(analogRead(input[i]), 0, 550, 150, 0);

(i.e. ranges from 150 to 0) but the library calls for this value to be limited from 0-127.

Same with sendNoteOn(…).

Double check the values you’re sending for all parameters in the MIDI lib calls.

Hi thx for the reply. What do you mean with the sensor 13 part ? I have 3 elements with channel as last for CC. Also im going to add constrain() to the sensor output, hope that helps.

Any idea how to decoupling the sensors ?

What do you mean with the sensor 13 part ?

Sensor 13’s value, at 123, is the first (and only) value in the visible sequence that causes that part of the code to be executed because it satisfies:

if(sensing[i]<127){...

What do you mean by decouple the sensors?

MIDI_CREATE_DEFAULT_INSTANCE();

with this you’ve associated the midi port to the primary hwSerial port, that is the same port you are writing your messages to.
That you haven’t called begin() should make the difference, but the Midi.h library does not always do the expected.
what blackfin is saying is

if(sensing[i]<127){

you start sending a midi message here 'cause the condition is true, at 31250bps through your hardware port.

by decoupling adding capacitors near the sensors. indeed but that value 123 was an unstable value should be 149 like the rest

tried using a different TX port but then the TX led doesnt even blink

#include <MIDI.h>
#include <DmxSimple.h>
int channel[15] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int playingNote[15] = {49,51,54,56,58,61,63,66,68,70,73,75,78,80,82};
int i;
int piez = 8;
int tones[15]={2000,2200,2500,2700,2900,3100,3300,3500,3600,3800,4200,4400,4600,5000,5200};
int input[15]={A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14};
int sensing[15];
int sensingConst[15];
int pitch[15];
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI2);
//MIDI_CREATE_DEFAULT_INSTANCE();
int noteStatus[15]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void setup() {
//  Serial.begin(9600);
  MIDI2.begin(MIDI_CHANNEL_OFF);
  DmxSimple.usePin(3);
  DmxSimple.maxChannel(4);
}

void loop(){
    for(i=0;i<15;i++){
    sensing[i] = map(analogRead(input[i]), 0, 700, 0, 127);
    sensingConst[i] = constrain(sensing[i], 0, 127);
    pitch[i] = map(analogRead(input[i]), 0, 700, -8190, 8190);
   //   Serial.print("Sensor ");
   //   Serial.print(i);
    //  Serial.print(" : ");
    //  Serial.println(sensingConst[i]);
      if(sensingConst[i]>30){
      MIDI2.sendControlChange(1, sensingConst[i], channel[i]); 
      MIDI2.sendPitchBend(pitch[i], channel[i]);
       if(noteStatus[i]==0){
          MIDI2.sendNoteOn(playingNote[i], sensingConst[i], channel[i]);
            tone(piez, tones[i], 500);
         //   Serial.print("Sensor ");
        //    Serial.print(i);
        //    Serial.print(" : ");
        //    Serial.println("aan");
          noteStatus[i]=1;
        }
     }else{
          if(noteStatus[i]!=0){
          MIDI2.sendNoteOff(playingNote[i], 0, channel[i]);
          noteStatus[i]=0;
              }
     }
 delay(100);
  }
}

The TX LED will only blink for transmissions on the default Serial port (e.g. not for Serial2).

I was mistaken; the “blip” you see is explained by @Deva_Rishi. In serial.MIDI.h (part of the MIDI library) you’ll see:

#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
    // Leonardo, Due and other USB boards use Serial1 by default.
    #define MIDI_CREATE_DEFAULT_INSTANCE()                                      \
        MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
#else
    /*! \brief Create an instance of the library with default name, serial port
    and settings, for compatibility with sketches written with pre-v4.2 MIDI Lib,
    or if you don't bother using custom names, serial port or settings.
    */
    #define MIDI_CREATE_DEFAULT_INSTANCE()                                      \
        MIDI_CREATE_INSTANCE(HardwareSerial, Serial,  MIDI);
#endif

As Deva noted, the original code had MIDI messages being sent out Serial which was also the port you used for debug/informative messages.

If you modify your code to re-enable Serial to send informative messages but leave the MIDI on Serial2, what do you see?

Not getting any results. Was wondering if im doing the right way in activating information on serial and midi over serial2

And we are supposed to guess how you have changed it and base our answer on our guesses ?
For now we only assume you are using a Mega because of the pins definitions. If you use the USB serial (Serial) for debug, and Serial2 for Midi, then you need to connect the midi hardware (TTL Schmitt-trigger & resistors) to the Serial2 TX-pin (you are not attempting midi-receive)

On a completely different note, this is probably not the best way to transmit DMX (swSerial at 250kbps rarely is), particularly since you have 4 hwSerial ports of which you still have 2 available.

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