Arduino&Processing_Page - singerman/TypeEquals GitHub Wiki
Arduino Code Blocks
//TypeEquals Arduino code
*long UVsensor = analogRead(0); //read first analog (UV sensor)
*long Photocell = analogRead(1); //read second analog (Photocell)
*long Airtemp = analogRead(2); //read third analog (Air Temperature)
void setup() {
Serial.begin(9600); // initialize the serial communication:
}
void loop() {
UVsensor = analogRead(0); //read first analog (UV sensor)
Photocell = analogRead(1); //read second analog (Photocell)
Airtemp = analogRead(2); //read third analog (Air Temperature)
Serial.print(analogRead(UVsensor), DEC); //first analog (UV sensor)
Serial.print(",");
Serial.print(analogRead(Photocell), DEC); //second analog (Photocell)
Serial.print(",");
Serial.print(analogRead(Airtemp), DEC); //third analog (Air Temperature)
Serial.println();
delay(1000);
}
Processing Code Blocks
// https://www.processing.org/tutorials/data/ //
// http://meettechniek.info/embedded/arduino-analog.html //
// http://wiring.org.co/learning/basics/sendingmultipledata.html //
import processing.serial.*;
Serial myPort;
numValues = 4; // number of input values or sensors
float[] values = new float[numValues];
int[] min = new int[numValues];
int[] max = new int[numValues];
color[] valColor = new color[numValues];
float partH; // partial screen height
int xPos = 0; // horizontal position of the graph
boolean clearScreen = true; // flagged when graph has filled screen
void setup() {
size(800, 600);
partH = height / numValues;
// List all the available serial ports:
printArray(Serial.list());
// First port [0] in serial list is usually Arduino, but *check every time*:
String portName = Serial.list()[2];
myPort = new Serial(this, portName, 9600);
// don't generate a serialEvent() until you get a newline character:
myPort.bufferUntil('\n');
textSize(10);
background(0);
noStroke();
values[0] = 0;
min[0] = 0;
max[0] = 1023; // full range example, e.g. any analogRead
valColor[0] = color(255, 0, 0); // red
values[1] = 0;
min[1] = 0;
max[1] = 700; // partial range example, e.g. IR distance sensor
valColor[1] = color(0, 255, 0); // green
values[2] = 0;
min[2] = 0;
max[2] = 1; // digital input example, e.g. a button
valColor[2] = color(0, 0, 255); // blue
values[3] = 0;
min[3] = 0;
max[3] = 400; // custom range example
valColor[3] = color(255, 0, 255); // purple
}
void draw() {
if (clearScreen) {
background(0);
clearScreen = false; // reset flag
}
for (int i=0; i<numValues; i++) {
// map to the range of partial screen height:
float mappedVal = map(values[i], min[i], max[i], 0, partH);
// draw lines:
stroke(valColor[i]);
line(xPos, partH*(i+1), xPos, partH*(i+1) - mappedVal);
// draw dividing line:
stroke(255);
line(0, partH*(i+1), width, partH*(i+1));
// display values on screen:
fill(50);
noStroke();
rect(0, partH*i+1, 70, 12);
fill(255);
text(round(values[i]), 2, partH*i+10);
fill(125);
text(max[i], 40, partH*i+10);
//print(i + ": " + values[i] + "\t"); // <- uncomment this to debug values in array
//println("\t"+mappedVal); // <- uncomment this to debug mapped values
}
//println(); // <- uncomment this to read debugged values easier
// increment the graph's horizontal position:
xPos++;
// if at the edge of the screen, go back to the beginning:
if (xPos > width) {
xPos = 0;
clearScreen = true;
}
}
void serialEvent(Serial myPort) {
try {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
//println("raw: \t" + inString); // <- uncomment this to debug serial input from Arduino
if (inString != null) {
inString = trim(inString);
values = float(splitTokens(inString, ", \t")); // delimiter can be comma space or tab
if (values.length >= numValues) {
/* you can increment xPos here instead of in draw():
xPos++;
if (xPos > width) {
xPos = 0;
clearScreen = true;
}
*/
}
}
}
catch(RuntimeException e) {
// only if there is an error:
e.printStackTrace();
}
}