Hi all
I'm French & new to Arduino. I need some help to use 4 NeoPixels strip controlled by MIDI. I would use them as a Vumeter. I have this code
int velocity;
#include <MIDIUSB.h>
#include "pitchToFrequency.h"
//#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 6 // pin sortie deck A
#define PIN1 7 // pin sortie deck B
#define PIN2 8 // pin sortie deck C
#define PIN3 9 // pin sortie deck D
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 8 // Popular NeoPixel strip Deck A
#define NUMPIXELS1 8 // Popular NeoPixel strip deck B
#define NUMPIXELS2 8 // Popular NeoPixel strip deck C
#define NUMPIXELS3 8 // Popular NeoPixel strip deck D
// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 32
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip1(NUMPIXELS, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip3(NUMPIXELS, PIN3, NEO_GRB + NEO_KHZ800);
uint32_t offColor = strip.Color(0, 0, 0); // Strip off
uint32_t whiteColor = strip.Color(32, 32, 32);
uint32_t yellowColor = strip.Color(127, 64, 0);
uint32_t redColor = strip.Color(64, 0, 0);
uint32_t blueColor = strip.Color(0, 0, 64);
#define BUZZ_PIN 9 // pin sortie buzzer
byte ng;
byte oct;
const char* pitch_name(byte pitch) {
static const char* names[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
return names[pitch % 12];
}
int pitch_octave(byte pitch) {
return (pitch / 12) - 1;
}
void noteOn(byte channel, byte pitch, byte velocity) {
tone(BUZZ_PIN, pitchFrequency[pitch]);
Serial.print("Note On: ");
Serial.print(pitch_name(pitch));
Serial.print(pitch_octave(pitch));
Serial.print(", channel=");
Serial.print(channel);
Serial.print(", velocity=");
Serial.print(velocity);
ng = pitch % 12;
oct = pitch / 12;
Serial.print(", ng= ");
Serial.println(ng);
strip.setPixelColor(ng, 0, 150, 0);
strip.show();
}
void noteOff(byte channel, byte pitch, byte velocity) {
noTone(BUZZ_PIN);
Serial.print("Note Off: ");
Serial.print(pitch_name(pitch));
Serial.print(pitch_octave(pitch));
Serial.print(", channel=");
Serial.print(channel);
Serial.print(", velocity=");
Serial.println(velocity);
ng = pitch % 12;
Serial.print(", ng= ");
Serial.println(ng);
strip.setPixelColor(ng, 0, 0, 0);
strip.show();
}
void controlChange(byte channel, byte control, byte value) {
Serial.print("Control change: control=");
Serial.print(control);
Serial.print(", value=");
Serial.print(value);
Serial.print(", channel=");
Serial.println(channel);
}
void setup() {
Serial.begin(115200);
strip.begin();
strip.clear();
}
void loop() {
noPixels();
midiEventPacket_t rx = MidiUSB.read();
switch (rx.header) {
case 0:
break; //No pending events
case 0x9:
noteOn(
rx.byte1 & 0xF, //channel
rx.byte2, //pitch
rx.byte3 //velocity
);
velocity = rx.byte3;
break;
case 0x8:
noteOff(
rx.byte1 & 0xF, //channel
rx.byte2, //pitch
rx.byte3 //velocity
);
velocity = 0;
break;
case 0xB:
controlChange(
rx.byte1 & 0xF, //channel
rx.byte2, //control
rx.byte3 //value
);
break;
default:
Serial.print("Unhandled MIDI message: ");
Serial.print(rx.header, HEX);
Serial.print("-");
Serial.print(rx.byte1, HEX);
Serial.print("-");
Serial.print(rx.byte2, HEX);
Serial.print("-");
Serial.println(rx.byte3, HEX);
}
//strip.noPixels();
if (velocity < 1) {
// pixels.setPixelColor(0, greenColor);
strip.setPixelColor(0, offColor);
}
if (velocity > 30) {
// pixels.setPixelColor(0, greenColor);
strip.setPixelColor(0, whiteColor);
}
if (velocity > 50) {
// pixels.setPixelColor(1, greenColor);
strip.setPixelColor(1, whiteColor);
}
if (velocity > 75) {
// pixels.setPixelColor(2, greenColor);
strip.setPixelColor(2, whiteColor);
}
if (velocity > 80) {
// pixels.setPixelColor(3, yellowColor);
strip.setPixelColor(3, whiteColor);
}
if (velocity > 90) {
// pixels.setPixelColor(4, yellowColor);
strip.setPixelColor(4, whiteColor);
}
if (velocity > 110 ) {
// pixels.setPixelColor(5, yellowColor);
strip.setPixelColor(5, blueColor);
}
if (velocity > 120) {
strip.setPixelColor(6, blueColor);
}
if (velocity > 126) {
strip.setPixelColor(7, redColor);
}
strip.show();
}
void noPixels()
{
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, offColor);
}
strip.show();
}
It works well but no matter which note is played, the strip always react. I would like to route MIDI channel 16 note A0 to Pin 6 only, note B0 ton PIN 7 only, note C0 to PIN 8 only & note D0 to PIN 9 only.
I defined the 4 strips, but I don't know how to route each MIDI note to the respective strip.
Can you help me ?
Thanks