Hello everyone!
I am an artist and I am trying to make an interactive artwork using Arduino and Processing. I am really new at all this,.I know very little about programming but I have to complete this artwork as it is the final task of an intensive program I took part in, which introduced me to interactive arts using Arduino.
So, for this artwork, I am using an ultrasonic distance sensor to capture the existence of a person in front of it, an Arduino Uno, and of course Processing. Firstly, I want to play a video that only has sound with Processing. When the sensor detects a person at a maximum distance of 50cm, then the first video stops, and a second video starts playing without looping. Right after the second video ends, then the first video starts looping again until the sensor detects another person standing in front.
So far I have found a code for Arduino and written a code in Processing. The problem is that the first video only plays a few seconds and then the second video starts playing without interacting with the sensor. I have checked the sensor and it functions properly. I have also tested the communication between Arduino and Processing with a premade code and it also works. But I still haven’t found what I do wrong. I would really appreciate any given help!
Thank you in advance!
This is the code I run with Arduino, which I found online:
#include <NewPing.h>
#define TRIGGER_PIN 10 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 50 // 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(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(100); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
//Serial.print("Ping: ");
Serial.write(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
//Serial.println(“cm”);
}
And this is a code that I did myself at processing:
import processing.serial.; //importing serial lib
import processing.video.;
Serial arduino;
int serialIn;
int val = 49; // Data received from the serial port
int THRESHOLD = 50;
Movie sound;
Movie video;
void setup() {
size(500,500);
printArray(Serial.list());
arduino = new Serial(this,Serial.list()[1],9600);
frameRate(25);
sound= new Movie(this,“keys.mp4”);
video= new Movie(this,“cross.mov”);
sound.loop();
}
void movieEvent(Movie m) {
m.read();
}
void draw() {
if(arduino.available()>0)
{
serialIn=arduino.read();
println(serialIn);
if (val <= THRESHOLD) { //If the sensor detect object at less 10cm
sound.stop();
video.play();
image(video,0,0);
}
}
}