Pages: [1] 2   Go Down
Author Topic: Arduino speedometer not working for an unknown reason  (Read 1732 times)
0 Members and 1 Guest are viewing this topic.
Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:

#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]
« Last Edit: August 14, 2012, 09:01:52 pm by danlebarbare » Logged

Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 246
Code Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I yield() for co-routines.

Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: August 14, 2012, 09:03:29 pm by danlebarbare » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 828
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To test a condition like this:

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

you need the modulus operator %

Code:
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

Code:
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:

Code:
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! ;-)

« Last Edit: August 15, 2012, 07:31:17 am by pico » Logged

WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!!



Code:
#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);
}
« Last Edit: August 16, 2012, 08:39:25 pm by danlebarbare » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

This is utter nonsense, as has already been explained.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Code:
#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);
}

}
« Last Edit: August 20, 2012, 12:41:31 pm by danlebarbare » Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 828
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:

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

Looks like this problem occurs in a few places.

Also, in this line:

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

what order of operator precedence do you intend?


Do you mean


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

or

Code:
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


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

I would rewrite it as


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

for increased accuracy when using integer arithmetic, or just simply

Code:
vitesse = 781/duration;

or for better accuracy still

Code:
vitesse = 7812/(duration*10);

Similarly, if you actually mean

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

it is better to use

Code:
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:

Code:
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!




« Last Edit: August 22, 2012, 06:05:44 pm by pico » Logged

WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

#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);
}
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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 (3.29 KB, 400x300 - viewed 24 times.)
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Québec, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)...
Logged

Pages: [1] 2   Go Up
Jump to: