We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Drawing using data from processing sketch
Page Index Toggle Pages: 1
Drawing using data from processing sketch (Read 524 times)
Drawing using data from processing sketch
Apr 13th, 2009, 9:32pm
 
I will start off by saying I am completely new to Processing so sorry if I am asking a simple question.  I recently found this code in an Arduino book and it works great for what I need.  Basically I want to search Twitter for the use of certain key words.  Once these key words are found I need Processing to draw squares equal to the number of results found for each word (i.e. if a word is found 10 times draw 10 squares).  I would really like these squares to be drawn completely randomly .  Is this at all possible? Here is the code I am using:

P.S.  Because I am new I am not allowed to add code that has a web link in it which is why there is no web address in String feed ="";

// Example 08A: Arduino networked lamp
// parts of the code are inspired
// by a blog post by Tod E. Kurt (todbot.com)

import processing.serial.*;

String feed = "";

int interval = 0;  
int lastTime;      

int move    = 0;
int movement   = 0;
int location = 0;
int place = 0;
int travel = 0;
int walk = 0;
int run = 0;
int xxyyzz = 0;

int light = 0;  

Serial port;
color c;
String cs;

String buffer = "";

PFont font;

void setup() {
 size(640,640);
 frameRate(10);    

 font = loadFont("HelveticaNeue-Bold-32.vlw");  
 fill(255);  
 textFont(font, 32);

 //println(Serial.list());
 String arduinoPort = Serial.list()[0];
 port = new Serial(this, arduinoPort, 9600);

 lastTime = 0;
 fetchData();
}

void draw() {
 background( 000 );
 int n = (interval - ((millis()-lastTime)/1000));

 // Build a colour based on the 3 values
 c = color(move, movement, location);
 cs = "#" + hex(c,6); // Prepare a string to be sent to Arduino

 //text("Arduino Networked Lamp", 10,40);
 //text("Reading feed:", 10, 70);
 text(feed, 10, 40);

 text("Next update in "+ n + " seconds",2,635);
 text("move" ,10,200);
 text(" " + move, 180, 200);
 rect(260,172, move, 40);

 text("movement ",10,240);
 text(" " + movement, 180, 240);
 rect(260,212, movement, 40);

 text("location ",10,280);
 text(" " + location, 180, 280);
 rect(260,252, location, 40);
 
 text("place ",10,320);
 text(" " + place, 180, 320);
 rect(260,292, place, 40);

 text("travel ",10,360);
 text(" " + travel, 180, 360);
 rect(260,332, travel, 40);
 
 text("walk ",10,400);
 text(" " + walk, 180, 400);
 rect(260,372, walk, 40);
 
 text("run ",10,440);
 text(" " + run, 180, 440);
 rect(260,412, run, 40);
 
 text("xxyyzz ",10,480);
 text(" " + xxyyzz, 180, 480);
 rect(260,452, xxyyzz, 40);
 
 // write the colour string to the screen
 //text("sending", 10, 370);
 //text(cs, 200,370);

 //text("light level", 10, 410);
 //rect(200, 382,light/10.23,28); // this turns 1023 into 100

 if (n <= 0) {
   fetchData();
   lastTime = millis();
 }

 port.write(cs);

 if (port.available() > 0) {
   int inByte = port.read();
   if (inByte != 10) {
     buffer = buffer + char(inByte);
   }
   else {
     if (buffer.length() > 1) { // make sure there is enough data
       buffer = buffer.substring(0,buffer.length() -1);
       light = int(buffer);
       buffer = "";
       port.clear();
     }
   }
 }

}

void fetchData() {
 String data;
 String chunk;

 move    = 0;
 movement   = 0;
 location = 0;
 place = 0;
 travel = 0;
 walk = 0;
 run = 0;
 xxyyzz = 0;
 
 try {
   URL url = new URL(feed);  
   URLConnection conn = url.openConnection();
   conn.connect();
   BufferedReader in = new
     BufferedReader(new InputStreamReader(conn.getInputStream()));

   // read each line from the feed
   while ((data = in.readLine()) != null) {

     StringTokenizer st =
       new StringTokenizer(data,"\"<>,.()[] ");// break it down
     while (st.hasMoreTokens()) {
       // each chunk of data is made lowercase
       chunk= st.nextToken().toLowerCase() ;

       if (chunk.indexOf("move") >= 0 )
         move++;    
       if (chunk.indexOf("movement") >= 0)
         movement++;  
       if (chunk.indexOf("location") >= 0)
         location++;
       if (chunk.indexOf("place") >= 0)
         place++;
       if (chunk.indexOf("travel") >= 0)
         travel++;
       if (chunk.indexOf("walk") >= 0)
         walk++;
       if (chunk.indexOf("run") >= 0)
         run++;    
       if (chunk.indexOf("xxyyzz") >= 0)
         xxyyzz++;  
     }
   }
 }
 catch (Exception ex) {
   ex.printStackTrace();
   System.out.println("ERROR: "+ex.getMessage());
 }
}
Page Index Toggle Pages: 1