// Send the values of A0, A1 and A3
// to serial every 500ms.
// 2416 bytes.
char data[20];
char buffer[6];
void setup(){
Serial.begin(9600);
}
void loop(){
itoa(analogRead(A0), buffer, 10);
strcat(data, buffer);
strcat(data, ",");
itoa(analogRead(A1), buffer, 10);
strcat(data, buffer);
strcat(data, ",");
itoa(analogRead(A2), buffer, 10);
strcat(data, buffer);
strcat(data, "\n");
Serial.write(data);
memset(data, 0, sizeof(data));
delay(500);
}
// Display the data received from the serial port, sent every
// 500 ms from the Arduino. The values sent are from A0, A1
// and A2, and were sent concatenated together, as a comma
// separated list, followed by a \n char.
import processing.serial.*;
import java.awt.TextArea;
Serial myPort;
TextArea myTextArea;
String input;
void setup(){
try{
myPort = new Serial(this, Serial.list()[0], 9600);
}
catch(Exception e){
System.err.println(e);
e.printStackTrace();
}
myTextArea = new TextArea("", 23, 49, 1);
this.add(myTextArea);
size(500, 380);
}
void draw(){
if(myPort.available() > 0){
input = myPort.readStringUntil('\n');
myTextArea.append(input);
}
}
EDIT
As Juraj mentions in his comments, I could have just used Serial.print() 6 times instead of the char arrays. Here is a second Arduino sketch that uses Serial.print(). Before you use it, be aware that it's compile size is 288 bytes more than the char array method. Imagine that :)
// Send the values of A0, A1 and A3
// to serial every 500ms.
// 2704 bytes.
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print(analogRead(A0));
Serial.print(",");
Serial.print(analogRead(A1));
Serial.print(",");
Serial.print(analogRead(A2));
Serial.print("\n");
delay(500);
}