Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * MIDI_theremin.ino
- * featuring a button! and a ToF sensor! and an ultrasonic sensor! and another button!
- * July 2018
- * ~ anonymous
- */
- #include "Adafruit_VL53L0X.h"
- #include "MIDIUSB.h"
- #include "NewPing.h"
- #define ENABLE_PIN 10 // to enable the instrument
- #define SET_RANGE_PIN 11 // used to setup MIDI volume mapping in Ableton
- #define TRIGGER_PIN 5 // HC-SR04 trigger
- #define ECHO_PIN 4 // HC-SR04 echo
- #define MAX_DISTANCE 150 // max HC-SR04 range
- #define PING_SAMPLES 3 // number of samples to use with NewPing median mode
- int RANGE_DIVIDER = 4; // determines pitch sensitivity
- int RANGE_OFFSET = 0; // offset pitch
- bool keyDown = false;
- VL53L0X_RangingMeasurementData_t rangeMeasurement;
- Adafruit_VL53L0X lox = Adafruit_VL53L0X();
- uint8_t current_range = 0;
- uint8_t last_range = 0;
- uint8_t current_volume = 0;
- uint8_t last_volume = 0;
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
- 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 setup() {
- pinMode(ENABLE_PIN, INPUT);
- pinMode(SET_RANGE_PIN, INPUT);
- Serial.begin(115200);
- while (!Serial) {
- delay(1);
- }
- Serial.println("Starting...");
- Serial.flush();
- lox.begin();
- }
- void loop() {
- lox.rangingTest(&rangeMeasurement, false);
- if (rangeMeasurement.RangeStatus == 4) {
- current_range = 0;
- } else {
- current_range = (rangeMeasurement.RangeMilliMeter/RANGE_DIVIDER) + RANGE_OFFSET;
- }
- current_volume = 64 - map(sonar.ping_median(PING_SAMPLES) / 58, 0, MAX_DISTANCE, 0, 64);
- if (digitalRead(ENABLE_PIN) == LOW && keyDown) {
- noteOff(0, last_range, 64);
- //noteOff(15, last_volume, 64);
- MidiUSB.flush();
- keyDown = false;
- Serial.println("Sending note off");
- Serial.flush();
- } else if (digitalRead(ENABLE_PIN) == HIGH && !keyDown) {
- noteOn(0, current_range, 64);
- noteOn(15, current_volume, 64);
- MidiUSB.flush();
- last_range = current_range;
- last_volume = current_volume;
- keyDown = true;
- Serial.println("Sending note on");
- Serial.flush();
- } else if (current_range != last_range && keyDown) {
- noteOff(0, last_range, 64);
- //noteOff(15, last_volume, 64);
- MidiUSB.flush();
- noteOn(0, current_range, 64);
- noteOn(15, current_volume, 64);
- MidiUSB.flush();
- last_range = current_range;
- last_volume = current_volume;
- keyDown = true;
- Serial.println("Changing note");
- }
- // use this to set the MIDI mapping range for volume control in Ableton
- if (digitalRead(SET_RANGE_PIN) == HIGH) {
- noteOn(15, 0, 64);
- noteOn(15, 64, 64);
- MidiUSB.flush();
- Serial.println("Setting range");
- Serial.flush();
- delay(500);
- noteOff(15, 0, 64);
- noteOff(15, 64, 64);
- MidiUSB.flush();
- delay(100);
- }
- MidiUSB.flush();
- delay(20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement