I added the buffer but still getting the same flickering issue. Also the video is not supposed to start until the value within the sensor range is met but the camera is always on.
here is my processing code with the buffer and another tweak.
int imgCount= 12;
PImage [] imgs = new PImage[imgCount];
float imgW;
float offset = 0;
float easing = 0.05; //using sensor value distance to change its position. this program overlays one image over another by modifying the alpha value of the image with the tint() function.
int currentImage=0;
int lastShown=0;
int interval=3000; // each slide shows for this many millis
int blendedInterval=9000;
int lastRangeReading = 0;
int videoResetInterval=1000; // if we haven't gotten any close range values for a second, reset video transparency
//keep track of loaded images (true or false)
boolean[] loadStates = new boolean [imgCount];
// for loading images
float loaderX, loaderY, theta;
import processing.video.*;
import processing.serial.*;
Capture video;
int count;
int writeRow;
int maxRows;
int topRow;
int buffer[];
boolean showLiveVideo=false;
Serial serialPort;
int rangeVal=255;
PImage videoBuffer;
void setup() {
size (480, 640, P3D);
background(0);
smooth();
imgW= width/imgCount;
// load images asynchronously
for (int i = 0; i < imgCount; i++) {
imgs[i] = requestImage("warhol_img"+nf(i, 4)+".jpg");
}
// Setup VIDEO Capture
String[] cameras = Capture.list();
//video = new Capture(this, 640, 480);
video = new Capture(this, cameras[9]);
video.start();
maxRows = height *2;
buffer = new int[width * maxRows];
writeRow = height - 1;
topRow = 0;
frameRate(10);
background(0);
loadPixels();
// Setup SERIAL PORT
println(Serial.list());
String portName = Serial.list()[0];
serialPort = new Serial(this, portName, 9600);
}
void draw()
{
if (video.available()) {
video.read();
videoBuffer=video;
}
if (videoBuffer !=null) {
image(videoBuffer, 0, 0);
}
// check for new range finder data
while ( serialPort.available () > 0) { // If data is available,
rangeVal = serialPort.read(); // read it and store it in val
lastRangeReading=millis();
//println(rangeVal);
}
if(millis()-lastRangeReading >videoResetInterval) {
lastRangeReading =millis();
rangeVal=255; // make video completely transparent
}
// draw pollock image
background(0);
//tint(255, 255);
//image(img, 0, 0, displayWidth, displayHeight);
for (int i = 0; i < imgs.length; i++) {
// Check if individual images are fully loaded
if ((imgs[i].width != 0) && (imgs[i].width != -1)) {
// As images are loaded set true in boolean array
loadStates[i] = true;
}
}
if (checkLoadStates()) {
// all the images are loaded
if (currentImage<imgCount) {
// we are in the first set of single images
drawNextImage();
if (millis()-lastShown>interval) {
currentImage=currentImage+1;
lastShown=millis();
}
}
else {
// we are drawing the blendef image
drawBlendedImages();
if (millis()-lastShown>blendedInterval) {
// if we've shown the blended image for long enough
// reset the whole system
currentImage=0; // start at first image;
lastShown=millis(); // reset the timer
tint(255);
}
}
}
// draw video image
tint(255, rangeVal); // display at half opacity
image(video, 0, 0, 480, 640);
}
void drawNextImage() {
int y = (height - imgs[0].height) / 2;
image(imgs[currentImage], 0, y, imgs[currentImage].height, imgs[currentImage].height);
}
void drawBlendedImages() {
int y = (height - imgs[0].height) / 2;
for (int i = 0; i < imgs.length; i++) {
//image(imgs[i], width/imgs.length*i, y, imgs[i].height, imgs[i].height);
tint(255, 128);
image(imgs[i], 0, y, imgs[i].height, imgs[i].height);
}
}
// Loading animation
void runLoaderAni() {
// Only run when images are loading
if (!checkLoadStates()) {
ellipse(loaderX, loaderY, 10, 10);
loaderX += 2;
loaderY = height/2 + sin(theta) * (height/8);
theta += PI/22;
// Reposition ellipse if it goes off the screen
if (loaderX > width + 5) {
loaderX = -5;
}
}
}
// Return true when all images are loaded - no false values left in array
boolean checkLoadStates() {
for (int i = 0; i < imgs.length; i++) {
if (loadStates[i] == false) {
return false;
}
}
return true;
}
void captureEvent(Capture c) {
c.read();
}
here is my arduino code
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
//Serial.println(sensorValue);
//Serial.println(map(sensorValue, 0, 545, 0, 255));
Serial.write(map(sensorValue, 20, 225, 0, 255));
// send sensor value as number between 0 - 255
//Serial.write(sensorValue/4);
delay(50); // delay in between reads for stability
}
here is my processing code with the buffer and another tweak.
int imgCount= 12;
PImage [] imgs = new PImage[imgCount];
float imgW;
float offset = 0;
float easing = 0.05; //using sensor value distance to change its position. this program overlays one image over another by modifying the alpha value of the image with the tint() function.
int currentImage=0;
int lastShown=0;
int interval=3000; // each slide shows for this many millis
int blendedInterval=9000;
int lastRangeReading = 0;
int videoResetInterval=1000; // if we haven't gotten any close range values for a second, reset video transparency
//keep track of loaded images (true or false)
boolean[] loadStates = new boolean [imgCount];
// for loading images
float loaderX, loaderY, theta;
import processing.video.*;
import processing.serial.*;
Capture video;
int count;
int writeRow;
int maxRows;
int topRow;
int buffer[];
boolean showLiveVideo=false;
Serial serialPort;
int rangeVal=255;
PImage videoBuffer;
void setup() {
size (480, 640, P3D);
background(0);
smooth();
imgW= width/imgCount;
// load images asynchronously
for (int i = 0; i < imgCount; i++) {
imgs[i] = requestImage("warhol_img"+nf(i, 4)+".jpg");
}
// Setup VIDEO Capture
String[] cameras = Capture.list();
//video = new Capture(this, 640, 480);
video = new Capture(this, cameras[9]);
video.start();
maxRows = height *2;
buffer = new int[width * maxRows];
writeRow = height - 1;
topRow = 0;
frameRate(10);
background(0);
loadPixels();
// Setup SERIAL PORT
println(Serial.list());
String portName = Serial.list()[0];
serialPort = new Serial(this, portName, 9600);
}
void draw()
{
if (video.available()) {
video.read();
videoBuffer=video;
}
if (videoBuffer !=null) {
image(videoBuffer, 0, 0);
}
// check for new range finder data
while ( serialPort.available () > 0) { // If data is available,
rangeVal = serialPort.read(); // read it and store it in val
lastRangeReading=millis();
//println(rangeVal);
}
if(millis()-lastRangeReading >videoResetInterval) {
lastRangeReading =millis();
rangeVal=255; // make video completely transparent
}
// draw pollock image
background(0);
//tint(255, 255);
//image(img, 0, 0, displayWidth, displayHeight);
for (int i = 0; i < imgs.length; i++) {
// Check if individual images are fully loaded
if ((imgs[i].width != 0) && (imgs[i].width != -1)) {
// As images are loaded set true in boolean array
loadStates[i] = true;
}
}
if (checkLoadStates()) {
// all the images are loaded
if (currentImage<imgCount) {
// we are in the first set of single images
drawNextImage();
if (millis()-lastShown>interval) {
currentImage=currentImage+1;
lastShown=millis();
}
}
else {
// we are drawing the blendef image
drawBlendedImages();
if (millis()-lastShown>blendedInterval) {
// if we've shown the blended image for long enough
// reset the whole system
currentImage=0; // start at first image;
lastShown=millis(); // reset the timer
tint(255);
}
}
}
// draw video image
tint(255, rangeVal); // display at half opacity
image(video, 0, 0, 480, 640);
}
void drawNextImage() {
int y = (height - imgs[0].height) / 2;
image(imgs[currentImage], 0, y, imgs[currentImage].height, imgs[currentImage].height);
}
void drawBlendedImages() {
int y = (height - imgs[0].height) / 2;
for (int i = 0; i < imgs.length; i++) {
//image(imgs[i], width/imgs.length*i, y, imgs[i].height, imgs[i].height);
tint(255, 128);
image(imgs[i], 0, y, imgs[i].height, imgs[i].height);
}
}
// Loading animation
void runLoaderAni() {
// Only run when images are loading
if (!checkLoadStates()) {
ellipse(loaderX, loaderY, 10, 10);
loaderX += 2;
loaderY = height/2 + sin(theta) * (height/8);
theta += PI/22;
// Reposition ellipse if it goes off the screen
if (loaderX > width + 5) {
loaderX = -5;
}
}
}
// Return true when all images are loaded - no false values left in array
boolean checkLoadStates() {
for (int i = 0; i < imgs.length; i++) {
if (loadStates[i] == false) {
return false;
}
}
return true;
}
void captureEvent(Capture c) {
c.read();
}
here is my arduino code
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
//Serial.println(sensorValue);
//Serial.println(map(sensorValue, 0, 545, 0, 255));
Serial.write(map(sensorValue, 20, 225, 0, 255));
// send sensor value as number between 0 - 255
//Serial.write(sensorValue/4);
delay(50); // delay in between reads for stability
}