Arduino speedometer not working for an unknown reason

Hi, I'm posting my problem here because that neither me or my brother can find why this program dosen't work. I tried to rewrite it completly many times, use the "else if" instead of all my "if" "else" and I tried other programs on the net. I use a reed switch wich is connected to A0 but I use it as digital input 14 because that my two 7 segments displays use all the digital outputs from 0 to 13. Thank you for your help and sorry for my bad english, i'm 16 y-o and french...

btw: this program shows the speed in KM/h and its designed for a 72.5 milimiters wheel (longboard wheels)

Here's the sketch:

#define  DELAY_START   HIGH	   // or low depending on your blink detection logic
#define  DELAY_END   !DELAY_START   // this is the inverse of the above
const int buttonPin = 14;
int buttonState = 0;
long start, duration;
long start2, duration2;
long start1, duration1;
long duration3;
long vitesse;
void setup() {
  pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);
}
void loop(){  
duration = 0; 
while( digitalRead(buttonPin) != DELAY_START   );
start1 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration1 = start1 - millis();
delay(50);
while( digitalRead(buttonPin) != DELAY_START   );
start2 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration2 = start2 - millis();
duration3 = duration1 + duration2;
duration = duration3/2;
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);


if (217/duration*3.6 == 1 or 11 or 21 or 31) {
digitalWrite(1, HIGH);
digitalWrite(4, HIGH);
delay(500);
}
else {
  digitalWrite(1, LOW);
  digitalWrite(4, LOW);
}
if (217/duration*3.6 == 2 or 12 or 22 or 32) {
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(0, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  delay(500);
}
else {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(0, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
}
if (217/duration*3.6 == 3 or 13 or 23 or 33) {
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(0, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  delay(500);
}
else {
    digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(0, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
}
if (217/duration*3.6 == 4 or 14 or 24 or 34) {
  digitalWrite(1, HIGH);
  digitalWrite(0, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(6, HIGH);
  delay(500);
}
else {
  digitalWrite(1, LOW);
  digitalWrite(0, LOW);
  digitalWrite(3, LOW);
  digitalWrite(6, LOW);
}
if (217/duration*3.6 == 5 or 15 or 25 or 35) {
  digitalWrite(2, HIGH);
  digitalWrite(1, HIGH);
  digitalWrite(0, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  delay(500);
}
else {
   digitalWrite(2, LOW);
  digitalWrite(1, LOW);
  digitalWrite(0, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
}
if (217/duration*3.6 == 6 or 16 or 26 or 36) {
   digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  delay(500);
}
else {
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
}
if (217/duration*3.6 == 7 or 17 or 27 or 37) {
  digitalWrite(9, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  delay(500);
}
else {
    digitalWrite(9, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
}
if (217/duration*3.6 == 8 or 18 or 28 or 38) {
  digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
  delay(500);
}
else {
  digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
if (217/duration*3.6 == 9 or 19 or 29 or 39) {
  digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
  delay(500);
}
else {
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
}

[code]

[/code]

danlebarbare:
I use a reed switch wich is connected to A0 but I use it as digital input 14 because that my two 7 segments displays use all the digital outputs from 0 to 13.

This doesn't apply to your actual problem, but you can look at multiplexing options for driving displays from far fewer pins.

You should also mention the Arduino hardware you are using (and make sure you use the "#" button to format your source code for easier reading -- see the FAQ.)

Also, you might need to explain exactly that "doesn't work" means in this case.

Ok thanks and I just changed the code because that it wasn't the good one (oops, sorry) and the problem is that Its just showing random numbers on the display and after like 2-4 sec. it only shows 88 and nothing changes after that...

if (217/duration*3.6 == 1 or 11 or 21 or 31) {

You can't just invent shortcuts because you are too lazy to develop proper code, and expect that the compiler will understand them.

To test a condition like this:

if (217/duration*3.6 == 1 or 11 or 21 or 31) {

you need the modulus operator %

int test_value = 217/(duration*3.6); // assuming you did not really mean (217/duration)*3.6 ! 
int remainder = test_value % 10; // take the modulus of the result

"remainder" holds the remainder (or modulus) of dividing the test_value by 10.

Of course, you could write it as one statement

int remainder = (217/(duration*3.6)) % 10;

but it is clearer to see what's going on to do it in two steps.

So your tests become:

if (remainder == 1) {
:
}
else if (remainder == 2) {
:
}
else if (remainder == 3) {
:
}

:
:

else if (remainder == 9) {
:
}

This will compile properly.

You can also look a the "switch" statement, which can be a useful alternative to many if ... else if ... statements.

BTW, what happens if remainder == 0? (i.e., test_val = 0 or 10 or 20 or 30?) You don't seem to explicitly handle that case...

And no need to apologise for your English... beats my French! :wink:

Thank you very much for your help and i'll check that out! I just saw that this is not the last version of my code and in the last one I changed the "or" for something other that I forgot but it still didn't worked. Sorry for my beginer errors...

I really thank you for your help but after MANY hours of reading on the internet and MANY modifications to my code its still not working because that I still understand what you said above (I know that I'm a bad programer, i'm a lot better to build than to program and i'm bad I dont completly understand english) after all these hours of reading on the net... Here's what I came up with but its just showing a 0 whili I turn the wheel and then it goes to 8 and it stays like that until I stop turning the wheel (then, it shoes nothing just like I want it to be). BTW in an other program I printed the speed values to my PC and they are negative; any ideas why? Oh and if someone want to write the program for me, I have no problem with that but I know that you dont really have the time will to do that. THANK YOU ALL FOR YOUR HELP!!!

#define  DELAY_START   HIGH	   // or low depending on your blink detection logic
#define  DELAY_END   !DELAY_START   // this is the inverse of the above
const int buttonPin = 14;
int buttonState = 0;
long start, duration;
long start2, duration2;
long start1, duration1;
long duration3;
long vitesse;
void setup() {
  pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);
}
void loop(){  
duration = 0;
duration2 = 0;
duration3 = 0;
while( digitalRead(buttonPin) != DELAY_START   );
start1 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration1 = start1 - millis();
delay(50);
while( digitalRead(buttonPin) != DELAY_START   );
start2 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration2 = start2 - millis();
duration3 = duration1 + duration2;
duration = duration3/2;
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);


if (217/duration*3.6 == -0 || -10 || -20 || -30) {
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
  delay(500);
}
else {
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
if (217/duration*3.6 == -1 || -11 || -21 || -31) {
digitalWrite(11, HIGH);
digitalWrite(7, HIGH);
delay(500);
}
else {
digitalWrite(11, LOW);
digitalWrite(7, LOW);
}
if (217/duration*3.6 == -2 || -12 || -22 || -32) {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(10, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(500);
}
else {
digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(10, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
if (217/duration*3.6 == -3 || -13 || -23 || -33) {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
delay(500);
}
else {
digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
}
if (217/duration*3.6 == -4 || -14 || -24 || -34) {
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(13, HIGH);
  digitalWrite(9, HIGH);
  delay(500);
}
else {
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(13, LOW);
  digitalWrite(9, LOW);
}
if (217/duration*3.6 == -5 || -15 || -25 || -35) {
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
  delay(500);
}
else {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
}
if (217/duration*3.6 == -6 || -16 || -26 || -36) {
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(500);
}
else {
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
}
if (217/duration*3.6 == -7 || -17 || -27 || -37) {
digitalWrite(9, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
delay(500);
}
else {
    digitalWrite(9, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
}
if (217/duration*3.6 == -8 || -18 || -28 || -38) {
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
  delay(500);
}
else {
  digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
if (217/duration*3.6 == -9 || -19 || -29 || -39) {
  digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
  delay(500);
}
else {
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}

danlebarbare:

if (217/duration*3.6 == -0 || -10 || -20 || -30) {

This is utter nonsense, as has already been explained.

This is utter nonsense, as has already been explained.

I think that should be:
This is STILL utter nonsense, as has already been explained.

First, THANK YOU VERY MUCH GUYS FOR YOUR HELP!!!I know guys that you already said to me that this part of the code ( || ) would not work but I dont see why... Anyways after reading even more on the forum with my brother (wich programs JAVA and things like that) we came up with this but it only shows 0 and I tested the speedometer part by printing in the values and they looked ok...

#define  DELAY_START   HIGH	   // or low depending on your blink detection logic
#define  DELAY_END   !DELAY_START   // this is the inverse of the above
const int buttonPin = 14;
int buttonState = 0;
long start, duration;
long start2, duration2;
long start1, duration1;
long duration3;
long vitesse;
int remainder;
void setup() {
  pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);
}
void loop(){ 
while( digitalRead(buttonPin) != DELAY_START   );
start1 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration1 = start1 - millis();
delay(50);
while( digitalRead(buttonPin) != DELAY_START   );
start2 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration2 = start2 - millis();
duration3 = duration1 + duration2;
duration = duration3/2;
vitesse = (217/(duration)*3.6);
remainder = vitesse % 10;
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);

if (remainder == 1) {
digitalWrite(1, HIGH);
digitalWrite(4, HIGH);
}

else if (remainder == 2) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(0, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}

else if (remainder == 3) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
}

else if (remainder == 4) {
digitalWrite(1, HIGH);
digitalWrite(3, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
}

else if (remainder == 5) {
digitalWrite(2, HIGH);
digitalWrite(1, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
}

else if (remainder == 6) {
digitalWrite(2, HIGH);
digitalWrite(1, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
}
else if (remainder == 7) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(6, HIGH);
}

else if (remainder == 8) {
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}

else if (remainder == 9) {
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(6, HIGH);
}
else if (remainder == 0) {
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}

else {
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}

}
start1 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration1 = start1 - millis();

This looks like a problem. Since millis() is going to return a value > start1 the 2nd time it is called, duration1 is going to be -ve, which I suspect is not what you want.

If you want to to calculate an interval using millis(), instead try:

start1 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration1 = millis() - start1;

Looks like this problem occurs in a few places.

Also, in this line:

vitesse = (217/(duration)*3.6);

what order of operator precedence do you intend?

Do you mean

vitesse = 217/(duration*3.6);

or

vitesse = (217/duration)*3.6;

?

Note that your parenthesis around (duration) doesn't actually do anything, so I suspect this is an area you need to pay a bit more attention to.

Btw, if you actually intend

vitesse = (217/duration)*3.6;

I would rewrite it as

vitesse = (217*3.6)/duration;

for increased accuracy when using integer arithmetic, or just simply

vitesse = 781/duration;

or for better accuracy still

vitesse = 7812/(duration*10);

Similarly, if you actually mean

vitesse = 217/(duration*3.6);

it is better to use

vitesse = 2170/(duration*36);

If using integer arithmetic, it is better not to use real values if it can be avoided, both for efficiency (faster to use just integers), and accuracy (unintended loss of precision can lead to unexpected results, like ending up with zero when expecting a non-zero result.)

Here's a study exercise:

int var1 = 5;
var1 = var1/10;
var1 = var1*10;

explain why var1 == 0, and not var1 == 5

Integer arithmetic != real arithmetic, and the difference is often very confusing for beginning programmers.

Bon chance!

OOps, I made a new version of the program whiled you posted this and here it is (it looks like its working for now :slight_smile:

#define  DELAY_START   HIGH	   // or low depending on your blink detection logic
#define  DELAY_END   !DELAY_START   // this is the inverse of the above
const int buttonPin = 14;
int buttonState = 0;
long start3, duration3;
long start, duration;
long start2, duration2;
long start1, duration1;
long duration4;
long vitesse;
int remainder;
int deuxieme;
void setup() {
  pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);
}
void loop(){ 
while( digitalRead(buttonPin) != DELAY_START   );
start1 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration1 = start1 - millis();
delay(50);
while( digitalRead(buttonPin) != DELAY_START   );
start2 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration2 = start2 - millis();
delay(50);
while( digitalRead(buttonPin) != DELAY_START   );
start3 = millis();
while( digitalRead(buttonPin) != DELAY_END   );
duration3 = start3 - millis();
duration4 = duration1 + duration2 + duration3;
duration = duration3/3;
vitesse = (217/(duration)*3.6);  //217 = circonférence de la roue en mm
remainder = vitesse % 10;
deuxieme = vitesse/10;

digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);

if (deuxieme == -1) {
digitalWrite(11, HIGH);
digitalWrite(7, HIGH);
delay(650);
}

else if (deuxieme == -2) {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(10, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(650);
}

else if (deuxieme == -3) {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
delay(650);
}

else if (deuxieme == -4) {
digitalWrite(11, HIGH);
digitalWrite(13, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
delay(650);
}

else if (deuxieme == -5) {
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
delay(650);
}

else if (deuxieme == -6) {
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(650);
}

else if (deuxieme == -7) {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(9, HIGH);

delay(650);
}

else if (deuxieme == -8) {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(650);
}

else if (deuxieme == -9) {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
delay(650);
}

else if (deuxieme == 0) {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(650);
}

else {
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
}


if (remainder == -1) {
digitalWrite(1, HIGH);
digitalWrite(4, HIGH);
delay(650);
}

else if (remainder == -2) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(0, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(650);
}

else if (remainder == -3) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(650);
}

else if (remainder == -4) {
digitalWrite(1, HIGH);
digitalWrite(3, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
delay(650);
}

else if (remainder == -5) {
digitalWrite(2, HIGH);
digitalWrite(1, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay(650);
}

else if (remainder == -6) {
digitalWrite(2, HIGH);
digitalWrite(1, HIGH);
digitalWrite(0, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
delay(650);
}
else if (remainder == -7) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(6, HIGH);
delay(650);
}

else if (remainder == -8) {
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(650);
}

else if (remainder == -9) {
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(6, HIGH);
delay(650);
}
else if (remainder == 0) {
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(650);
}

else {
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
delay(100);
}

It looks like you calculated the circumference of the wheel incorrectly.

You say it is a 72.5 mm wheel. I assume that that is the diameter.
72.5 * 3.1416 = 227.766
So, the circumference is not 217 mm; it is closer to 227 mm.

duration4 = duration1 + duration2 + duration3;
duration = duration3/3;
vitesse = (217/(duration)*3.6);  //217 = circonférence de la roue en mm

I think you made mistakes in your calculation of "duration" and "vitesse".

You also say that your two 7-segment displays use 14 inputs. Unless you go faster than 69 km/h, you only need 13 inputs. (The digit on the left only needs 6 inputs, not 7. See the picture.)

7seg.png

Not known to many it seams, but the arduino has a command to get time between pulses
http://arduino.cc/en/Reference/PulseIn

with this you can put a light sensor, over a wheel, have a hole in wheel sides so that after each turn a led can shine on a ldr, (you could make an electronic contact too but i would use a light sensor). Pulsein waits for a pulse so nothing gets in between the measurements

once you know the exact frequency of the light go on off, and get good timing results with this, then next frequency and diameter of your wheel will tell you the speed.

Thanks for the info PGTBOOS and The wheel diameter is right because that longboard wheels desintegrate (they get smaller) with time so I calculled it for a used wheel (because that my friend's ones were used)...

I was thinking about this some more.

I do not think that it is a good idea to measure the time between pulses. Two reasons:

  1. The pulse itself takes time!
  2. If the real speed is 0, there will be no pulse, and so the speedometer will be waiting forever for a pulse.

The basis for my method is this: Assume that the speed of your board is 1 kilometer per hour. One kilometer is 1000000 millimeters. Since your wheel is 217 mm in diameter, 1 kilometer equals about 4608 turns of your wheel. One hour is 3600 seconds. Divide these 3600 seconds by the 4608 turns, and you get about 0.781 second per turn.
The method itself, then, is to count the number of pulses in 0.781 second. That is your speed, in kilometers per hour.

Just count pulses. Every 781 milliseconds, display the number, then start counting again from zero.

Well this kind of measuring using pulses is widely used in manufacturing, for example robotics.
NOTICE that it measures the duration of a pulse, so there is no loose of pulse time here.
Depending on how you set pulsin you can measure the time of low signal or high signal, after a previous pulse.
Light itself is extreme fast wheel side hole should be small (needle).. and measure the LOW signal duration
instead of a hole you might put some small reflecting on your wheel to and point a led or so next to a LDR
..you could also work with large holes like 1/4 of diameter but math below will be different then
..but wouldbe easy once you get it.

Of-course it takes time to measure using PulseIn but the arduino will do nothing else until it has a next pulse.
Its the smallest code to to measure it.

for distance measuring, maybe something different might be better... dough you can work around it with some math
but if it is precise speed your after PulseIn is quite good, as it uses minimal code to measure time and so its more accurate.
to improve precision i would do a number a measurements then average them, and then display it

// 1000 micro seconds = 0,001 seconds
// 1 minute = 60000000 microseconds
// 1 hour = 360000000 microseconds
// km/hour >> lets say wheel diameter size = 217 mm (thats small)
// diameter 217mm * pi = 681,725mm
// so a full cycle gets you 681,725 mm away or ~ 0.000681725 km so 1/0,00681725 = 1 km = 1466,867 cyles
// so if you do 1466,867 cylces in a hour then your speed = 1km/p hour
//
// so ( 1466,867 cycles per hour ) equals 1466,867 cycles per 360000000 microseconds
// thus 360000000 / 1446,867 = microsecond time required for 1 cycle
// = 1 cycle should be done in 245421,023174 microseconds = 1km/hour (large number but 1 km/h is quite slow)
//
// so finally >> 1 / 245421,023174 * you measurement time = yourspeed in km/hour
//
// >> this math wont work with a integers, use dword or float
// >> also you can put 1/245421,023174 in a constant so you wont need to calculate it each time
// >>
//
// ~you might double check it but i think its oké
// (and i think previous person forgot pi x diameter, because many more cycles are needed )
//

although i have to agree that if the wheel stands still its not measuring speed and the execution flow would halt.
on the other hand, this way allows for rapid measurements might be nice for quadcopter motor speed measuring etc.
but the required code for this would be small and likely fit in a tinyduno too, then it could respond speed to a normal arduino, if you dont want execution flow to stop.. (i wonder dough, maybe speed sensors allready exist ?).
On the otherhand a tiny duno a led and a LDR equals a speed meter below 4 dollar or so, thats not expenisve :))

another thing to use averages i recommend to use the last x samples
you might do avSpeed = ( avSpeed * 9 + newspeedmeasurement ) / 10

PS a handy site to do conversions :

you can do all kind of conversions personally i prefer that, also you can calculate inside google search

I said "wheel diameter = 217 mm"
Sorry, that was a mistake.
Correct is "wheel circumference = 217 mm" (it is the week of a kind of skateboard)