Hi guys,
that's my first post in this forum. I have read a lot of your posts in the past, but now was the time I couldn't find a solution for this specific problem and wanted to ask if anybody of you could help me with this.
I know a few things about electronics as I completed a vocational education, but I am relatively new into Arduino programming, as this is just a hobby for me.
I think, it's a really simple thing I try to do, and I probably should see the mistake by myself, but I tried now for most of the evening and google researches didn't bring me answers where the mistake in my sketch is.
So, I am trying to make a 1 channel MIDI controller with an ultrasonic sensor as an input. The idea is, to control an audio program like ableton via the ultrasonic sensor quite similar to a theremin or something like that.
First I did a "dry run" with a poti and followed these steps right here:
I did just use the first potentiometer and could succesfully control ableton with it.
I set up the ultrasonic sensor in a second sketch according to this:
I could read out the distance via the serial monitor without any problems.
Then my attempt was to combine these two to one sketch, so I could control ableton via the ultrasonic sensor.
This resulted in the following code:
int val = 0; //Our initial pot values. We need one for the first value and a second to test if there has been a change made. This needs to be done for all 3 pots.
int lastVal = 0;
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup()
{
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
Serial.begin(9600); // Set the speed of the midi port to the same as we will be using in the Hairless Midi software
}
int sensorPin = 0;
int sensorValue = 0;
void loop()
{
digitalWrite (trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite (trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration0.034/2;
if (distance < 31)
{
val = distance4; // Multiply by 4 to get range of about 0-127 for midi
if (val != lastVal) // If the value does not = the last value the following command is made. This is because the pot has been turned. Otherwise the pot remains the same and no midi message is output.
{
MIDImessage(176,1,val);} // 176 = CC command (channel 1 control change), 1 = Which Control, val = value read from Potentionmeter 1 NOTE THIS SAYS VAL not VA1 (lowercase of course)
lastVal = val;
delay(10); //here we add a short delay to help prevent slight fluctuations, knocks on the pots etc. Adding this helped to prevent my pots from jumpin up or down a value when slightly touched or knocked.
}
else
{
delay(10);
return;
}
}
void MIDImessage(byte command, byte data1, byte data2) //pass values out through standard Midi Command
{
Serial.write(command);
}
I think there are just minimal differences to the code from instructables, it also compiles and upload succesfully, but my hairless midi doesn't receive any data. I already double checked settings like baud rate and can't get it.
I set the if clause to distance < 31, because I just wanted to send signals, if I go closer than 30cm to the sensor.
If you need more information on what where my thoughts on a specific part, please ask me.
And again, I am aware that this topic will almost certainly cause annoyance with some of you and I want to excuse in advance, but I really couldn't get any step forward here. Please get me the chance to learn and don't blame me.
I am open for any improvment suggestions ![]()
Thanks and regards,
Sront