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);
}
}
}