how to execute link() once when the webpage is already loaded

edited January 2014 in Programming Questions

hi i'm trying to use rfid module with processing.

my plan is like when rfid tag is read , then link some url

but the problem is the rfid reader keep reading and keep page loaded.

how to solve this problem ? I would like loading url when rfid is read, not loading url when url already loaded

I think I should use boolean but I have no idea how to use it.

here is my code

    import processing.serial.*;

    Serial myPort;  // Create object from Serial class

    String dummy;

    void setup()
    {
    size(200,200);
    background(255);

    myPort = new Serial(this, Serial.list()[8], 9600); 

    dummy=" ";

    }
    void draw()
    {


      byte[] inBuffer = new byte[7];
      while (myPort.available() > 0) {
        inBuffer = myPort.readBytes();
        myPort.readBytes(inBuffer);
        if (inBuffer != null) {

          String myString = new String(inBuffer);


          println(myString);
          //println(myString.length());
          //dummy=myString.substring(0,2);
          //print(dummy);

         dummy=myString;

         if(dummy.equals("139") ==true) { 
         fill(0);
         rect(0,0,150,150);
          println("received 139");
          link("http://allrecipes.com/search/default.aspx?ms=0&origin=Home+Page&rt=r&qt=i&wt=potato&pqt=i&fo=0"); 

         }

         if(dummy.equals("187") ==true) { 
         fill(150);
         rect(0,0,150,150);
          println("received 187");
          link ("http://allrecipes.com/search/default.aspx?ms=0&origin=Home+Page&rt=r&qt=i&pqt=i&fo=0&wt=potato&w0=carrot&w1=onion"); 

         }

        } 


      } 

Answers

  • Answer ✓

    General advice: in general, you can, and should, replace
    if(dummy.equals("xxx") ==true) { with
    if (dummy.equals("xxx")) {

    Have a global boolean, eg. linkOpened.
    When you call link(), set this variable to true.
    And change the test to:
    if (!linkOpened && dummy.equals("xxx")) {

    Thus, you open a link only if not already done.

    You can reset the variable to false after a while (use millis()).

  • edited January 2014

    yes I know what you mean

    so do I have to declare boolean above ? like this :

    boolean linkOpened = true;

    and then

        if (!linkOpened && dummy.equals("xxx")) {
         link("something"); 
        }
    

    right??
    about using millis() I should look into reference

    thanks

  • linkOpened should be false (the default) initially: you haven't opened the link yet! Set it to true after the call to link().

  • ah okay I see !!

    it's like this :

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    
    String dummy;
    
    boolean linkOpened = false;
    
    void setup()
    {
    size(200,200);
    background(255);
    //String portName = Serial.list()[8]; //change the 0 to a 1 or 2 etc. to match your port
    myPort = new Serial(this, Serial.list()[8], 9600); 
    //val = "";
    dummy=" ";
    }
    void draw()
    {
    
    // Expand array size to the number of bytes you expect
      byte[] inBuffer = new byte[7];
      while (myPort.available() > 0) {
        inBuffer = myPort.readBytes();
        myPort.readBytes(inBuffer);
        if (inBuffer != null) {
    
          String myString = new String(inBuffer);
    
    
          println(myString);
          //println(myString.length());
          //dummy=myString.substring(0,2);
          //print(dummy);
    
         dummy=myString;
    
         if(!linkOpened && dummy.equals("187")) { 
         fill(0);
         rect(0,0,150,150);
          println("received 59");
          link("http://mj3989.tumblr.com"); 
          linkOpened = true;
         }
    
         if(!linkOpened && dummy.equals("139")) { 
         fill(150);
         rect(0,0,150,150);
          println("received 139");
          link ("http://melaniejnewbould.tumblr.com"); 
          linkOpened = true;
         }
    
        } 
    
    
      } 
    
    
    
      }
    
Sign In or Register to comment.