Single Sensor as Byte from Analog In to Processing

/*
  AnalogReadSerial
 Reads an analog input on pin 0, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  Serial.write(sensorValue);
  delay(10);

}

Processing

//this is based on simple read example on processing.
import processing.serial.*; //includes serial library to processing
Serial myPort;  // Create object from Serial class
int val = 0;      // Data received from the serial port
int preVal = 0; //stores the previous sensor data
int xCoor = 0 ;//x coordinate  for drawing line
int preXCoor = 0;//previous x coordinate for drawing line
void setup()
{
  size(900, 900); //size of the sketch
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you’re using.
  //println(Serial.list());//if you want to see possible ports that you can use you can use this line
  String portName = Serial.list()[0];//we are connecting to port “0″
  myPort = new Serial(this, portName, 9600);//this should match with Arduino’s baudrate
}

void serialEvent(Serial _whichPort){
  int val = _whichPort.read();
  if (val != -1){

  // val = myPort.read();         // read it and store it in val
     println(val);
    // background(255);             // Set background to white
    xCoor++; //x position increases one pixel
    println(val);
    line(preXCoor, preVal, xCoor, val); //draws a line graph //line(x1, y1, x2 , y2)
    if ( xCoor > 900) {// if line is out of the screen cleans the screen and starts from zero
      xCoor = 0;
      background(255);
    }
    preXCoor = xCoor; //x coordinate stored, we will use it as previous coordinate in the next frame.
    preVal = val; //val stored, we will use it as previous val in the next frame.
  }
}
void draw()
{
  
}

Comments are closed.