Pages: [1]   Go Down
Author Topic: Speedometer simple code addition?  (Read 1075 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In my attempt to make a simple speedometer,after much research i found this code:
Code:
int inputPin = 2;
int pulseDuration = 0;
int pulseBegin = 0;


void setup()
{
  pinMode(inputPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
     if (digitalRead(inputPin) == LOW)
{
  pulseBegin = millis();
  while (digitalRead(inputPin) == LOW){}
  pulseDuration = millis()-pulseBegin;
 
Serial.println(pulseDuration);
}
}

After making a ADC circuit ,i got it running this code works fine!! BUT IM HAVING TROUBLE MAKING AN ADDITION TO THE CODE AND I JUST GET A FIXED "-1" SERIAL OUTPUT WHEN I APPLY THE FORMULA FOR SPEED..BASICALLY I WANT "pulseDuration" (time taken by each round which is being displayed by this code) and divide it by 1000 then further divide that answer by 60,i want my serial monitor to display this value...how can i add this to the code?
Logged

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

Quote
BUT IM HAVING TROUBLE
We might be a bunch of grumpy old men, but we aren't deaf. You can stop shouting.

You can start posting the modifications that you made to this code. You already said this code works. It's the modifications that you made that are causing you problems, so hiding them behind your back is not how you get help.
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


   I am not an expert but, one problem I see is you need to change your int to unsigned long. Your program uses similar variables like "Blink without Delay". You do need to show all of your code you are using.

Code:
unsigned long pulseBegin = millis();
unsigned long pulseDuration = millis() - pulseBegin;

Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul....please resolve your problems before commenting here.the caps were just used to separate the info from the question/problem...

[/quote]

You can start posting the modifications that you made to this code. You already said this code works. It's the modifications that you made that are causing you problems, so hiding them behind your back is not how you get help.
[/quote]

WTH.....if u like to cry at everything pls just dont comment...

anyways these are my additions:

Code:
int inputPin = 2;
int pulseDuration = 0;
int pulseBegin = 0;
int speedVal = 0;

void setup()
{
  pinMode(inputPin, INPUT);
  Serial.begin(9600);
}

void loop()
{
     if (digitalRead(inputPin) == LOW)
{
   pulseBegin = millis();
   while (digitalRead(inputPin) == LOW){}
   pulseDuration = millis()-pulseBegin;

}
speedVal = (0.057/(pulseDuration/1000))
Serial.println(speedVal);
}
Logged

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

I'll ignore your childish comments.

Think about what this is going to do if pulseDuration is less than 1000.
Code:
speedVal = (0.057/(pulseDuration/1000))
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the pulseDuration cant be less than 1000,but due to some error sometimes i do get a 0.

P.S i am a child! 
Logged

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

Quote
the pulseDuration cant be less than 1000
Why can't it?

Just for the sake of argument, though, since you've show not output to show what average values you are getting for pulseDuration, lets assume that pulseDuration has a value of 3500.

3500/1000 is 3.5, but, since pulseDuration is an int, the result is 3.

0.057 / 3 = 0.019

As in int
Code:
int speedVal = 0;
that is 0.

As pulseDuration goes up, the dividend gets larger. Therefore, the result of the division operation gets smaller. It will never be greater than 1, so the speedVal will never be other than 0.

If pulseDuration exceeds 32767, it actually becomes negative, since 32767 is the maximum value of an int.

If pulseDuration is ever less than 1000, the division operation will result in a value of 0. Dividing 0.057 by 0 is not a good idea.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh Okay...with a test setup,i was getting an average pulseDuration of 835...any way around this??????

so heres my setup:

im trying to make a rpm/speedomemter...By this code i am able to get the time between each rotation (pulseduration)..What would be the most suitable code to achieve this?
Logged

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

Quote
i was getting an average pulseDuration of 835
Great. Now, we're making progress.

Change speedVal to be a float variable, rather than an int variable. Divide pulseDuration by 1000.0, rather than 1000.

Let us know how that works for you.
Logged

Pages: [1]   Go Up
Jump to: