We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have the arduino and ultrasonic HC-SR04. I want to help please us. The arduino want to measure the distance and what gives the serial. Communicates with the processing and when the distance is greater than 150cm displays an image, if less playing video. My problem is that I run the processing but the video plays either away or close. Please help me. I beginner!!
Arduino is from a library NewPing // --------------------------------------------------------------------------- // Example NewPing library sketch that does a ping about 20 times per second. // ---------------------------------------------------------------------------
#include <NewPing.h>
#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.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
Processing
// Video
import processing.video.*;
Movie movie;
// Image
PImage img; // Declare variable "a" of type PImage
// Serial
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
size(640, 360);
background(0);
printArray(Serial.list());
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 115200);
// Load and play the video in a loop
movie = new Movie(this, "transit.mov");
movie.loop();
img = loadImage("moonwalk.jpg"); // Load the image into the program
}
void movieEvent(Movie m) {
m.read();
}
void draw() {
if (movie.available() == true) {
movie.read();
}
image(movie, 0, 0, width, height);
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
if (val>130) {
image(img, 0, 0);
} else {
image(movie, 0, 0, width, height);
}
println(val);
}
Answers
Can you help me??? Please.
Let me try. Try to delete all of this and tell what happens:
I did it and it doesn't work. Also, I see only video. When I put front my hamd ultrasonic at a distance 150cm and it doesn't show me the picture.
arduino serial.println send a ascii text,,processing myport.read read a byte, try arduino Serial.write or processing myport.readString
println(val);
what is the value of val? is it as expected?Camperos I change but it doesn't work. Ater my project helped a friend of mine but he does not know very well. The code Arduino he made is this. #include <NewPing.h>
But I think that it doesn't work 100%. Τhe former which was posted on the library NewPing in Arduino. Can you help me plz?
Why do you send descriptive texts out like "Ping:" or "cm"?
Why would Processing's receiving end need that for?
Also you send out
uS / US_ROUNDTRIP_CM
2 times.1st as String and 2nd as
int
?If you intend to read 1 val only from Processing, just send out 1 val!
Thank you very much my friends and thank you my friend GoToLoop. It works my project. :) :D