Skip to content

Commit

Permalink
feat: Use NeoPixel to show state
Browse files Browse the repository at this point in the history
  • Loading branch information
madskjeldgaard committed May 9, 2024
1 parent 65b0d4c commit 2143c19
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project is a simple Sustain pedal to midi note converter based on a raspber
- Dirt cheap (all you need is a mono jack socket and a raspberry pi pico)
- Sends note on/off
- Note number can be changed easily by sending a midi note to the device.
- (Optional) Use a WS2812 NeoPixel to show the state and midi note value via colour (I use [this module](https://www.aliexpress.com/item/1005001565556198.html?spm=a2g0o.order_list.order_list_main.4.17d91802H9j6ak) but I think any one should work) – disable this using the `SUS2MIDI_NEOPIXEL` build flag in the .ini file.

## Dependencies

Expand Down
6 changes: 3 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[env]
framework = arduino
build_unflags = -std=gnu++11
build_flags = -std=gnu++17 -fconcepts -DUSE_TINYUSB
build_flags = -std=gnu++17 -fconcepts -DUSE_TINYUSB -DSUS2MIDI_NEOPIXEL=true
lib_deps =
# USB support
Adafruit TinyUSB Library
# MIDI
https://github.com/FortySevenEffects/arduino_midi_library
# Button debounce
thomasfredericks/Bounce2@^2.72


# Neopixel
adafruit/Adafruit NeoPixel@^1.12.2

[env:raspberrypi-pico]
; See https://arduino-pico.readthedocs.io/en/latest/platformio.html
Expand Down
54 changes: 52 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <Adafruit_TinyUSB.h>
#include <Arduino.h>
#include <MIDI.h>

#include <Bounce2.h>
Bounce2::Button sustainPedal1 = Bounce2::Button();

#define SUS2MIDI_NEOPIXEL = true

constexpr auto SUSTAIN_PIN = 0;
constexpr auto MIDI_VELOCITY = 127;
constexpr auto TO_CHANNEL = 1;
Expand All @@ -13,12 +14,36 @@ constexpr auto PIN = SUSTAIN_PIN;
constexpr auto PIN_MODE = INPUT_PULLUP;
constexpr auto INTERVAL_IN_MS = 5;

#ifdef SUS2MIDI_NEOPIXEL
#include <Adafruit_NeoPixel.h>
constexpr auto LED_BRIGHTNESS = 20; // Set LED brightness (0-255)
constexpr auto LED_PIN = 5; // Change this to your LED pin
constexpr auto NUM_LEDS = 1; // Change this to the number of LEDs
// NeoPixel object
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
#endif

// USB MIDI object
Adafruit_USBD_MIDI usbMidi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usbMidi, MIDI);

static bool toggle_state1 = false;
static bool toggle_state1 = true;

#ifdef SUS2MIDI_NEOPIXEL
static int output_note = 48; // Default output note
static int red_value = 0; // Red color value
static int green_value = 0; // Green color value

void update_led(bool is_on) {
if (is_on) {
strip.setPixelColor(0, strip.Color(red_value, green_value, 0));
} else {
strip.setPixelColor(0, strip.Color(0, 0, 0)); // Turn off LED
}
strip.show(); // Update LED
}

#endif

void handle_sustain() {
sustainPedal1.update();
Expand All @@ -31,6 +56,10 @@ void handle_sustain() {
MIDI.sendNoteOff(output_note, 0, TO_CHANNEL);
}

#ifdef SUS2MIDI_NEOPIXEL
update_led(toggle_state1);
#endif

toggle_state1 = !toggle_state1;
}
}
Expand All @@ -41,10 +70,19 @@ void handle_midi_note_on(byte channel, byte note, byte velocity) {

if (toggle_state1) {
MIDI.sendNoteOff(output_note, 0, TO_CHANNEL);
#ifdef SUS2MIDI_NEOPIXEL
update_led(false);
#endif
}

toggle_state1 = false;
output_note = note;

#ifdef SUS2MIDI_NEOPIXEL
// Map note number to red and green color values
red_value = map(output_note, 0, 127, 255, 0);
green_value = map(output_note, 0, 127, 0, 255);
#endif
}

void setup() {
Expand All @@ -63,6 +101,18 @@ void setup() {
sustainPedal1.attach(PIN, PIN_MODE);
sustainPedal1.interval(INTERVAL_IN_MS);
sustainPedal1.setPressedState(LOW);
sustainPedal1.update();

// Update button state to initialize toggle state
#ifdef SUS2MIDI_NEOPIXEL
// Initialize NeoPixel strip
strip.begin();
strip.setBrightness(LED_BRIGHTNESS); // Set LED brightness
// Map note number to red and green color values
red_value = map(output_note, 0, 127, 255, 0);
green_value = map(output_note, 0, 127, 0, 255);
strip.show(); // Initialize all pixels to 'off'
#endif
}

void loop() {
Expand Down

0 comments on commit 2143c19

Please sign in to comment.