Hello everyone,
I am new to teensy board, I had a working setup to control my LEDs to turn on when i press a key on my digital piano. But my arduino uno R3 was too slow so i want to move my setup to work with a teensy 3.6.
Now I want to send serial data from a USB MIDI Board (It translates MIDI data to serial) to my teensy 3.6
Now I tried to do that but apparently the teensy is not responding to my commands.
Can you please help me in that regards? I will post the code that I am using below (coded on arduino IDE)
#include <MIDI.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include "noteList.h"
#include "pitches.h"
MIDI_CREATE_DEFAULT_INSTANCE();
static const unsigned sMaxNumNotes = 16;
MidiNoteList<sMaxNumNotes> midiNotes;
byte curWheelPos = 0;
#define KEYBOARD_LOWEST_NOTE 21
#define KEYBOARD_HIGHEST_NOTE 108
#define STRIP_REVERSED true
#define STRIP_PIN 12
#define STRIP_NUM_PIXELS 73
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_NUM_PIXELS, STRIP_PIN, NEO_GRB + NEO_KHZ800);
// -----------------------------------------------------------------------------
inline void handleGateChanged(bool inGateActive)
{
digitalWrite(LED_BUILTIN, inGateActive ? LOW : HIGH);
}
inline void pulseGate()
{
handleGateChanged(false);
delay(10);
handleGateChanged(true);
}
// -----------------------------------------------------------------------------
byte noteToStripIndex(byte pitch)
{
byte index;
if(pitch < KEYBOARD_LOWEST_NOTE)
index = 0;
else if(pitch > KEYBOARD_HIGHEST_NOTE)
index = STRIP_NUM_PIXELS -1;
else
index = (STRIP_NUM_PIXELS -1) * (pitch - KEYBOARD_LOWEST_NOTE) / (KEYBOARD_HIGHEST_NOTE - KEYBOARD_LOWEST_NOTE);
if(STRIP_REVERSED)
index = (STRIP_NUM_PIXELS -1) - index;
return index;
}
void handleNotesChanged(bool isFirstNote = false)
{
if (midiNotes.empty())
{
strip.clear();
strip.show();
handleGateChanged(false);
}
else
{
byte pitch = 0;
strip.clear();
for (byte i = 0; i < midiNotes.size(); ++i)
{
if(midiNotes.get(i, pitch))
{
strip.setPixelColor(noteToStripIndex(pitch), 0,255,119);
curWheelPos++;
}
}
strip.show();
if (midiNotes.getLast(pitch))
{
if (isFirstNote)
{
handleGateChanged(true);
}
else
{
pulseGate(); // Retrigger envelopes. Remove for legato effect.
}
}
}
}
// -----------------------------------------------------------------------------
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
{
const bool firstNote = midiNotes.empty();
midiNotes.add(MidiNote(inNote, inVelocity));
//digitalWrite(LED_BUILTIN, LOW);
handleNotesChanged(firstNote);
}
void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
{
midiNotes.remove(inNote);
//digitalWrite(LED_BUILTIN, HIGH);
handleNotesChanged();
}
// -----------------------------------------------------------------------------
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
//Serial.begin(115200);//For debug only
digitalWrite(LED_BUILTIN, HIGH);
strip.begin();
//strip.show(); // Initialize all pixels to 'off'
//rainbow(10);
strip.clear();
strip.show();
}
void loop()
{
MIDI.read();
}
I am new to teensy board, I had a working setup to control my LEDs to turn on when i press a key on my digital piano. But my arduino uno R3 was too slow so i want to move my setup to work with a teensy 3.6.
Now I want to send serial data from a USB MIDI Board (It translates MIDI data to serial) to my teensy 3.6
Now I tried to do that but apparently the teensy is not responding to my commands.
Can you please help me in that regards? I will post the code that I am using below (coded on arduino IDE)
#include <MIDI.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include "noteList.h"
#include "pitches.h"
MIDI_CREATE_DEFAULT_INSTANCE();
static const unsigned sMaxNumNotes = 16;
MidiNoteList<sMaxNumNotes> midiNotes;
byte curWheelPos = 0;
#define KEYBOARD_LOWEST_NOTE 21
#define KEYBOARD_HIGHEST_NOTE 108
#define STRIP_REVERSED true
#define STRIP_PIN 12
#define STRIP_NUM_PIXELS 73
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_NUM_PIXELS, STRIP_PIN, NEO_GRB + NEO_KHZ800);
// -----------------------------------------------------------------------------
inline void handleGateChanged(bool inGateActive)
{
digitalWrite(LED_BUILTIN, inGateActive ? LOW : HIGH);
}
inline void pulseGate()
{
handleGateChanged(false);
delay(10);
handleGateChanged(true);
}
// -----------------------------------------------------------------------------
byte noteToStripIndex(byte pitch)
{
byte index;
if(pitch < KEYBOARD_LOWEST_NOTE)
index = 0;
else if(pitch > KEYBOARD_HIGHEST_NOTE)
index = STRIP_NUM_PIXELS -1;
else
index = (STRIP_NUM_PIXELS -1) * (pitch - KEYBOARD_LOWEST_NOTE) / (KEYBOARD_HIGHEST_NOTE - KEYBOARD_LOWEST_NOTE);
if(STRIP_REVERSED)
index = (STRIP_NUM_PIXELS -1) - index;
return index;
}
void handleNotesChanged(bool isFirstNote = false)
{
if (midiNotes.empty())
{
strip.clear();
strip.show();
handleGateChanged(false);
}
else
{
byte pitch = 0;
strip.clear();
for (byte i = 0; i < midiNotes.size(); ++i)
{
if(midiNotes.get(i, pitch))
{
strip.setPixelColor(noteToStripIndex(pitch), 0,255,119);
curWheelPos++;
}
}
strip.show();
if (midiNotes.getLast(pitch))
{
if (isFirstNote)
{
handleGateChanged(true);
}
else
{
pulseGate(); // Retrigger envelopes. Remove for legato effect.
}
}
}
}
// -----------------------------------------------------------------------------
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
{
const bool firstNote = midiNotes.empty();
midiNotes.add(MidiNote(inNote, inVelocity));
//digitalWrite(LED_BUILTIN, LOW);
handleNotesChanged(firstNote);
}
void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
{
midiNotes.remove(inNote);
//digitalWrite(LED_BUILTIN, HIGH);
handleNotesChanged();
}
// -----------------------------------------------------------------------------
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
//Serial.begin(115200);//For debug only
digitalWrite(LED_BUILTIN, HIGH);
strip.begin();
//strip.show(); // Initialize all pixels to 'off'
//rainbow(10);
strip.clear();
strip.show();
}
void loop()
{
MIDI.read();
}