Ultrasonic sensor HC-SR04 to MIDI CC issue

Hello everyone, searching for a basic example for a Distance Sensor to MIDI CC I’ve found this code based on Ultrasonic sensor / NewPing.h librarie, but it generates some errors that I’m not able to solve: any suggestion? Thank you

#include <NewPing.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

int cc = 0;

NewPing sonar(12,11,30);

void setup(){
Serial.begin(115200);
}

void loop(){
delay(50);
int uS = sonar.ping();

Serial.print(“Time: “);
Serial.print(uS);
Serial.println(” “);

cc = map(uS, 0, 1700, 0, 127);

Serial.print(“CC: “);
Serial.print(cc);
Serial.println(” “);

}

It compiles without error for me. What error did you get? Is it a 'File not found" error for NewPing.h or MIDI.h that would indicate that one or both of the libraries have not been installed yet?

Hello John, thank you for your kind willingness :slight_smile: actually all libraries are already installed. Please take a look to this log report hoping it would be clear enough about issue.

Ultrasonic_HCSR04_MIDI_CC_2.ino:19: error: stray '\342' in program
 Serial.print(“Time: “);
 ^
Ultrasonic_HCSR04_MIDI_CC_2.ino:19: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:19: error: stray '\234' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:19: error: stray '\342' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:19: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:19: error: stray '\234' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:21: error: stray '\342' in program
 Serial.println(” “);
 ^
Ultrasonic_HCSR04_MIDI_CC_2.ino:21: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:21: error: stray '\235' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:21: error: stray '\342' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:21: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:21: error: stray '\234' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:25: error: stray '\342' in program
 Serial.print(“CC: “);
 ^
Ultrasonic_HCSR04_MIDI_CC_2.ino:25: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:25: error: stray '\234' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:25: error: stray '\342' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:25: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:25: error: stray '\234' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:27: error: stray '\342' in program
 Serial.println(” “);
 ^
Ultrasonic_HCSR04_MIDI_CC_2.ino:27: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:27: error: stray '\235' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:27: error: stray '\342' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:27: error: stray '\200' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:27: error: stray '\234' in program
Ultrasonic_HCSR04_MIDI_CC_2.ino:1: error: 'https' does not name a type

 ^
Ultrasonic_HCSR04_MIDI_CC_2.ino:9: error: 'NewPing' does not name a type
 NewPing sonar(12,11,30);
 ^
/Users/lionacid/Documents/Arduino/Ultrasonic_HCSR04_MIDI_CC_2.ino/Ultrasonic_HCSR04_MIDI_CC_2.ino.ino: In function 'void loop()':
Ultrasonic_HCSR04_MIDI_CC_2.ino:17: error: 'sonar' was not declared in this scope
 int uS = sonar.ping();
          ^
Ultrasonic_HCSR04_MIDI_CC_2.ino:19: error: 'Time' was not declared in this scope
 Serial.print(“Time: “);
                 ^
Ultrasonic_HCSR04_MIDI_CC_2.ino:25: error: 'CC' was not declared in this scope
 Serial.print(“CC: “);
                 ^
exit status 1
stray '\342' in program

“error: stray ‘\xxx’ in program” means you have characters with values over 127 in your sketch. This often happens when a website or text editor has decided to make your text prettier.

Something has replaced many of the double-quote characters in your sketch with ‘fancy quotes’. If you look carefully you will see that the opening quotes and closing quotes look different. Replace every one with a regular double-quote.

There may be other characters that were ‘pretified’, like using different width spaces or ‘non-breaking spaces’. You may have to re-type each line that has a “stray ‘\xxx’ in program” error.

The error messages say that you have ‘https’ on line 1 of your sketch. You didn’t show that part. That bogus line is causing the “#include <NewPing.h>” to fail so you get errors when you try to use names that would have been defined in that file.

got it! Infact it’s a copy\past from an http address… the code is loaded now (thank you) though at last it’s doesn’t work properly as MIDI CC: any advice by you?

You never actually call the MIDI library to send the message so that would explain why it doesn’t do what you want.