Hello!
I am working on project where I have 7 different sensor and one servo motor, so 8 different data (one data gives me a analog value of laser sensor, second data is position of servo motor and another 6 data are analog values of photocells). All of this are connected on Arduino. I don't have no problems using serial monitor on Arduino IDE, to show me values of position and sensors.
Anyway, now I would like to visualize this data on Processing.org. Here I have problems where to start. I saw examples how to show data on processing with using one analog sensor on Arduino. Examples for proccesing.org are in different books like:
Example 2A: Light sensor (Wiring/Arduino)
// Code to read an analog value and write it to the serial port
int val;
int inputPin = 0; // Set the input to analog in pin 0
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
val = analogRead(inputPin)/4; // Read analog input pin, put in range 0 to 255
Serial.print(val, BYTE); // Send the value
delay(100); // Wait 100ms for next reading
}
Example 2B: Light sensor (Processing)
// Read data from the serial port and assign it to a variable. Set the fill a
// rectangle on the screen using the value read from a light sensor connected
// to the Wiring or Arduino board
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
size(200, 200);
noStroke();
frameRate(10); // Run 10 frames per second
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, 9600);
}
void draw() {
if (0 < port.available()) { // If data is available to read,
val = port.read(); // read it and store it in val
}
background(204); // Clear background
fill(val); // Set fill color with the value read
rect(50, 50, 100, 100); // Draw square
}
or six analog sensor connected on Arduino (example on this post:http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1262190340, but in this case is using function print().Instead using function print() I used function text() but sensor values ??were recorded one over the another. So in time there you couldn't see nothing. Proccesing code:
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
int sensors[];
PFont font;
void setup() {
// List all the available serial ports
println(Serial.list());
font = loadFont("Calibri-18.vlw");
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
String portName = Serial.list()[0];
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil('\n');
size(600, 600);
}
void draw() {
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
textFont(font, 15);
fill(0);
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
text("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t",100*sensorNum, 200);
}
}
}
One part of my Arduino code is look like:
for(pos = 60; pos < 120; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(150); // waits 15ms for the servo to reach the position
// record the maximum sensor value
if (averageValue > sensorMax) {
sensorMax = averageValue;
polozajMaxGrobo = pos;
}
// record the minimum sensor value
if (averageValue < sensorMin) {
sensorMin = averageValue;
positionMin = pos;
}
Serial.print(pos);
Serial.print(",");
Serial.print(sensorValue00);
Serial.print(",");
Serial.print(sensorValue01);
Serial.print(",");
Serial.print(sensorValue02);
Serial.print(",");
Serial.print(sensorValue03);
Serial.print(",");
Serial.print(sensorValue04);
Serial.print(",");
Serial.print(sensorValue05);
Serial.print(",");
}
I heared for parsing but I don't know how to do it. I'll be very grateful if someone could help me to solve the problem or just give me an advice. I apologize if I forgot to add some information, so please ask me.
Thank you very much!