Tuesday, October 23, 2012

Some of our previous codes

Here are some of our processing codes that we worked with before the final code:


// Etch-a-Sketch
// by Trevor Shannon
//
// based on Graph 
// by David A. Mellis

import processing.serial.*;

Serial myPort;
String buff = "";
String buff1 = "";
String buff2 = "";
int index = 0;
int NEWLINE = 10;

// Store the last 256 values received so we can graph them.
int[] valuesx = new int[256];
int[] valuesy = new int[256];

void setup()
{
  size(512, 512);
  
   println(Serial.list());

  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
  
  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
  

}

void draw()
{
  background(0);
  stroke(0);
  
  // Graph the stored values by drawing a lines between them.
  for (int i = 0; i < 255; i++){
    stroke(i);
    line(512 - valuesx[i], 512 - valuesy[i], 512-valuesx[i + 1], 512 - valuesy[i + 1]);
  }
  while (myPort.available() > 0)
    serialEvent(myPort.read());
}

void serialEvent(int serial)
{
  if (serial != NEWLINE) {
    // Store all the characters on the line.
    buff += char(serial);
  } 
  else {
    // The end of each line is marked by two characters, a carriage
    // return and a newline.  We're here because we've gotten a newline,
    // but we still need to strip off the carriage return.
    buff = buff.substring(0, buff.length()-1);
    index = buff.indexOf(",");
    buff1 = buff.substring(0, index);
    buff2 = buff.substring(index+1, buff.length());
    
    // Parse the String into an integer.  We divide by 4 because
    // analog inputs go from 0 to 1023 while colors in Processing
    // only go from 0 to 255.
    int x = Integer.parseInt(buff1)/2;
    int y = Integer.parseInt(buff2)/2;

    // Clear the value of "buff"
    buff = "";
    
    // Shift over the existing values to make room for the new one.
    for (int i = 0; i < 255; i++)
    {
      valuesx[i] = valuesx[i + 1];
      valuesy[i] = valuesy[i + 1];
    }
    
    // Add the received value to the array.
    valuesx[255] = x;
    valuesy[255] = y;
  }
}

This is the code that help us create the visual for what we showed during critique. 

void setup(){
  size(600,600);
  colorMode(HSB, 100);
    smooth();
     background (50);
}

float mainx,mainy;
float cx1,cy1,cx2,cy2;
int x2,y2;
int col = 100;
int wght = 1;

void draw() {
 stroke(col);
  
 wght +=int(random(-2,2));
 wght = abs(wght);
 strokeWeight(wght);
  

   if (mousePressed) {
   line(pmouseX, pmouseY, mouseX, mouseY);
   if (col>0) {
     col=0;}
     else {
       col=100;}
   }
   if (wght>16) {wght-=2;}
if (keyPressed == true) {
background(50);
}

}
This was another code that didn't do much of anything once we tried to change it. 

import processing.serial.*;
Serial myPort;        // The serial port
  int value;
  boolean actBuff;
  
  String xBuff="";
  String yBuff="";           // horizontal position of the graph

void setup () {
  size(800, 600);        // window size

  // List all the available serial ports
  println(Serial.list());

  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);

  background(#000000);
}


 // nothing happens in draw.  It all happens in SerialEvent()


       void draw () {
   
}



void serialEvent (Serial myPort) {
  // get the byte:
  int inByte = myPort.read();

  // print it:
  println(inByte);

 float xPos = inByte;
  float yPos = inByte;
  // draw the line in a pretty color:
  stroke(random(0,255),random(0,255),random(0,255));
  line(xPos, yPos , xPos, yPos);

  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    // clear the screen by resetting the background:

  }
  else {
    // increment the horizontal position for the next reading:
    xPos++;
  }
}

This was a code that I was messing with for two weeks before we found the etch a sketch code. 

No comments:

Post a Comment