We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone,
I'm working on a project where I have to display 2 images on my processing window. I work with Arduino Uno , 2 pushbuttons and a led. On processing, I chose 2 images of the same size (250.425) which separate my window (540.425) into 2 equal parts. I have to choose one of my 2 images, throught 2 pushbuttons which are on my Arduino.
Ideally, when i push the first one, my window will display the image which is on the left, at the window size (540*425). In the same way, when i push the second one, my window will display the image which is on the right (at the window size).
Before, I worked on a project with a mouse and it was easier because there were a special function for it (mouseButton). Does somebody know if there is something special for the use of a pushButton ?
I added a led on my electric circuit to check that it detects the push. No worry for the 2 pushbuttons, it works. Finally, I succeeded in displaying the 2 images on the window but i don't succeed in detecting the impact of the push (apart with the led). I tried something with the function buttonPressed but it doesn't work. Could somebody tell me where I'm mistaken ?
Thank you in advance for your answer!!
This is my code: import processing.serial.*;
Serial myPort; //L'objet qui gère le port série
int ETAT_BP; // variable d'état du bouton poussoir
int ETAT_BP1;
int APPUI;
PImage img;
void setup(){
size(540,425);
img=loadImage("quinze.jpg");
image(img,0,0); // display the image in img at co-ordinate (0,0)
image(img,0,0,250,425);
img=loadImage("ecrannoir.jpg");
image(img,250,0,40,425);
img=loadImage("cinq.jpg");
image(img,290,0,250,425);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0],9600);
myPort.buffer(1);
//size(200,500);
//background(51);
}
void draw(){
}
void buttonPressed(){
if( ETAT_BP == APPUI){
img=loadImage("quinze.jpg");
image(img,0,0,540,425);
println("square 1");
myPort.write(1);
}
if( ETAT_BP1 == APPUI){
img=loadImage("cinq.jpg");
println("square 2");
myPort.write(2);
}
}
Comments
Look at using Processing's Arduino library along with Arduino's Firmata software. See http://playground.arduino.cc/Interfacing/Processing . The other way is to set up serial communication between the two, which is kind of a pain but puts you in touch with what's really going on.
Function buttonPressed() isn't a valid Processing's callback event I'm afraid.
Have you read Processing's reference about keyPressed() callback event?
http://processing.org/reference/keyPressed_.html
Thanks for your advice. I hadn't read the keyPressed callback. So, I adapted my code to this function, it's working but... with a keyboard not with a pushbutton. Do you know how to adapt my code for working with my pushbuttons ?
This is my code updated:
And the results that I get:
(with b pushed):
(with c pushed):
No one ?
There are some excellent examples of Processing<->Arduino serial communication in Processing's Examples, in the File menu. Look in Libraries>Serial. The SimpleRead example looks like just what you need.
(Don't call loadImage inside draw() - it's too slow. Load the images to 3 separate pimages in setup and then just display the correct one in draw())
Thanks for your reply. Indeed, the SimpleRead has helped me. When I work with just one pushbutton, no problem: i succeed in switching on the led (Arduino) and playing with the images (Processing). However, when I work with 2 pushbuttons, I don't succeed in switching on the led (for a push on the first or on the second pushbutton), so the push is not detected.. Does someone have an explanation ?
This is my Arduino code without any problem (1 pusbutton):
This is my Arduino code for 2 pusbuttons, it doesn't work:
It looks like you're sending a
Serial.write(1)to Processing for both switches. Shouldn't it beSerial.write(2)for switchPin2? Then your Processing program can decide what to do with each.I tried to change switchPin(2) to put Serial.write(2) but it doesn't work :-/
or modify "virtualcolormixer" or
try this ( I can not try it now)
processing
Yes, I did a if...else, and it works! Thanks !!