Pages: 1 [2] 3 4 5   Go Down
Author Topic: Speedometer calculation  (Read 3891 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?


Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Your speedometer cable probably turns many times per wheel revolution.

I tried to make that point earlier in reply 17.
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 D7
digitalWrite(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
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@ 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:
// constants
const int hallPin = 7; // the number of the hallsensor pin

// variables will change:
int hallState = 0; // variable for reading the pushbutton status
const 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);
}
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup, my mistake - should have read the refence before posting - I've always assumed it was the pin number - learned something.
Logged

Pages: 1 [2] 3 4 5   Go Up
Jump to: