I'm doing a project using a pressure sensor and an arduino to read the results and using processing the graph it. i would like to add numbers on the x and y axis to make the graphs more valid, my codes are below.
Arduino code:
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
int A0Vin = ((sensorValue / 102.3)* readVcc());
Serial.print("A");
Serial.println(A0Vin);
delay(10);
}
Processing code
/**
* Serial Read & Write to LogFile
*
* Read data from Arduino through serial port and appends the data to a text file provided.
* Code by: Naveen Karuthedath, Barry Chris & Aradhan
*/
import processing.serial.*;
Serial sPort;
String sData;
int yCoord;
int yCoord2;
int[] plot1;
int Ypos;
String file1 = "C:\Users\George\Documents\Research project\graphing codes\Processing";
String comPort = "COM4";
int logDelay = 100; //ms delay
void setup()
{
size(640,550);//window size
frameRate(100);
//if serial port is available
try {
sPort = new Serial(this, comPort, 9600);
// don’t read the serial buffer until a new line char
// should use Serial.println("") in arduino code
} finally {}
plot1 = new int[551];
Ypos = 1;
for (int i = 0; i < plot1.length; i++){
plot1[i] = 0;
}
}
void draw()
{
printScreen();
// If data is available,
if (sPort.available() > 0) {
sData = sPort.readStringUntil('\n');
//sData = sPort.readString();
logData(file1,getDateTime() + yCoord2 + System.getProperty("line.separator"),true);
println(sData);
sData = trim(sData);
}
if (sData != null) {
if (sData.charAt(0) == 'A'){
yCoord = int(sData.substring(1));
yCoord2 = (yCoord / 100);
println(yCoord2);
}
}
// THESE ARE THE PULSE SENSOR WAVEFORM DRAWING ROUTINES
// fisrt, move the Y coordintae of previous pulse data points over one pixel to the left
for (int i = 0; i < plot1.length-1; i++){
plot1[i] = plot1[i+1];
}
// new data enters on the right of the screen. sensor value is placed in the last array position
Ypos = yCoord2;
plot1[plot1.length-1] = Ypos; // Ypos is updated in the serialEvent tab
// This for loop renders the Pulse Sensor waveform
for (int x = 1; x < plot1.length-1; x++){ // variable 'x' will take the place of pixel x position
// get ready to make a red line
// Here are a few ways to draw the datapoints.
// line(x+61, pulseY[x]+1, x+61, pulseY[x]-1); // display previous datapoint as vertical line
// point(x+61,plot1[x]); //display previous datapoint as point
ellipse(x+70,plot1[x],1,1); // display previous datapoint as a small dot
// line(x+76, plot1[x], x+75, plot1[x-1]); //display previous datapoint as a connected line
redraw();
}
}
void printScreen(){ // DRAW MAJOR SCREEN ELEMENTS AND TEXT
background(0); // black background
noStroke();
fill(255,253,248);
}
void logData( String fileName, String newData, boolean appendData)
{
BufferedWriter bw=null;
try { //try to open the file
FileWriter fw = new FileWriter(fileName, appendData);
bw = new BufferedWriter(fw);
bw.write(newData);// + System.getProperty("line.separator"));
} catch (IOException e) {
} finally {
if (bw != null){ //if file was opened try to close
try {
bw.close();
} catch (IOException e) {}
}
}
}
String getDateTime()
{
SimpleDateFormat df = new SimpleDateFormat("[ dd/MM/yyyy HH:mm:ss ] ");
Calendar date = Calendar.getInstance(); // the current date and time
return(df.format(date.getTime()));
}
1