In my Arduino code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
double servo2;
double vel;
double acc;
double t1;
double t2;
double t3;
double t4;
double vel1;
double vel2;
double vel3;
boolean b_s1;
boolean b_s2;
boolean b_s3;
boolean b_s4;
unsigned long task_time_ms=0;
LiquidCrystal_I2C lcd(0x27,16,2);
unsigned long time_timer=millis();
void serial() {
// if((millis()-task_time_ms)>=2000){
// task_time_ms=millis();
//pal processing Serial.print("Velocitat: ");
Serial.println(vel);
//pal processing Serial.print("Acceleracio: ");
Serial.println(acc);
// }
}
void LCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vel");
lcd.setCursor(8, 0);
lcd.print("Acc");
lcd.setCursor(0, 1);
lcd.print(vel);
lcd.setCursor(8, 1);
lcd.print(acc);
}
void sensors() {
b_s1 = digitalRead(8);
b_s2 = digitalRead(9);
b_s3 = digitalRead(10);
b_s4 = digitalRead(11);
if ((!b_s1)) {
time_timer=millis();
t1 = millis();
t1 = (t1 / 1000);
}
if ((!b_s2)) {
t2 = millis();
t2 = (t2 / 1000);
vel1 = (0.26 / ((t2 - t1)));
}
if ((!b_s3)) {
t3 = millis();
t3 = (t3 / 1000);
vel2 = (0.5 / ((t3 - t2)));
}
if ((!b_s4)) {
t4 = millis();
t4 = (t4 / 1000);
vel3 = (0.26 / ((t4 - t3)));
vel = (((vel1 + ((vel2 + vel3)))) / 3);
acc = (vel / ((t4 - t1)));
}
}
void setup()
{
Serial.begin(9600); Serial.flush(); while(Serial.available()>0)Serial.read();
lcd.init();lcd.noCursor();lcd.backlight();
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
b_s1 = true;
b_s2 = true;
b_s3 = true;
b_s4 = true;
t1 = 0;
t2 = 0;
t3 = 0;
t4 = 0;
vel1 = 0;
vel2 = 0;
vel3 = 0;
vel = 0;
acc = 0;
}
void loop()
{
sensors();
LCD();
serial();
}
I calculate a speed and acceleration and print them in the serial port. So my question is: how do I put those two floats in the Processing window?
Here’s the Processing code I have for now:
import processing.serial.*;
Serial myPort; // Create object from Serial class
float vel; // Data received from the serial port
float acc;
void setup()
{
size(800, 500);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw()
{
background(200);
fill(0);
String rellotge=createTimeStamp(); //declara variable rellotge
textSize(20);
text("Rellotge= " +rellotge, 2*width/8, height-40); //Mostra Rellotge
vel = myPort.read(); // read it and store it in val
textSize(25);
text("Velocitat= " +vel+ " m/s", 50, 150);
acc = myPort.read();
textSize(25);
text("Acceleració= " +acc+ " m/s2", 50, 200);
}
String createTimeStamp() { //crea string rellotge
String timeStamp = String.format("%02d", hour());
timeStamp += ":" + String.format("%02d", minute());
timeStamp += ": " + String.format("%02d", second());
// timeStamp += " " + year();
// timeStamp += "/" + String.format("%02d", month());
// timeStamp += "/" + String.format("%02d", day());
return timeStamp;
}