calculate time diference and store variables
in
Integration and Hardware
•
2 years ago
Im trying to use a sonar wuith arduino, and I want to calculate the velocity and aceleration of a person aproaching the sensor. It's for an instalation.
I can alredy calculate the distance and send the information to processing via serial port, but I need to store the information on time and distance in specific points(distances), in order to make the calculations.
check the code:
import processing.serial.*;
Serial myPort;
int d1;
int d2;
int t1;
int startingTime = 0;
void setup() {
myPort = new Serial(this, Serial.list()[0], 9600);
String[] fontList = fontA.list();
println(fontList);
startingTime = millis();
}
void draw() {
background(102);
fontA = createFont("Verdana", 32);
if (myPort.available() > 0) {
val = myPort.read();
}
myPort.clear();
if (val<=34) {
int tempo = (millis() - startingTime)/1000;
d1=val ; // if i do this d1 changes alog with val and I want d1 to be the value of val at this time
if (val<=32){
t1=millis()/1000 tempo; //tring to get the time diference
d2=val //samething with d1, so i can calculate (d1-d2)/(time diference)=velocity
textFont(fontA, 150);
fill(0);
text ("final time", 400, 400);
//where finaltime is a prediction of how long it'll take for the person to get to a certain point.
fill(0);
text ("/ "+tempo, 700, 400);
}
}
//this is me tryin to solve the problem, hope u guys understand what I'm tryng to do.
if (val>34) {
// reset start time
startingTime = millis();
}
}
I'm not a programer, I'm an artist, that is the reason for the ridiculous code.
also i need an answer before the end of the week. Im sure there is a ridiculusly easy answer, but i couldn't find anything helpful..
1