# Adding total distance travelled over a period of time with different speed

Hi friendly community of arduino.
I am a new user of Arduino.
Currently, I am trying to convert the analog voltage to digital speed and displaying it in a 16x2 LCD.

As i move the voltage source, the voltage is converted to speed which will increase total distance traveled.

But i can't seem to do "for loop " to calculate and store the total distance traveled like in the mileage of a vehicles's odometer

my code is as below :

``````// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 7, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int analogPin = A5;
float newdistance = 0;
signed short minutes, seconds;
char timeline[16];
void setup() {

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
// PIC 2 TOP LEFTlcd.setCursor(0,0);
lcd.print("Tesla");
lcd.setCursor(0,1);

// Print the speed of the vehicle after converting from analog to digital
// PIC 2 BOTTOM LEFTint sensorValue  = analogRead(A5);
float speed = round((sensorValue / 1023.0 )*250.0);
lcd.print(speed,0);
lcd.setCursor(3, 1);
lcd.print("km/h");

// print duration
// PIC 2 BOTTOM RIGHTint seconds= (millis()/1000);
int min = (millis()/60000);
int hour = min/60;
lcd.setCursor(14, 1);
if (min == 60)
{
min == 0;
hour++;
}
lcd.print(min);
lcd.setCursor(15, 1);
lcd.print("m");
lcd.setCursor(11, 1);
lcd.print(hour);
lcd.setCursor(12, 1);
lcd.print("h");

// set the cursor to column 0, line 11
// Show distance Travalled
// PIC 2 TOP RIGHT
** NEED HELP HERE **
float distance = ((speed / 3600) * seconds);
float newdistance = distance + newdistance;
lcd.setCursor(11,0);
lcd.print(newdistance,1);
lcd.print("km");
lcd.setCursor(16,0);
}
``````

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

UKHeliBob:

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

Alright noted with thanks ! have made the changes accordingly

Thanks for adding the code tags

i can't seem to do "for loop " to calculate and store the total distance traveled.

I don't see why you need a for loop and there is not one in your code

UKHeliBob:
Thanks for adding the code tags
I don't see why you need a for loop and there is not one in your code

Oh hi, Mr moderator.
I'm not sure as I search online and think that using a for loop helps me.
But I am confused with how to store the previous calculated value and add it to the new value when I change my voltage

You have a speed but you need to know how long you have been moving at that speed to get a distance. You seem to be calculating how far you would have traveled if you had been traveling the current speed the entire time the program was running. You need to calculate a 'delta time' (change in time) between the previous speed and the current speed. Calculate a distance for that time interval (use the average of the start and end speed) and add that distance to the total distance.

There are no delays in your sketch so your measurement intervals will be in milliseconds if not microseconds.

johnwasser:
You have a speed but you need to know how long you have been moving at that speed to get a distance. You seem to be calculating how far you would have traveled if you had been traveling the current speed the entire time the program was running. You need to calculate a 'delta time' (change in time) between the previous speed and the current speed. Calculate a distance for that time interval (use the average of the start and end speed) and add that distance to the total distance.

There are no delays in your sketch so your measurement intervals will be in milliseconds if not microseconds.

Hi john, this was exactly my thinking , so i programmed it like this :

finaldistance = olddistance + finaldistance
but It cant seem to work as i am unfamiliar with coding

No no no..

This is your current speed? float speed = round((sensorValue / 1023.0 )*250.0);

Then you need a timer to integrate speed giving you distance.

``````#include <timeObj.h>

#define DELTA_MS 100   // Lets say 100 ms timer.

timeObj  deltaT(DELTA_MS,false);
float    distance;

void setup(void) {

// Do setup things..
deltaT.start();
}

void loop(void){

float speed;

if (deltaT.ding()) {
speed = round((analogRead(A5) / 1023.0 )*250.0);
distance = distance + speed * 100;
deltaT.stepTime();
}
}
``````

If you want to try this you'll need LC_baseTools from the IDE library manager.

P.S. Actually I'd use a mapper to change this into more useful units.

-jim lee

jimLee:
No no no..

This is your current speed? float speed = round((sensorValue / 1023.0 )*250.0);

Then you need a timer to integrate speed giving you distance.

``````#include <timeObj.h>
``````

#define DELTA_MS 100   // Lets say 100 ms timer.

timeObj  deltaT(DELTA_MS,false);
float    distance;

void setup(void) {

// Do setup things..
deltaT.start();
}

void loop(void){

float speed;

if (deltaT.ding()) {
speed = round((analogRead(A5) / 1023.0 )*250.0);
distance = distance + speed * 100;
deltaT.stepTime();
}
}

``````

If you want to try this you'll need LC_baseTools from the IDE library manager.

P.S. Actually I'd use a mapper to change this into more useful units.

-jim lee
``````

Hi Jim,
I am actually trying to code an odometer,
and this is to calculate the total distance travelled by car with varying speed.
I am using OSEK and using SimulDE, so I am not sure if I can include such stuff into my codes.

Probably not.

-jim lee

wenhannnn:
I am actually trying to code an odometer,
and this is to calculate the total distance travelled by car with varying speed.

Cars generally calculate the distance by counting axle revolutions (times tire circumference) and calculate speed from that by differentiating over time (distance per hour). Do you have some way of doing that? It is more likely to be accurate than measuring an analog of tire rotation rate and integrating over time to get the distance.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.