Ultrasonic distance sensor HC-SR04 Readings Low

Hello Arduino lovers,

For a project I am using a HC-SR04 range sensor set up with a Arduino Redboard (which I think is just the UNO) and Processing to project images onto a wall. Based upon ones distance form the sensor, the images will change and preform almost like a interactive stop motion that the persons distance is controlling. I have 72 images in all. The space that people will be walking into to view it is a hallway of about 6.5 feet. I want the images to change within that entire range (minus like 5 inches), but I am only getting readings up to about 2.5~3ft. I am pointing it everywhere to try to get result and I am always getting the same, not sure what I am doing wrong. If anyone has any hints or suggestions I would greatly appreciate them! Sorry I am kinda new to all of this!

Here is my Arduino code

#define echoPin 3
#define trigPin 4
#define LEDPin 13 // ignore

// Maximum range needed
int maximumRange = 72;

// Minimum range needed
int minimumRange = 10;

// image to show
int imagetoshow = 0;

// Duration used to calculate distance
long duration, distance;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // Use LED indicator (if required)
  pinMode(LEDPin, OUTPUT);
}

void loop() {
  /* The following trigPin/echoPin cycle is used to determine the distance of
  the nearest object by bouncing soundwaves off of it */
 
 digitalWrite(trigPin, LOW);
delayMicroseconds(2);

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

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

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
  /* Send a negative number to computer and Turn LED ON to 
  indicate "out of range" */
  Serial.println("-1");
  digitalWrite(LEDPin, HIGH);
}
else {
  /* Send the distance to the computer using Serial protocol, and
  turn LED OFF to indicate successful reading */
  //Serial.println(distance);
  digitalWrite(LEDPin, LOW);
  
  // map(sensorvalue, sensor min, sensor max, 1, 36);
  imagetoshow = map(distance, 15, 85, 15, 85);
  
  Serial.println(imagetoshow);
  
}

//Delay 50ms before next reading.
delay(200);
}

Here is what's going on in processing

import processing.serial.*;
 
int MAX = 72;

// Load all the images
PImage[] images = new PImage[MAX]; //MAX = 73
PImage img;

//Include serial communication
Serial myPort;

// Value of sensor
int val = 0;

int i = 0;

// Counter variable used to create graph
int count = 0;

void setup(){
  
  background(0);
  size(1440,850);
  //size(displayWidth, displayHeight);
  smooth();
  
  // Lets load all the images
  for (int i = 0; i < MAX; i++) {
    images[i] = loadImage(i + ".jpg");
  }
  // Set up serial connection
  myPort = new Serial(this, Serial.list()[0], 9600);
  
  // Prints out available serial ports
  println(Serial.list());

  
  
}

void draw(){
}
  
// When we hear something form the serial port
void serialEvent(Serial myPort)
{
  // Read the data until the new line character
  String data = myPort.readStringUntil(10);
  
  // If data exists
  if(data != null)
  {
    // Convert String into an integer
    val = int(trim(data));
     
   println(val);
  if(val == -1){
     img = loadImage("73.jpg");
      image(img, 90, 0);
   }
  else {
   int newval = int(map(val, 0, 72, 0, 72));
   image(images[newval], 90, 0);
  }
  
  }
}

The sensor may be defective. Test it with the simplest program you can find, in an area that is free of obstacles (except a target). If the sensor still misbehaves, there will be a better chance that someone can help if you post both that simple program and some example results.

Just a couple of things to look into:

.1. The trigger signal has to be above 10uS. I remember a similar sensor that needed at least 15 uS.
.2. Do you have an Oscilloscope around? It should be pretty easy to see the trigger / echo signals. Judging by the formula the sensor keeps the pulse HIGH for 5uS for every mm .
.3. I haven't used Arduino software to much, but shouldn't you specify where the pulseIn function is defined? If you haven't received any comping errors/warning , then I would imagine is a function included in the standard software (?)
4. Is your arduino board powered at 5V? Is your sensor powered at 5V? Are the grounds connected? (The ground if your arduino board to the ground if your sensor)
5. And ... it should be unsigned long distance instead of long distance. You don't want to store the sign on the first bit (especially since the pulseIn function returns a unsigned long). It might mess up your calculation.

Let us know how it goes.

Ok, I'm still a noob also, but I have this sensor and played with it as well. Correct me if I'm wrong, but this statement:

/ Maximum range needed
int maximumRange = 72;

Limits your range to 72cm or about 28" or the 2.5-3 feet you are getting. Try upping that number.