I’m working on a project where image transparency is scaled with the viewers’ proximity to an Arduino ultrasonic sensor. I’ve been following a tutorial that scales image size with proximity. Here is my code so far.
Arduino:
#include <NewPing.h>
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 243 // 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(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");
}
Processing:
import processing.serial.*; //importing serial lib
PImage diy; //declaring
Serial arduino;
int serialIn;
int val=0;
void setup()
{
size(800,800,P2D);
//fullScreen(P2D);
printArray(Serial.list());
arduino = new Serial(this,Serial.list()[1],115200);
diy= loadImage("testface1.jpg");
imageMode(CENTER);
diy.resize(500,500);
}
void draw()
{
background(0);
image(diy,width/2,height/2,val,val);
if(arduino.available()>0)
{
serialIn=arduino.read();
println(serialIn);
}
val=int(map(serialIn,0,50,100,500));
}
I know I can change transparency with tint- tint(255, 126)- and changing the second value. I just need to figure out how to substitute the size scaling with tint.
Any help would be greatly appreciated.