Please Help: Pressure sensor correlating with processing. Code included.
in
Core Library Questions
•
1 year ago
Hello, I need help. Currently, I have a gif animation running in processing. However, I would like to have a white image over the running gif. Then when the pressure sensor is pressed, have the white image fade out to reveal the gif and when the pressure sensor is release have the white image fade back over the gif. Is this even possible? If so, what is my next step? I have included the I have written, and a code I have been using that incorporates the pressure sensor. I have been trying to meld the two, but I am having trouble. Thank you!
MY CODE
import fullscreen.*;
import processing.serial.*;
FullScreen fs;
Serial myPort; // The serial port
int numFrames = 13; // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[numFrames];
int transparency = 0;
int clickTime = 0;
float inByte = 0;
boolean readyToChange = false;
void setup()
{
size(1024, 768);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
fs = new FullScreen(this);
fs.enter();
frameRate(.100);
images[0] = loadImage("Don_Quixote_Quote_0.gif");
images[1] = loadImage("Don_Quixote_1.gif");
images[2] = loadImage("Don_Quixote_2.gif");
images[3] = loadImage("Don_Quixote_3.gif");
images[4] = loadImage("Don_Quixote_Quote_4.gif");
images[5] = loadImage("Don_Quixote_Quote_5 copy.gif");
images[6] = loadImage("Don_Quixote_Quote_6 copy.gif");
images[7] = loadImage("Don_Quixote_Quote_7 copy.gif");
images[8] = loadImage("Don_Quixote_Quote_8 copy.gif");
images[9] = loadImage("Don_Quixote_Quote_9 copy.gif");
images[10] = loadImage("Don_Quixote_Quote_10 copy.gif");
images[11] = loadImage("Don_Quixote_Quote_11 copy.gif");
images[12] = loadImage("Don_Quixote_Quote_12 copy.gif");
// If you don't want to load each image separately
// and you know how many frames you have, you
// can create the filenames as the program runs.
// The nf() command does number formatting, which will
// ensure that the number is (in this case) 4 digits.
//for(int i=0; i<numFrames; i++) {
// String imageName = "PT_anim" + nf(i, 4) + ".gif";
// images[i] = loadImage(imageName);
//}
}
void draw()
{
background(255);
transparency = int(inByte);
frame = (frame+1) % numFrames; // Use % to cycle through frames
image(images[frame], .10, 10);
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, 255);
}
BORROWED SENSOR CODE
// Graphing sketch
// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, followed by a newline, or newline and carriage return
// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// This example code is in the public domain.
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127, 34, 255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
1