How can i change background color in a while loop - processing -


i'm new processing , trying make simple program have arduino produces seriel input (according analogue read value). idea processing window open block color shown 30 seconds. in time readings arduino summed , averaged - creating average color.

after 30 seconds colour change , new average (for next color) start being calculated. code have started write (for focusing on 1 30 second period of green).

i realise there problems reading/summing , averaging (i havent researched these yet i'll put 1 side) - main question why isn't background green? when run program expect background green 30 seconds - happens is white 30 seconds changes green. can't figure out why! help!

import processing.serial.*; serial myport;     float gsraverage; float greenaverage; int gsrvalue; int greentotal = 0; int greencount = 1;  int timesincestart = 0; int timeatstart; int count=0; color green = color(118,236,0);   void setup () {   size(900, 450);   // list available serial ports   //println(serial.list());    myport = new serial(this, serial.list()[0], 9600);  }   void draw () {    while (timesincestart < 30000) {     background(green);     greentotal = greentotal + gsrvalue;     greencount = greencount + 1;     delay(500);     timesincestart = millis()-timeatstart;     //println(timesincestart); de bugging   }   greenaverage = greentotal/greencount;  //println(greenaverage); de bugging }  void serialevent (serial myport) {   int inbyte=myport.read();   //0-255   gsrvalue=inbyte; } 

what timers, use if statements , use millis() or updated variable 'm' right inside condition:

int timesincestart; int m;  void setup(){   timesincestart = millis(); // initialize here happens once }  void draw(){   m = millis(); // update variable    if(timesincestart + 30000 < m){     greenaverage = greentotal/greencount; // or whatever outside while loop     timesincestart = millis();   }    //anything went inside while loop can go here, or above if  } 

this makes around every 30 seconds, background change once, , re-update timesincestart variable in there too. way, update when want update , not update , break code.

i tend not use while loops in processing cause headaches. hope example helps.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -