Hello,
I realize this has been discussed at length in other posts however I can't seem to find one that fits this exact scenario - in terms that I'm understanding.
I am attempting to use a sparkfun ultrasonic sensor (HC-SR04) to send MIDI CC values via middleware (hairless MIDI) to ableton/Max.
I've examined my code carefully and I'm not clear on why this isn't working. The code compiles and I am able to map to a sufficient CC value, however I can't transmit it to the middleware.
Any help is much appreciated. Thanks!
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
struct MySettings : public midi::DefaultSettings
{
static const bool UseRunningStatus = false;
static const long BaudRate = 115200;
};
//declare custom MIDI instance. Formatted as (Serial Type (HW of SW), name of arduino serial, name of //port, custom settings)
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings);
//declaring variable for previous MIDI value
int oldMIDIValue = 0;
//declaring variables for distance sensor
int trigPin = 10;
int echoPin = 13;
float duration, distance;
void setup() {
//Start listening for MIDI on the serial port. Used in place of Serial.begin()
MIDI.begin();
//setup for distance sensor
//Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
//Start the trigger at low
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//Send a trigger pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
//go back to not sending a pulse
digitalWrite(trigPin, LOW);
//define the duration
duration = pulseIn(echoPin, HIGH);
//calculate the distance between object and sensor based on response time
distance = (duration/2)*0.034;
//map the the distance between 0 and 127 (possible midi cc values)
oldMIDIValue = round(map(distance, 2, 400, 0, 127));
int newMIDIValue = round(map(distance, 2, 400, 0, 127));
if(newMIDIValue != oldMIDIValue){
//Send MIDI CC (Control Number (0-127), pot reading, MIDI Channel (1-16))
MIDI.sendControlChange(01, newMIDIValue, 1);
oldMIDIValue = newMIDIValue;
}
// if (distance >=400 || distance <= 2){
// Serial.println("out of range");
// }else{
// Serial.print("MIDI Value: ");
// Serial.println(newMIDIValue);
// //Serial.print("Distance: ");
// //Serial.println(distance);
// }
}