Una palabra no dice nada…

Posted by | October 07, 2008 | ITP, Physical Computing | No Comments

… and neither does a picture. Or something like that.

So my first time into processing and… i kinda like it. My only gripe is it’s name. I dare you to go to google and look do a casual search for code using the name “processing.” Go ahead. I’ll wait.

Anyhoo, here’s my etch-a-sketch.


Here’s the processing code:

import processing.serial.*;

Serial myPort;       
int xpos, ypos;

int pingX=0,pingY=0;

/*
  this will be a psuedo matrix to hold
  all generated points. the main index of
  bob will represent x, while the arraylist
  of that index will hold all the y points
  along that x line that are lit up.
 */
ArrayList[] bob;

//the pinging you-are-here circle
subPoint p = new subPoint();

void setup () {
  myPort = new Serial(this, "COM4", 9600);
  myPort.bufferUntil('\n');
  
  size(512, 512);        
  xpos=ypos=0;
  
  bob = new ArrayList[513];
  for(int i=0;i<513;i++)
  {
    bob[i]=new ArrayList();
  }
}

void serialEvent (Serial myPort) {
  String data = myPort.readStringUntil('\n');
  if(data!=null)
  {
    println(data);
    data = trim(data);
    int inputs[] = int(split(data,'-'));
    
    if(inputs.length > 1)
    {
      xpos=inputs[0];
      ypos=512-inputs[1];
      
      if(!bob[xpos].contains(ypos)){ bob[xpos].add(ypos);}
      
      pingX++;
      pingY++;
      if(pingX>45){pingX=pingY=0;}
      
      if(inputs[2]==1){ clear(); }
    }
  }
}

void clear()
{
  for(int i=0;i<513;i++)
  {
    bob[i].clear();
  }
}

void draw () {
  background(255);
  stroke(0,255,0);

  for(int i=0;i<513;i++)
  {
    if(!bob[i].isEmpty())
    {
      for(int j=0;j < bob[i].size();j++)
      {
        String s = bob[i].get(j).toString();
        point(i,int(s));
      }
    } 
  }
  p.update(xpos,ypos);
}

class subPoint {
  
  void update(float x, float y)
  {
    stroke(255,0,0);
    noFill();
    ellipse(x+1,y+1,pingX/3,pingY/3);
  }
}



The arduino code is relatively simple:

int aPin1 = 0;
int aPin2 = 1;
int aVal = 0;
int dPin = 2;

void setup()
{
  Serial.begin(9600);
  pinMode(dPin,INPUT);
}

void loop()
{
  aVal = analogRead(aPin1);
  aVal = map(aVal,0,1024,0,512);
  Serial.print(aVal,DEC);
  Serial.print('-');
  aVal = analogRead(aPin2);
  aVal = map(aVal,0,1024,0,512);
  Serial.print(aVal, DEC);
  Serial.print('-');
  aVal = digitalRead(dPin);
  Serial.println(aVal, DEC);
  // pause for 10 milliseconds:
  delay(10);                 
}

Leave a Reply

Your email address will not be published.