Hi there,
Was just wondering if somebody could possibly help me with some code?
I had this all working fine (I'm fairly certain I did anyway) but left the project for about a year or so and in that time for some reason it now doesn't do everything it used to do. I don't know why that is as I've literally not touched the board since that time so the code on the board is exactly as I left it.
I have code which is more or less working fully which is basically code that when you press a key on a digital piano (MIDI based) then an LED strip (WS812B) lights up the corresponding LEDs above the key pressed. The lights all light up fine but I'm having a couple of problems which I need help with.
The first being an Arduino IDE issue in that I have some serial print lines in the code to help me follow what is going on except they are firing all the while (they're inside the void loop part of the code) and so the serial monitor is scrolling all the while and is impossible to read. I've tried clicking on the Toggle Autoscroll button (OSX) but it never stops scrolling so I've got no real chance of reading the values very easily
The second issue being that in my project I have 3 potentiometers attached to the board to be able to set the hue, saturation and brightness (value) of the LEDs. The hue potentiometer works fine and changes the colour of the LEDs but now for some reason the other two aren't changing the LEDs in any way at all.
I've checked that the pots are still working correctly with an ohmeter and they are all working fine so not too sure what is going on.
Therefore to cover these issues I'd like to be able to in my code only print out the values in the loop from each of the pots being read but only if the value is different to the previous read value.
This will enable me to not have the serial monitor spitting out tons and tons of data and also I'll be able to see right away if the values are changing or not.
Also if anyone can see any glaring reason for the saturation and value (brightness) code not working (it's definitely possible that I didn't have this all working after all but it's been such a long time that I'm not totally sure now - I'm fairly positive I remember seeing all the pots working though) then I'd really appreciate any help with that. Thank you.
The code so far is as follows :
/*
MIDIUSB_test.ino
Created: 4/6/2015 10:47:08 AM
Author: gurbrinder grewal
Modified by Arduino LLC (2015)
*/
// a lookup table to convert pitch to text
const char pitchLUT[][4] =
{
" A0", "B0b", " B0", " C1", "D1b", " D1", "E1b", " E1", " F1", "G1b",
" G1", "A1b", " A1", "B1b", " B1", " C2", "D2b", " D2", "E2b", " E2",
" F2", "G2b", " G2", "A2b", " A2", "B2b", " B2", " C3", "D3b", " D3",
"E3b", " E3", " F3", "G3b", " G3", "A3b", " A3", "B3b", " B3", " C4",
"D4b", " D4", " E4b", " E4", " F4", "G4b", " G4", "A4b", "A4", "B4b",
" B4", " C5", "D5b", " D5", "E5b", " E5", " F5", "G5b", " G5", "A5b",
" A5", "B5b", " B5", " C6", "D6b", " D6", "E6b", " E6", " F6", "G6b",
" G6", "A6b", " A6", "B6b", " B6", " C7", "D7b", " D7", "E7b", " E7",
" F7", "G7b", " G7", "A7b", " A7", "B7b", " B7", " C8",
};
#include "MIDIUSB.h"
#include "FastLED.h"
#define DEBUG 1 // Comment out to stop verbose serial messages
#define DATA_PIN 3 // Output Pin to Data Line on Strip
#define COLOR_ORDER GRB // I had to change this for my strip if your color is off then you know.
#define NUM_LEDS 176 // # of LEDS in the strip
#define BRIGHTNESS 220 // Maximum brightness of LEDs
#define LED_TYPE WS2812B // WS2812B or WS2811 or whatever
#define MAX_POWER_MILLIAMPS 4000 // Max power draw that LED strip can pull
#define HUE_POT_PIN A0 // Pin that Pot is attached to for setting LED hue
#define SATURATION_POT_PIN A1 // Pin that Pot is attached to for setting LED saturation
#define VALUE_POT_PIN A2 // Pin that Pot is attached to for setting LED value (brightness)
CRGB leds[NUM_LEDS];
uint8_t
evtCIN,
evtType,
evtChan,
evtPitch,
evtVel,
pitch;
char
szStr[40];
void setup()
{
// Sanity check delay - allows reprogramming if accidently blowing power w/leds
delay(2000);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setMaxPowerInVoltsAndMilliamps( 5, MAX_POWER_MILLIAMPS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
Serial.begin(115200);
}
void loop()
{
// Read our HUE potentiometer (A0) input and set a variable hue_pot to be used below
uint16_t huePotRead = analogRead(HUE_POT_PIN);
uint8_t hue_pot = map(huePotRead, 0, 1023, 0, 255);
Serial.print( "Hue = ");
Serial.println( hue_pot );
// Read our SATURATION potentiometer (A1) input and set a variable saturation_pot to be used below
uint16_t saturationPotRead = analogRead(SATURATION_POT_PIN);
uint8_t saturation_pot = map(saturationPotRead, 0, 1023, 0, 255);
Serial.print( "Saturation = ");
Serial.println( saturation_pot );
// Read our VALUE potentiometer (A2) input and set a variable value_pot to be used below
uint16_t valuePotRead = analogRead(VALUE_POT_PIN);
uint8_t value_pot = map(valuePotRead, 0, 1023, 0, 255);
Serial.print( "Value = ");
Serial.println( value_pot );
// Fade all LEDs down by 1 in brightness each time this is called
// fadeToBlackBy(leds, NUM_LEDS-1, 10);
// FastLED.show();
midiEventPacket_t rx = MidiUSB.read();
if ( rx.header != 0 )
{
do
{
#ifdef DEBUG
sprintf( szStr, "Packet: %02X - %02X - %02X - %02X",
rx.header,
rx.byte1,
rx.byte2,
rx.byte3 );
// Serial.println( szStr );
#endif
evtCIN = rx.header & 0xf;
evtType = (rx.byte1 >> 4) & 0xf;
evtChan = rx.byte1 & 0xf;
evtPitch = rx.byte2;
evtVel = rx.byte3;
switch ( evtCIN )
{
case 0x8:
case 0x9:
if ( evtPitch >= 21 && evtPitch <= 108 )
{
evtPitch -= 21;
if ( evtType == 0x8 || (evtType == 0x9 && evtVel == 0) )
{
leds[evtPitch << 1] = CRGB::Black;
leds[(evtPitch << 1) + 1] = CRGB::Black;
// Fade all LEDs down by 1 in brightness each time this is called
// leds[evtPitch << 1].fadeToBlackBy(0);
// leds[(evtPitch << 1) + 1].fadeToBlackBy(0);
}
else
{
// LED Brightness set depending upon velocity key is played at!
//leds[evtPitch << 1] = CHSV(150, 255, evtVel*2);
//leds[(evtPitch << 1) + 1] = CHSV(150, 255, evtVel*2);
// Standard way of setting LEDs using CHSV
// int LED_HUE = random(255);
// leds[evtPitch << 1] = CHSV(LED_HUE, 255, 255);
// leds[(evtPitch << 1) + 1] = CHSV(LED_HUE, 255, 255);
// Set LEDS using potentiometers
leds[evtPitch << 1] = CHSV(hue_pot, saturation_pot, value_pot);
leds[(evtPitch << 1) + 1] = CHSV(hue_pot, saturation_pot, value_pot);
}// Else
}// If
else
{
// Illegal note
// Serial.println( "Unknown note" );
}// else
break;
default:
break;
}// Switch
FastLED.show();
#ifdef DEBUG
sprintf( szStr, "Type: %1X Chan: %d Note: %s Velocity: %d",
evtType,
evtChan,
pitchLUT[evtPitch],
evtVel );
Serial.println( szStr );
#endif
rx = MidiUSB.read();
} while (rx.header != 0);
}// If
}// Loop
Many thanks in advance for reading this far and for any help anyone can offer with this.
Mark