We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm a student new to coding and am trying to get two arduinos, (connected via serial port) to change the colour of two different ellipses on processing. Both arduinos have a simple push button.
One ellipse changes colour when one arduino button is pressed (this part is fine) But when I start to introduce the second arduino the coloured ellipses just flash quickly between the colours.
Is there a way to connect the two arduinos successfully? My code is posted below.
Thank you, Jade ![](> > )
import processing.serial.*;
import processing.serial.*; Serial port; Serial portss; PImage[] img = new PImage[1]; int val = 0;
void setup () { size (1000, 707);
img [0] = loadImage ("nocolouredblobs.jpg"); background(img [0]);
fill(#459839);
ellipse(931,68,75,75);
port = new Serial(this, Serial.list()[3], 9600); }
void draw() { if (port.available() > 0) { val = port.read(); }
if (val == 2) { fill (#EDB137); ellipse (833, 68, 75, 75); fill (#FFDE9B); ellipse (931,68,75,75); } else { fill (#459839); ellipse (833, 68, 75, 75); fill (#459839); ellipse (931,68,75,75); }
}
Answers
Don’t you need one port per arduino?
I have just added the second port to my code...
Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Kf
Kf
Great formatting tip thanks.
@kfrajer, do i get the serial arduino1 and serial arduino2 form downloading in the library? Im not sure what this means basically.
Notice the above is comparing your code and my code. I am using a label for the port object. From your code, port and portss would do the same job. Just consider using better names relevant to the task as it makes your code easier to read (no difference in this small demo - however always aim for clarity)
Kf
I have just updated the code and unfortunately its still not working?!!
Never mutate the sketch's main canvas when outside the "Animation Thread"! [-X
You need a draw function and you change the sketch inside this function, as GoTo pointed out.
Kf
so I just add void draw like this?
this has an error...unexpected void token.
jgid
Ok, so this is sort of the structure:
Usually you change a global variable in the serial event and then, based on this variable, you dictate what draw should do.
Kf
Hi, unfortunately I am still getting the ellipses flashing between the colours really quickly.
I am not sure what is the update rate for your ellipses or the full concept of your program. However, I need you to check this revise version of your code below. Notice I didn't test the code below as I do not have your setup.
Kf
AMAZING. Thank you so much Kf
perfect advice. :)