i did my home work, and your asnwers was help full, i write this code, is a vu meter, it takes the midi cc and acording it light the led.
because i have 2 deferent row of led in the max7219 that i want to light up, i end up with this code, and i will like your oppinion, i did it corect or wrong. code is working
2 thinks that are not clear in my mind is this.
i have an array with 12 index.
lets take case 2. my logic is, if CC is 2, then case 2 will activate. now i have a four loop.
when i tell that (int i=2; ) ( i < 9; ) is equal to 2 < 9 till here i got it.
for (int i = 2; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
here when i declare (vul[i], false.) my logic is that (i ) is 2 and this (i) is transfer to (vul[i], and it call the index number 1, that is 1 and it light the led no1
the next loop the( i ) equal to 3. so it call index no3 that is no 2, so led no2 is light up
and i assume the loop is exit when 9 < 9; it stop here?
take a look in case 9.
my logic here is that because i need to light leds in row no4 , but leds in row no2 not to turn off
my question here is that (i) should be 9 too? or 5 how i did it.. because i only light up 4 leds from the row now 4
case 2:
for (int i = 2; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 2; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
case 3:
for (int i = 3; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 3; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
#include "MIDIUSB.h"// Library from here https://github.com/FortySevenEffects/arduino_midi_library
#include "LedControl.h"
//pin 12 is connected to the DataIn
// pin 11 is connected to the CLK
// pin 10 is connected to LOAD
LedControl lc = LedControl(12, 11, 10, 1);
byte VU_MIDI_CH = 0;
byte vuLcc = 12;
int delaytime = 200;
unsigned int vul[12] = {0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3,};
unsigned int vul1[8] = {0, 1, 2, 3, 4, 5, 6, 7,};
int ccLastValue = 0;
//unsigned int Vuvaleu[7] = {0, 0, 0, 0, 0, 0, 0,};
void setup() {
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
Serial.begin(115200);
lc.setLed (0, 7, 1, true); // turn 1 led on to see if the code is working
//all leds in row 2 are turn on one by one
for (int i = 0; i < 8; i++) {
lc.setLed(0, 2, vul[i], true);
delay(delaytime);
}
//all leds in row 6 turn on one by one
for (int i = 0; i < 8; i++) {
lc.setLed(0, 6, vul1[i], true);
delay(delaytime);
}
lc.clearDisplay(0);
}
void loop() {
midiEventPacket_t rx = MidiUSB.read();
switch (rx.header) {
case 0:
break; //No pending events
// case 0x9:
// handleNoteOn(
// rx.byte1 & 0xF, //channel
// rx.byte2, //pitch
// rx.byte3 //velocity
// );
// break;
// case 0x8:
// handleNoteOff(
// rx.byte1 & 0xF, //channel
// rx.byte2, //pitch
// rx.byte3 //velocity
// );
// break;
case 0xB:
vumeter(
rx.byte1 & 0xF, //channel
rx.byte2, //cc
rx.byte3 //value
);
break;
}
}
void vumeter(byte channel, byte number, byte value) {
int value_ = value;
if (value_ != ccLastValue) {
if (channel == VU_MIDI_CH) {
if (number == vuLcc ) {
switch (value_) {
//led in row 2 are light acording to the case
case 0:
for (int i = 0; i < 12; i++) {
lc.setLed(0, 2, vul[i], false);
lc.setLed(0, 4, vul[i], false);
}
break;
case 1:
for (int i = 1; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 1; i++) {
lc.setLed(0, 2, vul[0], true);
}
break;
case 2:
for (int i = 2; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 2; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
case 3:
for (int i = 3; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 3; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
case 4:
for (int i = 4 ; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 4; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
case 5:
for (int i = 5; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 5; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
case 6:
for (int i = 6; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 6; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
case 7:
for (int i = 7; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 7; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
case 8:
for (int i = 8; i < 9; i++) {
lc.setLed(0, 2, vul[i], false);
}
for (int i = 0; i < 8; i++) {
lc.setLed(0, 2, vul[i], true);
}
break;
// led in the row 4 are light acording to the case, but leds in the row 2 should stay on
case 9:
for (int i = 0; i < 9; i++) {
lc.setLed(0, 2, vul[i], true);
}
for (int i = 0; i < 5; i++) {
lc.setLed(0, 4, vul[i], false);
}
for (int i = 0; i < 0; i++) {
lc.setLed(0, 4, vul[i], true);
}
break;
case 10:
for (int i = 0; i < 9; i++) {
lc.setLed(0, 2, vul[i], true);
}
for (int i = 1; i < 9; i++) {
lc.setLed(0, 4, vul[i], false);
}
for (int i = 0; i < 1; i++) {
lc.setLed(0, 4, vul[i], true);
}
break;
case 11:
for (int i = 0; i < 9; i++) {
lc.setLed(0, 2, vul[i], true);
}
for (int i = 2; i < 9; i++) {
lc.setLed(0, 4, vul[i], false);
}
for (int i = 0; i < 2; i++) {
lc.setLed(0, 4, vul[i], true);
}
break;
case 12:
for (int i = 0; i < 9; i++) {
lc.setLed(0, 2, vul[i], true);
}
for (int i = 3; i < 9; i++) {
lc.setLed(0, 4, vul[i], false);
}
for (int i = 0; i < 3; i++) {
lc.setLed(0, 4, vul[i], true);
}
break;
}
ccLastValue = value;
}
}
}
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}