Depth sensor troubleshoot!

I’m trying to get a depth sensor to return data to me which I’m going to end up using to manipulate sound in Max. I’m testing this with both an HC-SR04 ultrasonic sensor and a Sharp IR (GP2Y0A710F) distance sensor.

When I try to serial print it only returns question marks, when I should be seeing numerical data.

  • I have tried 2 different Arduino UNOs, restarted my computer, and have made sure the proper ports are set up & the wiring is correct.

Here’s the code I’m using to try and get the ultrasonic sensor to return data:

#define echoPin 11
#define trigPin 12
#define LEDPin 13

int maximumRange = 200;
int minimumRange = 0;
long duration, distance;

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT);

}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){

Serial.println("-1");
digitalWrite(LEDPin, LOW);
}

delay(50);
}

When I try to serial print it only returns question marks, when I should be seeing numerical data.

That is odd, because you don't seem to be printing question marks.

Have you set the serial monitor line speed?

Thanks for the reply,

Yes it’s set to 57600 baud. Here’s what I’m seeing:

Yes it's set to 57600 baud

Take a look at the code you wrote.

Is the duration returning what you think? Try initialise it to something that you know will be valid and if it doesn't change then maybe the cause?

Also I never see you printing the distance in your sketch. Only a "-1". Your LED will also be off (assuming pin LOW means LED is off, otherwise it'll be on) at all times.