Hi,
I am just starting to explore posibilities to make something work in processing via arduino (sensors). On school exhibition I want to make a picture display that shows one image at the time on the wall (with a projector), depending how close or far person is located from distance sensor.
This is a sketch of what I am thinking about:
To see if it really works, I explored some tutorials that explains how to connect Arduino input with Processing. Currently, I have these codes in both programs:
for Arduino:
/*
This code reads the Analog Voltage output from the
LV-MaxSonar sensors
Please note that we do not recommend using averaging with our sensors.
Mode and Median filters are recommended.
*/
const int anPin1 = 0;
long distance1;
void setup() {
Serial.begin(9600); // sets the serial port to 9600
}
void read_sensors(){
/*
Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
*/
distance1 = analogRead(anPin1)/2;
}
void print_all(){
/*
Serial.print("S1");
Serial.print("inches");
*/
Serial.print(" ");
Serial.print(distance1);
Serial.println();
}
void loop() {
read_sensors();
print_all();
delay(50);
}
And for Processing:
import processing.serial.*;
Serial myPort;
String data="" ;
PFont myFont;
int distance;
void setup(){
size(1366,900); // size of processing window
//background(0);// setting background color to black
myPort = new Serial(this, "/dev/cu.usbmodemfd111", 9600);
myPort.bufferUntil('\n');
}
void draw(){
background(0);
textAlign(CENTER);
fill(255);
text(data,820,400);
textSize(100);
fill(#4B5DCE);
text(" Distance : cm",450,400);
noFill();
stroke(#4B5DCE);
}
void serialEvent(Serial myPort){
data=myPort.readStringUntil('\n');
}
And this distance reading works perfect!
Unfortunately didn’t found any specific example for what i am looking for.
So the question is, how can I get, for example, 3 pictures working this way:
if(distance>60){
image(photo1, 0, 0); //display first photograpgy
}
else if (40<distance<60) {
image(photo2, 0, 0);
}
else if (distance<40) {
image(photo3, 0, 0);
}
Do I have to write something like that in draw ()?
And how can I get income distance value as number from which depends displayed image?
And should I upload any specific library for this?
Would be great if someone could suggest any examples or codes for this.
Hoping for advise,
komats