Problem to change the color according to sensor values

Hi I am trying to change the colour according to the values of potentiometer. I have the problem that the colour does not change in accordance to the parameters that I settled. Attached is the code :

import processing.serial.*;
Serial myPort; 
int x_1=1200; 
int y_1= 1024;

// values 

int  count=1; 
float [] sen1= new float [999999]; 
float [] sen2= new float [999999] ; 


float s1,s2;



// String variables 

String a1,a2; 

//colors
color blue= #2500FF; 
color green=#00FF3D; 
color yellow=#F6FF00;
color orange=#FFAE58;
color red=#FF0303;


color x1,x2;


// 

int  value; 
int xPos=30;

PImage img; 

//font variables 
PFont myFont,title,font; 

void setup(){
 background(0); 
 size (1200, displayHeight); 
// size(1300,750);
 println(Serial.list()); 
 myPort=new Serial(this,Serial.list()[1],9600); 
myPort.bufferUntil('\n'); 
}

void draw(){

beginShape(); 
fill(x1); 
smooth(); 
vertex(107,162); 
vertex(194,102); 
vertex(287,144); 
vertex(292,258); 
vertex(203,315); 
vertex(114,264); 
endShape();

beginShape(); 
fill(x2); 
smooth(); 
vertex(288,144); 
vertex(361,96); 
vertex(434,151); 
vertex(441,250); 
vertex(371,305); 
vertex(293,259); 
endShape(); 




}
void serialEvent ( Serial myPort){

String inString= new String(myPort.readBytesUntil('\n')); 
if(inString !=null){
String  q[]= splitTokens(inString,","); 
if (q.length > 0){

a1= trim(q[0]);
a2= trim(q[1]); 

s1=float(a1); 
s2=float(a2);

sen1[count]=s1;
sen2[count]=s2; 

smooth();
fill(x1); 
rect(1100,400,20,-plot_sen1[count]); 

fill(x2);
rect(800,400,20,-plot_sen2[count]); 

if(sen1[count] <= 0.0 && sen1[count] <= 204.0){
          x1=blue; 
                     }

  if(sen1[count] > 204.0 && sen1[count] <=408.0){
         x1=green; 
                     }     

  if(sen1[count] >408.0 && sen1[count] <=612.0) {
         x1=yellow; 
                     }

  if(sen1[count] >612.0 && sen1[count] <=816.0) {
         x1=orange; 
                     }

  if(sen1[count] > 816.0 ) {
         x1=red;     
                     }


   if(sen2[count] <= 0.0 && sen2[count] <= 204.0){
          x1=blue; 
                     }

  if(sen2[count] > 204.0 && sen2[count] <=408.0){
         x1=green; 
                     }     

  if(sen2[count] >408.0 && sen2[count] <=612.0) {
         x1=yellow; 
                     }

  if(sen2[count] >612.0 && sen2[count] <=816.0) {
         x1=orange; 
                     }

  if(sen2[count] > 816.0 ) {
         x1=red;     
                     }
 }

}

count++;
if (xPos >=x_1) {
  xPos=30; 
 background(0);  
}

else 
{
  xPos++; 
}

}

Answers

  • We should never directly access sketch's canvas outside its "Animation" Thread.
    FYI, serialEvent() is run by Serial's own Thread.

Sign In or Register to comment.