Go Down

### Topic: Speedometer calculation (Read 9108 times)previous topic - next topic

#### PeterH

#15
##### Jan 09, 2012, 02:44 pm

Code: [Select]
`]unsigned long currentMillis = millis();int reading = digitalRead(inputPin);while(reading != lastButtonState) {  m = m++;`

intends to increment the variable to give me number of revolutions in a second(interval value) for this equation kmh = n * 6.8

I don't think it does anything like that. In fact I can't make any sense of it at all, and the only thing I think it'll do is lock your sketch up as soon as you enter the while loop.

I'd expect to see a variable recording last time you calculated the speed, and the number of wheel sensor inputs since that time, and some logic to see whether it was due to do the next speed calculation. The speed calculation itself would consist of a bit of arithmetic to convert the wheel counter to a speed (and perhaps display it somewhere) and then note when the next calculation was due and zero the wheel counter.

#### Nishant_Sood

#16
##### Jan 09, 2012, 02:50 pm
Thnks peter for the input NOW im considering the approach by paulS to use interrupt that whenever the sensor clicks it activates an interrupt and then counts the pulses and the loop() does the timing work like take care of how many pulses were there in a second, lets see what next turns out.
"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### dxw00d

#17
##### Jan 09, 2012, 04:04 pm
Just as a quick aside, have you checked the drive ratio of the speedometer cable? It isn't necessarily one revolution of cable for one revolution of wheel.

#### Nishant_Sood

#18
##### Jan 11, 2012, 08:02 am
As the speedometer cable will be removed and the console where the speedometer cable is inserted and screwed would be open i will FIX in THE HALL SENSOR and as the magnet moves the sensor must be flowing pulses, hold on for some time yesterday i already ordered sensors and other stuff so pretty soon i will test all this.
"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### Nishant_Sood

#19
##### Jan 13, 2012, 08:32 am
The main problem im facing is in the equation and not in the program i mean to say FOR EXAMPLE to get a speed of 1km/hr on the LCD screen i need N=0.1471 or N = 1/6.8(FOR THE EQUATION N X 6.8 (where n is the number of revolutions in one second)) so HOW THIS VALUE I CAN ATTAIN FROM A HALL SENSOR?

"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### Nishant_Sood

#20
##### Jan 13, 2012, 09:29 am
I think i need to implement the Differential here that is the difference between the 1st pulse and the last pulse in a second and then calculate as follows:

N x 6.8/Differential.

What you guys think am i right here?

"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### PeterH

#21
##### Jan 13, 2012, 08:19 pm
Use your interrupt handler, or poll the input value, to detect when a pulse occurs.

Each pulse represents one revolution of the wheel which means you have travelled Pi times the diameter of the wheel. This is a constant you can code in your sketch.

Each time a pulse occurs, calculate the elapsed time since the previous pulse. Speed is distance over time, so divide your constant value by your elapsed time to calculate the speed. The units of the speed will be determined by the units you have used for distance and time.

Since your answer will probably be quite small when you are moving slowly, I suppose you will want to use fixed point numbers so that you have a few decimal places to display. Exactly how you do that calculation would depend on what units you want to use for the display, and how many decimal places you want to display.

#### Nishant_Sood

#22
##### Jan 14, 2012, 05:33 am
OK, so the formula becomes Speed = Distance/ Elapsed time since the previous pulse.

also i have received My Hall sensors i have called for 2 hall sensors one is type 1)HALL Effect Sensor-A3290XUA
2)HALL Effect Sensor-MH183 (Unipolar)

which one i should use and i plan to just use one of them at anyone of rear or front wheel, also suggest the connection to arduino? & How much distance i should keep the magnet from Hall Sensor?
"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### GoForSmoke

#23
##### Jan 14, 2012, 07:22 am
Your speedometer cable probably turns many times per wheel revolution. Using that can get you your lower speed data as data instead of guesswork.

This... reading and lastButtonState never change in the loop so how does it exit? It does not.
Code: [Select]
`while(reading != lastButtonState) {  m = m++;}`

You are moving confidently in the wrong direction while not actually checking for mistakes or improvements, doing a great Emperor in No Clothes impression.

Quote
i need N=0.1471 or N = 1/6.8(FOR THE EQUATION N X 6.8 (where n is the number of revolutions in one second)) so HOW THIS VALUE I CAN ATTAIN FROM A HALL SENSOR?

How should the magnet on the wheel pass the sensor more than 1 time per wheel turn the way you want to set it up? The way you say with the magnet and sensor it should read 1 time per turn. Sure you can guess based on past wheel turnings but only if your speed is not changing!

What you should be doing is playing with Hall sensors and magnets and other electronic bits until you understand them before throwing them into a project.
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### dxw00d

#24
##### Jan 14, 2012, 11:29 am
Quote
Your speedometer cable probably turns many times per wheel revolution.

I tried to make that point earlier in reply 17.

#### Nishant_Sood

#25
##### Jan 14, 2012, 12:36 pm
Quote

I tried to make that point earlier in reply 17.

After that you can already see me taking up pulse differential into consideration.So your point is equally honored already here.
"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### Nishant_Sood

#26
##### Jan 15, 2012, 10:04 am
The following is the code that i have made to light the 13 pin LED when magnet comes near The hall sensor is a UNIPOLAR one

Code: [Select]
`const byte led = 13;const byte inputpin = 7;void pinchange()//ISR{  digitalWrite(led, HIGH);}void setup() {  Serial.begin(9600);pinMode(led, OUTPUT);//led 13 pin pinMode(inputpin, INPUT);// pin D7digitalWrite(inputpin, HIGH);attachInterrupt(2, pinchange, CHANGE);}void loop() {  // put your main code here, to run repeatedly:  }`

dont know where the problem is used the iNTERNAL pullup resistors on the Arduino
"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### wildbill

#27
##### Jan 15, 2012, 01:56 pm
What is the issue?

Whatever it is, the code looks odd - you setup pin 7 as an input with a pullup and then never use it. Then you attach an interrupt to pin 2, which when it fires turns an led on, but you never turn it off. What were you hoping to have it do?

#### Nishant_Sood

#28
##### Jan 15, 2012, 02:41 pm
@ WildBill i didnot attach the interrupt to pin 2 attachInterrupt(2 ,pinchange,CHANGE); here 2 refers to the interrupt handler that handles interrupts on D0 to D7 pins and the pin D7 is not left alone it gets interrupted when its state changes ,by the way i got it working with OMNIPOLAR Hall sensor the code i used to success is as follows:

Code: [Select]
`// constantsconst int hallPin = 7; // the number of the hallsensor pin// variables will change:int hallState = 0; // variable for reading the pushbutton statusconst int led = 13;void setup() {Serial.begin(9600);// initialize the pushbutton pin as an input:pinMode(hallPin, INPUT);pinMode(led, OUTPUT);}void loop(){// read the state of the pushbutton value:hallState = digitalRead(hallPin);if (hallState == HIGH) {Serial.println("MAG");digitalWrite(led, HIGH);}else {Serial.println("NO MAG");digitalWrite(led, LOW);}delay(200);}`
"Real Men can Accomplish  Anything"

- skype : nishants5
ਫ਼ਤੇਹ ਕਰੁਂ!
www.winacro.com

#### wildbill

#29
##### Jan 15, 2012, 02:45 pm
Yup, my mistake - should have read the refence before posting - I've always assumed it was the pin number - learned something.

Go Up