Please help... Processing Arduino with 2 HC-SR04 Ultrasonic Sensors.
in
Integration and Hardware
•
5 months ago
Hi there,
I am in desperate need of help. I am doing a school project using processing, Arduino with 2 ultrasonic sensors and what I am trying to do is change between 7 images by using the sensors (left or right) unfortunately I haven’t been able to achieve this as I am really new to Arduino and processing and I don’t understand where I am going wrong.
The two sensors will be set apart so that when the user approaches the left or right sensor the image changes either going back to the previous image or go to the next image. I want to trigger the change when the distance is between 50 and 60 cm so that the images don’t get triggered when you are too close or too far.
The codes that I am using are bellow. Please take a look and help solve this. I only have 4 days to do it plus evaluate it.
Thank you so so much I am very grateful to you all.
Arduino code:
#include <NewPing.h>
int generalDist = 50;
int TRIGGER_PIN1 = 2 ; // Arduino pin tied to trigger pin on the ultrasonic sensor.
int ECHO_PIN1 = 4 ; // Arduino pin tied to echo pin on the ultrasonic sensor.
int TRIGGER_PIN2 = 7 ; // Arduino pin tied to trigger pin on the ultrasonic sensor.
int ECHO_PIN2 = 8 ; // Arduino pin tied to echo pin on the ultrasonic sensor.
//#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
//#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
int MAX_DISTANCE1 = 200 ;// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int MAX_DISTANCE2 = 200 ;// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
//#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int dist1=0;
int dist2=0;
unsigned int uS;
int val= 0;
//int Delay_Ms = 300; //delay value for setting tempo
// int range = 0;
NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE1); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
delay(500); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
uS = sonar1.ping(); // Send ping, get ping time in microseconds (uS).
MAX_DISTANCE1 = ((uS / US_ROUNDTRIP_CM)); // Convert ping time to distance in cm and print result (0 = outside set distance range)
delay(100);
uS = sonar2.ping(); // Send ping, get ping time in microseconds (uS).
MAX_DISTANCE2 = ((uS / US_ROUNDTRIP_CM)); // Convert ping time to distance in cm and print result (0 = outside set distance range)
delay(500);
}
void loop() {
delay(100);
int uS = sonar1.ping(); // Send ping, get ping time in microseconds (uS).
dist1 = (uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
delay (200);
uS = sonar2.ping(); // Send ping, get ping time in microseconds (uS).
dist2 = (uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//range = map(dist, 0, 50, 15, 0);
// Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
//int uS = sonar1.ping(); // Send ping, get ping time in microseconds (uS).
// Serial.print(" ");
delay (500);
Serial.write(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//Serial.print("L");
Serial.write(dist1);
if (dist1<generalDist){
Serial.println(1);
}
else{
Serial.println(0);
}
//Serial.println ("");
//int uS = sonar2.ping(); // Send ping, get ping time in microseconds (uS).
//Serial.print(dist);
delay (100);
Serial.write(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//Serial.print("R");
Serial.write(dist2);
if (dist2<generalDist){
Serial.println(2);
}
else{
Serial.println(0);
}
Serial.println(dist2);
}
Processing Code:
[td]import processing.serial.*;
// curent state
int currentState=0;
// images and text to display
ArrayList<PImage> displayImage= new ArrayList<PImage>();
int sensorValue=0;
int sensorMin=1023;
int sensorMax=0;
int leftSensorPin=4; // the left sensor pin
int rightSensorPin=8; // the right sensor pin
boolean leftSensorTriggered=false;
boolean rightSensorTriggered=false;
Serial myPort; // Create object from Serial class
int val=0; // Data received from the serial port
void setup()
{
size(900, 700);
displayImage.add( loadImage("1.jpg"));
displayImage.add(loadImage("2.jpg"));
displayImage.add(loadImage("3.jpg"));
displayImage.add(loadImage("4.jpg"));
displayImage.add(loadImage("5.jpg"));
displayImage.add(loadImage("6.jpg"));
displayImage.add(loadImage("7.jpg"));
// etc for other images
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
println(portName);
myPort = new Serial(this, portName, 9600);
}
void draw()
{
byte[] inBuffer = new byte[2];
while (myPort.available()> 0) {
inBuffer = myPort.readBytes();
myPort.readBytes(inBuffer);
if (inBuffer != null) {
String myString = new String(inBuffer);
println(myString);
}
}
delay (500);
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.read(); // read it and store it in val
}
if (currentState<displayImage.size()) {
image(displayImage.get(currentState), 0, 0);
}
int dist = 20;
println(val);
//--------------------------------- Left Sensor Triggered ---------------------------------------
if (!leftSensorTriggered && val < dist) {
// not previously triggered - but triggered this time
// i.e. *just* triggered
leftSensorTriggered = true ;
currentState= currentState +1;
// need to check if we're at the end:
if (currentState>= displayImage.size()) {
currentState=0;
}
}
if (leftSensorTriggered && val < dist) {
// was triggered, but now isn't
leftSensorTriggered=false;
}
//---------------------------------Right Sensor Triggered ---------------------------------------
if (!rightSensorTriggered && val < dist) {
// not previously triggered - but triggered this time
// i.e. *just* triggered
rightSensorTriggered = true ;
currentState= currentState -1;
// need to check if we're at the end:
if (currentState>= displayImage.size()) {
currentState=-1;
}
}
if (rightSensorTriggered && val < dist) {
// // was triggered, but now isn't
leftSensorTriggered=false;
}
}
PS only the sensor on TRIGGER_PIN2 = 7 ; ECHO_PIN2 = 8; is working properly and the other one doesnt work.
Thank you again and i am sorry for the long essay :)
1