I'm in need of help; i can't interpret the data from a fluviometer and graph it in Processing. The first data (flowRate) is received just fine, the problem is that i don't know how to read two separate data (flowRate and totalMilliliters).
Arduino Code:
byte statusLed = 13;
byte sensorInterrupt = 0;
byte sensorPin = 2;
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
void pulseCounter()
{
pulseCount++;
}
void setup()
{
Serial.begin(9600);
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH);
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void loop()
{
if((millis() - oldTime) > 1000)
{
detachInterrupt(sensorInterrupt);
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
unsigned int frac;
//Serial.print("Flow rate: ");
Serial.print(int(flowRate));
Serial.print(",");
//Serial.print("L/min");
//Serial.print("\t");
//Serial.print("Output Liquid Quantity: ");
Serial.println(totalMilliLitres);
//Serial.println("mL");
//Serial.print("\t");
//Serial.print(totalMilliLitres/1000);
//Serial.print("L");
pulseCount = 0;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
Processing Code:
import processing.serial.*;
Serial myPort;
String flowRate;
String totalMilliliters;
float button=0;
int cont = 0;
void setup()
{
size(800,700);
noStroke();
background(255);
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
print(portName);
}
void draw()
{
background(255);
stroke(0);
line(50,50,50,500);
line(50,500,700,500);
for(int i=50; i<=500;i+=50)
{
stroke(0);
line(45,i,55,i);
}
stroke(0);
line(350,495,350,505);
line(650,495,650,505);
//on off
fill(255);
stroke(0);
rect(640,570,40,20);
// TEXTO
fill(0);
textSize(20);
text("Flow rate: " +flowRate,50,600);
fill(0);
textSize(20);
text("Volume:" +totalMilliliters,50,625);
fill(0); //Grafica
textSize(10);
text("200 ml",10,450);
text("400 ml",10,400);
text("600 ml",10,350);
text("800 ml",10,300);
text("1.0 ml",10,250);
text("1.2 ml",10,200);
text("1.4 ml",10,150);
text("1.6 ml",10,100);
text("1.8 ml",10,50);
text("1 s ml",340,550);
text("2 s ml",640,550);
text("On/Off",642,582);
fill(button);
ellipse(642,602,10,10);
if(cont==1)
{
if ( myPort.available() > 0)
{
flowRate = myPort.readStringUntil('\n');
totalMilliliters += flowRate;
println(flowRate);
println(totalMilliliters);
}
}
}
void mousePressed()
{
if((mouseX>640 && mouseX<680)&&(mouseY>570 && mouseY<590))
{
cont = cont+1;
button=255;
if(cont>1)
{
cont=cont-2;
button=0;
}
}
}