I am still a little bit confused about the proper code to use when you want Arduino to send ASCII vs. binary as well as when I want Processing to read ASCII vs. binary. The Arduino Serial.print library reference has become my best friend. This is a very useful program to keep around to test future analog inputs and understand the mapping of their actions to the analog values they produce.
Arduino Code:
int analogPin = 0;
int analogValue = 0;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read analog input, divide by 4 to make the range 0-255:
analogValue = analogRead(analogPin);
//analogValue = analogValue / 4;
Serial.print(analogValue,BYTE);
// pause for 10 milliseconds:
delay(10);
}
Processing Code:
import processing.serial.*;
Serial myPort; // The serial port
int graphXPos = 1; // The horizontal position of the graph
void setup() {
size(400,300); // Window size
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this,Serial.list()[0],9600); // Pick serial port
background(48,31,65);
}
void draw() {
// Nothing happens in draw. It all happens in SerialEvent()
}
void serialEvent(Serial myPort) {
// Get the byte
int inByte = myPort.read();
// Print it
//println(inByte);
// Set the drawing color
stroke(123,128,158);
// Draw the line
line(graphXPos,height,graphXPos,height-inByte);
// At the edge of the screen, go back to the beginning
if(graphXPos >= width) {
graphXPos = 0;
// Clear the screen
background(48,31,65);
}
else {
// Increment the horizontal position for the next reading
graphXPos++;
}
}
Graphing Analog Sensors
I am still a little bit confused about the proper code to use when you want Arduino to send ASCII vs. binary as well as when I want Processing to read ASCII vs. binary. The Arduino Serial.print library reference has become my best friend. This is a very useful program to keep around to test future analog inputs and understand the mapping of their actions to the analog values they produce.
Arduino Code:
int analogPin = 0;
int analogValue = 0;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read analog input, divide by 4 to make the range 0-255:
analogValue = analogRead(analogPin);
//analogValue = analogValue / 4;
Serial.print(analogValue,BYTE);
// pause for 10 milliseconds:
delay(10);
}
Processing Code:
import processing.serial.*;
Serial myPort; // The serial port
int graphXPos = 1; // The horizontal position of the graph
void setup() {
size(400,300); // Window size
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this,Serial.list()[0],9600); // Pick serial port
background(48,31,65);
}
void draw() {
// Nothing happens in draw. It all happens in SerialEvent()
}
void serialEvent(Serial myPort) {
// Get the byte
int inByte = myPort.read();
// Print it
//println(inByte);
// Set the drawing color
stroke(123,128,158);
// Draw the line
line(graphXPos,height,graphXPos,height-inByte);
// At the edge of the screen, go back to the beginning
if(graphXPos >= width) {
graphXPos = 0;
// Clear the screen
background(48,31,65);
}
else {
// Increment the horizontal position for the next reading
graphXPos++;
}
}