Lina Maria Giraldo's Blog

Multimedia Artist and ITP Student

Serial communication Week 1

without comments

Code 1:

int analogPin = 0;

int analogValue = 0;

 

void setup()

{

  // start serial port at 9600 bps:

  Serial.begin(9600);

}

 

void loop()

{

  // read analog input, divide by 4 to make the range 0-255:

  analogValue = analogRead(analogPin); 

  analogValue = analogValue / 4;

  Serial.print(analogValue, BYTE);

  // pause for 10 milliseconds:

  delay(10);                 

}Code 2:

import processing.serial.*;

Serial myPort;        // The serial port

int graphXPos = 1;    // the horizontal position of the graph:  

 

void setup () {

  size(400, 300);        // window size

 

  // List all the available serial ports

  println(Serial.list());

 

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

    background(113,86,86);

}

 

void draw () {

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

}

 

void serialEvent (Serial myPort) {

  // get the byte:

  int inByte = myPort.read(); 

  // print it:

  println(inByte);

  // set the drawing color. Pick a pretty color:

  stroke(123,128,158);

  // draw the line:

  line(graphXPos, height, graphXPos, height – inByte);

  // at the edge of the screen, go back to the beginning:

  if (graphXPos >= width) {

    graphXPos = 0;

    // clear the screen:

    background(113,86,86);

  } 

  else {

    // increment the horizontal position for the next reading:

    graphXPos++;

  }

}

Written by admin

February 24th, 2009 at 5:40 pm

Leave a Reply

You must be logged in to post a comment.