For a new course I'm attending I have to create a connection between Processing and Arduino. I want that if a certain switch is ON that a rectangle is drawn in Processing. For this I have 3 switches and one button. The three switches represent a color: Red, Green and Blue. So the user should first pick a color and then press te button to let a rectangle be drawn in that certain color. After that, the user should be able to turn of the switch for that color, turn on the switch of another color and draw a second rectangle by pressing the switch. This is unfortunatly not working for me.
Once one rectangle is drawn, it's not possible to draw another rectangle. I'm quite new to this so I'm sorry if my code is very bad!
Thanks in advance!
My Arduino code:
int switchPinRed = 21;
int switchPinGreen = 22;
int switchPinBlue = 23;
int buttonPinRect = 20;
void setup() {
pinMode(switchPinRed, INPUT);
pinMode(switchPinGreen, INPUT);
pinMode(switchPinBlue, INPUT);
pinMode(buttonPinRect, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(buttonPinRect) == HIGH) {
Serial.print(1);
}
else if (digitalRead(switchPinRed) == HIGH) {
Serial.print(2);
}
else if (digitalRead(switchPinGreen) == HIGH) {
Serial.print(3);
}
else if (digitalRead(switchPinBlue) == HIGH) {
Serial.print(4);
}
}
And my Processing code:
import processing.serial.*;
Serial myPort;
int val;
int[] rects = new int [3];
void setup()
{
size(600, 200);
String portName = Serial.list()[0];
myPort = new Serial(this, "/dev/tty.usbmodem3330431", 9600);
for (int i=0; i < 3; i++) {
rects[i] = 0;
}
}
void draw()
{
if ( myPort.available() > 0) {
val = myPort.read();
}
background(255);
for (int i =0; i < 3; i++) {
if (val == 1) {
if (val == 2) {
fill(255, 0, 0);
} else if (val == 3) {
fill(0, 255, 0);
} else if (val == 4) {
fill(0, 0, 255);
}
rect(random(50), random(50), 100, 100);
}
}
}