Oct 052013
Topic: In order to display the measured voltage values graphically, the data need to be transferred to the computer.
The Arduino sketch:
In order to transmit the data as quickly as possible (for having a good time resolution of the measurements),
the Arduino sketch has to be reduced to a minimum. All the evaluations are done on the main computer.
/* Measures and transmits the Voltage to the external computer. */ // the setup routine runs once when you press reset: void setup() { // initialize serial communication at maximum 115200 bits per second: Serial.begin(115200); } // the loop routine runs over and over again forever: void loop() { /* Read the voltage over the photo sensistive resistor */ int sensorValue = analogRead(A0); Serial.print("I: "); Serial.println(sensorValue); }
The Processing sketch on the main computer.
This is a modified sketch of a sketch written by Maik Schmidt.
import processing.serial.*; final int WIDTH = 1000; final int HEIGHT = 600; final int xCenter = WIDTH / 2; final int yCenter = HEIGHT / 2; final int LINE_FEED = 10; Serial arduinoPort; int time = 0; float voltage = 0; void setup() { size(WIDTH, HEIGHT); println(Serial.list()); String arduinoPortName = Serial.list()[2]; // modify this according to your system environment println("Arduino is at : "+arduinoPortName); arduinoPort = new Serial(this, arduinoPortName, 115200); arduinoPort.bufferUntil(LINE_FEED); init_screen(); } void serialEvent(Serial port) { voltage = getSensorData(); if (voltage != 0) { println("Voltage: " + voltage); } } float getSensorData() { if (arduinoPort.available() > 0) { final String arduinoOutput = arduinoPort.readStringUntil(LINE_FEED); println(arduinoOutput); int result = parseArduinoOutput(arduinoOutput); return result * (5.0 / 1023.0); } else { return 0; } } int parseArduinoOutput(final String arduinoOutput) { int number = 0; if (arduinoOutput != null) { final int[] data = int(split(trim(arduinoOutput), ' ')); //print("Data length "); println(data.length); if (data.length == 2) { number = data[1]; // Integer.parseInt(data[1]); //println(number); } } else { number = 0; } return number; } void init_screen() { background(255); stroke(0); strokeWeight(1); line(1,HEIGHT,WIDTH,HEIGHT); line(1,1,1,HEIGHT); strokeWeight(10); } void draw() { int yValue; yValue=round((1-voltage/5.0)*HEIGHT); point(time, yValue); if (++time == WIDTH) { init_screen(); time = 0; } }