We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I'm fairly new to programming, I've done some MatLab and a little bit of java. I am starting to use Processing and I was reading some tutorials on how to draw shapes. I wanted to draw 4 boxes and have them change colors based on some values extracted from my Arduino Mini (atMega328P)
I have the Arduino set to get values from 4 analog sources, I want to transfer those values to Processing and have the values make boxes change colors.
This is what I have so far for my processing part:
PShape rectangle1;
PShape rectangle2;
PShape rectangle3;
PShape rectangle4;
void setup(){
size(640,480,P2D);
smooth();
rectangle1 = createShape(RECT, 50, 50,100,50);
rectangle2 = createShape(RECT, 175,50,100,50);
rectangle3 = createShape(RECT, 300,50,100,50);
rectangle4 = createShape(RECT, 425,50,100,50);
}
void draw()
// DRAW THE BOXES THAT REPRESENT EACH CHANNEL AND THEIR RESPONSE TO THE CHANNEL VALUE
if(channel[0]>799){
rectangle1.setFill(color(0,255,0));}
else{
rectangle1.setFill(color(255,0,0))
}
shape(rectangle1);
shape(rectangle2);
if(channel[1]>799){
rectangle2.setFill(color(0,255,0));}
else{
rectangle2.setFill(color(255,0,0))
}
shape(rectangle3);
if(channel[2]>799){
rectangle3.setFill(color(0,255,0));}
else{
rectangle3.setFill(color(255,0,0))
}
shape(rectangle4);
if(channel[3]>799){
rectangle4.setFill(color(0,255,0));}
else{
rectangle4.setFill(color(255,0,0))
}
What I am not sure is how to read the values for the channel array that are going to be transfered tru COM4 from the Arduino.
I am also not sure if that if/else structure would work, I assume it will (I am not sure if it will dynamically work though, like I want the rectangle to change back and forth between red and green, depending on the value obtained from the sensor)
For my arduino code I have the following:
// Declare the pin assigned to each sensor
const int sensorpin_1 = A0;
const int sensorpin_2 = A1;
const int sensorpin_3 = A2;
const int sensorpin_4 = A3;
const int sensorpin_5 = A6;
int i;
void channelread()
{
// Declare variables that will store values read from each sensor
float channels[4];
//read input pins with analog values
channels[0] = analogRead(sensorpin_1);
channels[1] = analogRead(sensorpin_2);
channels[2] = analogRead(sensorpin_3);
channels[3]= analogRead(sensorpin_4);
channels[4] = analogRead(sensorpin_5);
//print value on the serial monitor screen
Serial.print("C1= "); Serial.print(channels[0]);
Serial.print(" C2= "); Serial.print(channels[1]);
Serial.print(" C3= "); Serial.print(channels[2]);
Serial.print(" C4= "); Serial.print(channels[3]);
Serial.print(" C5= "); Serial.println(channels[4]);
delay(200)
}
}
I am not even sure if I should add that text ("C1 =" , C2 = etc. . .) since those are there to make it easier for a human to read if you are observing the serial monitor.