MIDI control of LED strips

Status
Not open for further replies.
Hi again mortonkopf

Yes absolutely, re your "//add lines to go an update all leds that are currently on", I thought that having Pin>0 would indeed do this, but it seems this doesn't take into account the state of noteOn.

So now I've tried this if line, as per your mention of && above
Code:
 if (myNoteOn==60 && LeftRedPin > 0)

And I also tried this
Code:
if (myNoteOn==60)

But now I'm back to square one and the CC brightness control has no effect on the light once it's lit.

I have read through the link you posted but it's too far over my head sadly and leaves me even more confused than before. You mention this is just one way of doing it.. is there another way you could recommend that I might be able to get my head around easier?!

Many thanks for all your help.
 
Stick with the state machine approach for now, its worthwhile. below is the kind of thing you need to do, but i am a bit rushed at the moment, so its the concept rather than the specifics below

Firstly, you need to add a state variable for each led, this is NOT the same as saying it is off because you have set brightness to zero.

You do this by adding a variable to look after it:
Code:
boolean note60 =0;
you will need a boolean state for each note led you want to control.

Then, when you receive
Code:
void myNoteOn(byte channel, byte note, byte velocity) {
  velocity = velocity * 2;
 if (note == 60) {
    analogWrite(LeftRedPin, RedBrightness);
    analogWrite(LeftGreenPin, GreenBrightness);
    analogWrite(LeftBluePin, BlueBrightness);
  }

You add in the line
Code:
if (note == 60) {
	note60 = 1; //this tells the state machine that note 60 is on
    analogWrite(LeftRedPin, RedBrightness);
    analogWrite(LeftGreenPin, GreenBrightness);
    analogWrite(LeftBluePin, BlueBrightness);
  }

Then, in
Code:
void myNoteOff(byte channel, byte note, byte velocity) {
  velocity = velocity * 2;

  if (note == 60) {
    analogWrite(LeftRedPin, 0);
    analogWrite(LeftGreenPin, 0);
    analogWrite(LeftBluePin, 0);
  }

You add the line
Code:
  if (note == 60) {
note60 =0;
    analogWrite(LeftRedPin, 0);
    analogWrite(LeftGreenPin, 0);
    analogWrite(LeftBluePin, 0);
  }

This way we have a variable to check. If it is 1, we have received a note on, and it can be influenced via the && , if we have received a note off, it cant be influenced when checked by the &&

Then in the
Code:
void myControlChange(byte channel, byte number, byte value) {

  if ( ( number == 22) && (the red led boolean == 1))
  {
    RedBrightness = (int)value * 2;
    if (LeftRedPin > 0)
      analogWrite(LeftRedPin, RedBrightness);
    if (RightRedPin > 0)
      analogWrite(RightRedPin, RedBrightness);
  }
 
mortonkopf, thank you very much for your help, I think it's working now! I couldn't make the myControlChange section look like your example but I managed to get it to work.

This is what I have and seems to be doing the job, is there anything that looks wrong to you?

Code:
int RedBrightness = 0;
int GreenBrightness = 0;
int BlueBrightness = 0;

int LeftRedPin = 16;
int LeftGreenPin = 17;
int LeftBluePin = 20;

int RightRedPin = 22;
int RightGreenPin = 23;
int RightBluePin = 3;

boolean note60 = 0;
boolean note62 = 0;

void myNoteOn(byte channel, byte note, byte velocity) {
  velocity = velocity * 2;

  // left - note C3

  if (note == 60) {
    note60 = 1;
    analogWrite(LeftRedPin, RedBrightness);
    analogWrite(LeftGreenPin, GreenBrightness);
    analogWrite(LeftBluePin, BlueBrightness);
  }

  // right - note D3

  if (note == 62) {
    note62 = 1;
    analogWrite(RightRedPin, RedBrightness);
    analogWrite(RightGreenPin, GreenBrightness);
    analogWrite(RightBluePin, BlueBrightness);
  }
}


void myNoteOff(byte channel, byte note, byte velocity) {
  velocity = velocity * 2;

  // left - note C3

  if (note == 60) {
    note60 = 0;
    analogWrite(LeftRedPin, 0);
    analogWrite(LeftGreenPin, 0);
    analogWrite(LeftBluePin, 0);
  }

  // right - note D3

  if (note == 62) {
    note62 = 0;
    analogWrite(RightRedPin, 0);
    analogWrite(RightGreenPin, 0);
    analogWrite(RightBluePin, 0);
  }
}

void myControlChange(byte channel, byte number, byte value) {

  // RGB brightness - MIDI CC 22/23/24

  //red

  if (number == 22)
  {
    RedBrightness = (int)value * 2;
    if (note60 = 1)
      analogWrite(LeftRedPin, RedBrightness);
    if (note62 = 1)
      analogWrite(RightRedPin, RedBrightness);
  }

  //green

  if (number == 24)
  {
    GreenBrightness = (int)value * 2;
    if (note60 = 1)
      analogWrite(LeftGreenPin, GreenBrightness);
    if (note62 = 1)
      analogWrite(RightGreenPin, GreenBrightness);
  }

  //blue

  if (number == 23)
  {
    BlueBrightness = (int)value * 2;
    if (note60 = 1)
      analogWrite(LeftBluePin, BlueBrightness);
    if (note62 = 1)
      analogWrite(RightBluePin, BlueBrightness);
  }
}

void setup() {
  usbMIDI.setHandleNoteOff(myNoteOff);
  usbMIDI.setHandleNoteOn(myNoteOn);
  usbMIDI.setHandleControlChange(myControlChange);

  // left
  pinMode(LeftRedPin, OUTPUT);
  pinMode(LeftGreenPin, OUTPUT);
  pinMode(LeftBluePin, OUTPUT);

  // right
  pinMode(RightRedPin, OUTPUT);
  pinMode(RightGreenPin, OUTPUT);
  pinMode(RightBluePin, OUTPUT);
}

void loop() {
  usbMIDI.read(16); // Only listens to MIDI Channel 16
}

Thank you once again mortonkopf, MarkT, kd5rxt-mark and PaulS for all your responses, it's really appreciated.
 
glad you got it working. state machines are building blocks of many projects, so its good to understand them.

Had a quick scan and it look good, the difference comes from there being two leds per colour, and my example just dealt with one. it works!!
 
Hi again @mortonkopf, I'm afraid I seem to have jumped the gun! It's not doing what I thought it was, the LEDs will still light if there is CC data but no noteOn ... Can you see a mistake in the code ?

Code:
int RedBrightness = 255;
int GreenBrightness = 255;
int BlueBrightness = 255;

int LeftRedPin = 16;
int LeftGreenPin = 17;
int LeftBluePin = 20;

int RightRedPin = 22;
int RightGreenPin = 23;
int RightBluePin = 3;

boolean note60 = 0;
boolean note62 = 0;

void myNoteOn(byte channel, byte note, byte velocity) {
  velocity = velocity * 2;

  // left - note C3

  if (note == 60) {
    note60 = 1;
    analogWrite(LeftRedPin, RedBrightness);
    analogWrite(LeftGreenPin, GreenBrightness);
    analogWrite(LeftBluePin, BlueBrightness);
  }

  // right - note D3

  if (note == 62) {
    note62 = 1;
    analogWrite(RightRedPin, RedBrightness);
    analogWrite(RightGreenPin, GreenBrightness);
    analogWrite(RightBluePin, BlueBrightness);
  }
}


void myNoteOff(byte channel, byte note, byte velocity) {
  velocity = velocity * 2;

  // left - note C3

  if (note == 60) {
    note60 = 0;
    analogWrite(LeftRedPin, 0);
    analogWrite(LeftGreenPin, 0);
    analogWrite(LeftBluePin, 0);
  }

  // right - note D3

  if (note == 62) {
    note62 = 0;
    analogWrite(RightRedPin, 0);
    analogWrite(RightGreenPin, 0);
    analogWrite(RightBluePin, 0);
  }
}

void myControlChange(byte channel, byte number, byte value) {

  // RGB brightness - MIDI CC 22/23/24

  //red

  if (number == 22)
  {
    RedBrightness = (int)value * 2;
    if (note60 = 1)
      analogWrite(LeftRedPin, RedBrightness);
    if (note62 = 1)
      analogWrite(RightRedPin, RedBrightness);
  }

  //green

  if (number == 24)
  {
    GreenBrightness = (int)value * 2;
    if (note60 = 1)
      analogWrite(LeftGreenPin, GreenBrightness);
    if (note62 = 1)
      analogWrite(RightGreenPin, GreenBrightness);
  }

  //blue

  if (number == 23)
  {
    BlueBrightness = (int)value * 2;
    if (note60 = 1)
      analogWrite(LeftBluePin, BlueBrightness);
    if (note62 = 1)
      analogWrite(RightBluePin, BlueBrightness);
  }
}

void setup() {
  usbMIDI.setHandleNoteOff(myNoteOff);
  usbMIDI.setHandleNoteOn(myNoteOn);
  usbMIDI.setHandleControlChange(myControlChange);

  // left
  pinMode(LeftRedPin, OUTPUT);
  pinMode(LeftGreenPin, OUTPUT);
  pinMode(LeftBluePin, OUTPUT);

  // right
  pinMode(RightRedPin, OUTPUT);
  pinMode(RightGreenPin, OUTPUT);
  pinMode(RightBluePin, OUTPUT);
}

void loop() {
  usbMIDI.read(16); // Only listens to MIDI Channel 16
}
 
Ahh, I found it! I was missing an extra =

I had
Code:
note60 = 1
instead of
Code:
note60 == 1

Thanks again for your help!
 
ah yes, check all if() statements, as = must be == when doing a compare. they are there throughout the control change routine
 
Status
Not open for further replies.
Back
Top