analog input to serial
const int SENSOR = A0;
const int LED = 13;
int val = 0;
int max = 0;
void setup() {
Serial.begin(9600);
pinMode(SENSOR, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
val = analogRead(SENSOR);
if (val > max) {
max = val;
}
String space = " ";
Serial.println(val);
if (val > 1) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
delay(10);
}
serial input to graph
import processing.serial.*;
Serial myPort;
int xPos = 1;
float max = 0;
float cur = 0;
void setup() {
size(720, 240);
println(Serial.list());
myPort = new Serial(this, Serial.list()[2], 9600);
myPort.bufferUntil('\n');
background(58,179,254);
}
void draw() {
}
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
float inByte = float(inString);
inByte = map(inByte, 0, 50, 0, height);
stroke(13,0,192);
line(xPos, height, xPos, height - inByte);
if (xPos >= width) {
xPos = 0;
max = 0;
background(58,179,254);
} else {
xPos++;
}
if (inByte > max) {
stroke(58,179,254);
line(0, height - max, width, height - max);
max = inByte;
cur = height - inByte;
stroke(13,0,192);
line(0, cur, width, cur);
}
}
}