Created
May 15, 2023 02:34
-
-
Save ianthehenry/bebb537302c0e3a371252207799aa5f4 to your computer and use it in GitHub Desktop.
Binary synthesizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MIDIUSB.h" | |
#define STATUS_LED 13 | |
int pressed[8] = {0, 0, 0, 0, 0, 0, 0, 0}; | |
int intervals[8] = {1, 2, 4, 8, -1, -2, -4, -8}; | |
int rows[2] = {5, 6}; | |
int cols[4] = {9, 10, 11, 12}; | |
int note = 0; | |
void setup() { | |
for (int row = 0; row < 2; row++) { | |
pinMode(rows[row], OUTPUT); | |
digitalWrite(rows[row], HIGH); | |
} | |
for (int col = 0; col < 4; col++) { | |
pinMode(cols[col], INPUT_PULLUP); | |
} | |
pinMode(STATUS_LED, OUTPUT); | |
} | |
void noteOn(byte channel, byte pitch, byte velocity) { | |
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity}; | |
MidiUSB.sendMIDI(noteOn); | |
} | |
void noteOff(byte channel, byte pitch, byte velocity) { | |
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity}; | |
MidiUSB.sendMIDI(noteOff); | |
} | |
void loop() { | |
int light = 0; | |
for (int row = 0; row < 2; row++) { | |
digitalWrite(rows[row], LOW); | |
for (int col = 0; col < 4; col++) { | |
int i = row * 4 + col; | |
pressed[i] = digitalRead(cols[col]) == LOW; | |
} | |
digitalWrite(rows[row], HIGH); | |
} | |
int prevNote = note; | |
int any = 0; | |
note = 60; | |
for (int i = 0; i < 8; i++) { | |
if (pressed[i]) { | |
any = 1; | |
note += intervals[i]; | |
} | |
} | |
if (any) { | |
digitalWrite(STATUS_LED, HIGH); | |
} else { | |
digitalWrite(STATUS_LED, LOW); | |
note = 0; | |
} | |
if (note != prevNote) { | |
if (prevNote != 0) { | |
noteOff(0, prevNote, 64); | |
} | |
if (note != 0) { | |
noteOn(0, note, 64); | |
} | |
} | |
MidiUSB.flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment