My Sketch Won't forget old Values!

Hi, I have a sketch that draws a line whenever an OSC event happens. It is just like drawing a line from mouse to pmouse, but, because I am using an OSC slider, there is no built in variable to track the slider's position in the previous frame. Basically I want it so that I press a button and a line is drawn for one spot to another. Then, when I release the button, the final value is forgotten, so that when I press the button a new lines starts.

Right now, in my sketch, this almost works, but the x value is remembered somehow. That means that when I release button and move slider then press again, the line starts in a new Y coordinate, but not a new X coordinate?! I am using array lists so I dont think it makes sense to copy the whole code, but here is the relevant part. (I am waiting on an OSC event called IR, it sends to values continuously while pressed, and then a bang message when released). Thanks!

void oscEvent(OscMessage theOscMessage) {

  if(theOscMessage.checkAddrPattern("/bang")==true){
   firstTimer=true;
  }

  if(theOscMessage.checkAddrPattern("/ir")==true) { 
    if(theOscMessage.checkTypetag("ff")) {
      float firstValue = theOscMessage.get(0).floatValue();
      float secondValue = theOscMessage.get(1).floatValue();  

          if(!firstTimer){
             lines.add(new Line(map(firstValue,0,1,0,width), map(secondValue,0,1,0,height), cursorX, cursorY, 26, color(0, 0, 255)));     
          }else {firstTimer=false;}
          cursorX= map(firstValue,0,1,0,width);
          cursorY=map(secondValue,0,1,0,height);
        }
       return;
    }
  }

Answers

  • Answer ✓

    Funny, people more often has issue to keep track of old values, rather than to forget them... :-)

    I fear the code snippet isn't enough to diagnose your problem. At least, I don't see an obvious error, and usage of x and y seems symmetrical. The logic seems OK. (Just you don't need the == true parts of the conditions...)

  • edited November 2013

    Thank you for your advice PhiLho, and suggestions on code simplification- it was a strange problem. I somehow fixed it, but I am not sure exactly why my solution worked (for pedagogical purposes it would be nice to know). What I did was add an if condition if(zee>1) before the (!FirstTimer) condition. zee is an integer that is reset on the close (/bang) OSC message. It doesn't seem like anything fundamentally changed, but now it works. Any idea why?

    void oscEvent(OscMessage theOscMessage) {
    
    
    
    
      if(theOscMessage.checkAddrPattern("/bang")){
       firstTimer=true;
       zee=0;
      }
    
      if(theOscMessage.checkAddrPattern("/ir")) { 
    
    
    
    
          //Get cursor values from the message  
            newX = map(theOscMessage.get(0).floatValue(), 0, 1, 0, width);
            newY = map(theOscMessage.get(1).floatValue(), 0, 1, 0, height); 
            zee++;
    
          //New CONDITIONAL HERE
          if(zee>1){
               //If it's not the first time through, add the line
              if(!firstTimer){
    
                 lines.add(new Line(newX, newY, cursorX, cursorY, 26, color(0, 0, 255)));  
    
              // Otherwise, just clear the firstTimer flag
              } else {
    
                      firstTimer=false;
    
              }
    
              // In any case, save the new values as the cursor
              cursorX= newX;
              cursorY=newY;
    
          }
        }
      }
    
  • Apparently, you ignore the values of the first /ir message received after /bang. Why it is necessary is dependent on your system... You can println() the values to see what is going on, perhaps.

Sign In or Register to comment.