Class Home

Lab reports

Work

Class details

Physcomp Home
Wiki help
Edit sidebar

Very Basic Serial

This code is a starting point, change it, fix it, improve it, build from it.

The Arduino sends the data out constantly (not polite conversation) with the value it reads from 1 analog sensor.

Processing uses the values from the analog sensor to determine the diameter of a circle.

Arduino Code:

int analogPin = 0;  //data pin for the analog sensor
int analogValue = 0;// outgoing ADC value

void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
 pinMode (analogPin, INPUT);
}

void loop(){

 // read analog input, divide by 4 to make the range 0-255:
 analogValue = analogRead(analogPin);
 analogValue = analogValue/4; 
 Serial.print(analogValue, BYTE); //Arduino will constantly send the value out
 delay(10);  // pause for 10 milliseconds:
//}
}

Processing Code Made some changes on October 7th

import processing.serial.*;
//Needed to be able to use the special serial communication functions

//////VARIABLES////////////////////////////////////////////////////////////////////////////////////////////////  
int xpos; //x position of the ellipse
int ypos;
int ediam = 10; // diameter of the ellipse

//Variables for serial input
Serial bananaPort;  // Name your serial port like you name any variable
int serialIn;    // Where we'll put what we receive
boolean firstContact = false;        // Whether we've heard from the microcontroller

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void setup()
{
  size(600, 600);
  background(0);
  smooth();

  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  bananaPort = new Serial(this, Serial.list()[2], 9600);

}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
void draw() 
{  

  background(0);

  // If no serial data has beeen received, send again until we get some.
  // (in case you tend to start Processing before you start your 
  // external device): 
  if (firstContact == false) {
    println("nothing");
    delay(300);
  }

  xpos = (width/2);
  ypos = (height/2);

  variableEllipse(xpos, ypos, ediam);

}


//////////FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////// 
void variableEllipse(int x, int y, int ed) {
  ellipseMode(CENTER);
  noStroke();
  fill (150, 15, 0);
  ellipse(x, y, ed , ed);
}

//////////SERIAL FUNCTION//////////////////////////////////////////////////////////////////////////////////////////// 
void serialEvent(Serial bananaPort) {

  //Identify the first time the data comes through
  if (firstContact == false) {
    firstContact = true;
  }

  // Put the byte that comes through inside our processing variable:
  serialIn = bananaPort.read();
  ediam = serialIn/2; //make the diameter of the elipse proportional to the value coming out of the sensor

  // print the values (for debugging purposes only):
  println(ediam);

}



  Edit | View | History | Print | Recent Changes | Search Page last modified on October 07, 2007, at 06:38 PM