Resend my activation email : Register : Log in 
BCF: Bike Chat Forums


Calling Electronics/Maths geeks :-)

Reply to topic
Bike Chat Forums Index -> The Geek Zone
View previous topic : View next topic  
Author Message

The Shaggy D.A.
Super Spammer



Joined: 12 Sep 2008
Karma :

PostPosted: 16:08 - 26 Sep 2009    Post subject: Calling Electronics/Maths geeks :-) Reply with quote

I have a circuit, part of which is based on a voltage divider using resistors :-

https://www.bikechatforums.com/files/voltagedivider.gif

The maths behind this is :-

Vout = Vin * R2 / (R1 + R2)

So, for example, if R1 and R2 were equal, Vout would be half of Vin. In my circuit, R1 is fixed at 1k, whilst R2 varies between 25k and 1k, in steps of 1k. With an input of 5V, this gives me the following theoretical voltages at each step :-

0 4.807692308
1 4.8
2 4.791666667
3 4.782608696
4 4.772727273
5 4.761904762
6 4.75
7 4.736842105
8 4.722222222
9 4.705882353
10 4.6875
11 4.666666667
12 4.642857143
13 4.615384615
14 4.583333333
15 4.545454545
16 4.5
17 4.444444444
18 4.375
19 4.285714286
20 4.166666667
21 4
22 3.75
23 3.333333333
24 2.5

I'm using an Analogue to Digital converter to read these values, in a C# proggy. I need to return what step number the circuit is at, from 0 to 24. If the voltage was linear, this would be simple. Alas Smile

These values vary due to heat, contact resistance, quality of components, stability of the input voltage etc. Not by much, but enough to stop me being able to do an exact equality check. To get around this, I have a table of thresholds which I simply loop through and compare the value against. These are represented by an integer, thus :-

Value = INT(Vin / 32767 * Vout)

The ADC does this for me, I'm just including it for clarity.

These thresholds are the halfway point between each step, so that (for example) between steps 24 and 23, my threshold is 19113. If I'm lower than that, I'm at step 24. If I'm lower than 23209, I'm at step 23, and so on. Once I've obtained a step number, I can exit my loop.

Which brings me, finally, to my question.

Can anyone think of a clever formula that can remove the necessity of looping through a lookup table, to return an integer of 0 to 24 dependant on the output voltage? I get the feeling it's staring me in the face, but I never was any good at maths.
____________________
Chances are quite high you are not in my Monkeysphere, and I don't care about you. Don't take it personally.
Currently : Royal Enfield 350 Meteor
Previously : CB100N > CB250RS > XJ900F > GT550 > GPZ750R/1000RX > AJS M16 > R100RT > Bullet 500 > CB500 > LS650P > Bullet Electra X & YBR125 > Bullet 350 "Superstar" & YBR125 Custom > Royal Enfield Classic 500 Despatch Limited Edition (28 of 200) & CB Two-Fifty Nighthawk > ER5
 Back to top
View user's profile Send private message You must be logged in to rate posts

The Shaggy D.A.
Super Spammer



Joined: 12 Sep 2008
Karma :

PostPosted: 16:27 - 26 Sep 2009    Post subject: Reply with quote

If it makes it easier to understand, here's a spready.
____________________
Chances are quite high you are not in my Monkeysphere, and I don't care about you. Don't take it personally.
Currently : Royal Enfield 350 Meteor
Previously : CB100N > CB250RS > XJ900F > GT550 > GPZ750R/1000RX > AJS M16 > R100RT > Bullet 500 > CB500 > LS650P > Bullet Electra X & YBR125 > Bullet 350 "Superstar" & YBR125 Custom > Royal Enfield Classic 500 Despatch Limited Edition (28 of 200) & CB Two-Fifty Nighthawk > ER5
 Back to top
View user's profile Send private message You must be logged in to rate posts

The Shaggy D.A.
Super Spammer



Joined: 12 Sep 2008
Karma :

PostPosted: 16:51 - 26 Sep 2009    Post subject: Reply with quote

I've only got Vout available to the proggy, expressed as an integer between 0 and 32767. With a smoothing capacitor, the values are as near as dammit, and I can get away with dividing the result by 10 to lower the DAC's resolution, which gives me :-

24) 3150
23) 3145
22) 3140
21) 3134
20) 3127
19) 3120
18) 3112
17) 3104
16) 3094
15) 3083
14) 3071
13) 3058
12) 3042
11) 3024
10) 3003
9) 2978
8) 2949
7) 2912
6) 2867
5) 2808
4) 2730
3) 2621
2) 2457
1) 2184
0) 1638

But it's still not linear.
____________________
Chances are quite high you are not in my Monkeysphere, and I don't care about you. Don't take it personally.
Currently : Royal Enfield 350 Meteor
Previously : CB100N > CB250RS > XJ900F > GT550 > GPZ750R/1000RX > AJS M16 > R100RT > Bullet 500 > CB500 > LS650P > Bullet Electra X & YBR125 > Bullet 350 "Superstar" & YBR125 Custom > Royal Enfield Classic 500 Despatch Limited Edition (28 of 200) & CB Two-Fifty Nighthawk > ER5
 Back to top
View user's profile Send private message You must be logged in to rate posts

The Shaggy D.A.
Super Spammer



Joined: 12 Sep 2008
Karma :

PostPosted: 17:08 - 26 Sep 2009    Post subject: Reply with quote

Well bugger me with a slide rule. It seems that typing all this out focused me enough Smile

If Vout is the DAC reading and Vin is 32767, then

Step = ((Vout * R1 / (Vin - Vout)) / 1000) - 1

Dance!

As you were Smile
____________________
Chances are quite high you are not in my Monkeysphere, and I don't care about you. Don't take it personally.
Currently : Royal Enfield 350 Meteor
Previously : CB100N > CB250RS > XJ900F > GT550 > GPZ750R/1000RX > AJS M16 > R100RT > Bullet 500 > CB500 > LS650P > Bullet Electra X & YBR125 > Bullet 350 "Superstar" & YBR125 Custom > Royal Enfield Classic 500 Despatch Limited Edition (28 of 200) & CB Two-Fifty Nighthawk > ER5
 Back to top
View user's profile Send private message You must be logged in to rate posts

carvell
Scuttler



Joined: 05 Sep 2003
Karma :

PostPosted: 17:32 - 26 Sep 2009    Post subject: Reply with quote

Or - a more ridiculous way:

y is the step and x is the ADC reading.

y = 0.0033x^5 - 0.2327x^4 + 6.2145x^3 - 78.767x^2 + 494.73x + 1694.8

It's a decent approximation!
____________________
Yamaha TDM 850
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

The Shaggy D.A.
Super Spammer



Joined: 12 Sep 2008
Karma :

PostPosted: 17:37 - 26 Sep 2009    Post subject: Reply with quote

I'd hate to try and debug that in a couple of year's time though Smile
____________________
Chances are quite high you are not in my Monkeysphere, and I don't care about you. Don't take it personally.
Currently : Royal Enfield 350 Meteor
Previously : CB100N > CB250RS > XJ900F > GT550 > GPZ750R/1000RX > AJS M16 > R100RT > Bullet 500 > CB500 > LS650P > Bullet Electra X & YBR125 > Bullet 350 "Superstar" & YBR125 Custom > Royal Enfield Classic 500 Despatch Limited Edition (28 of 200) & CB Two-Fifty Nighthawk > ER5
 Back to top
View user's profile Send private message You must be logged in to rate posts
Banned This post is not being displayed because the poster has bad karma. Unhide this post / all posts.

The Shaggy D.A.
Super Spammer



Joined: 12 Sep 2008
Karma :

PostPosted: 18:02 - 26 Sep 2009    Post subject: Reply with quote

Well, we wouldn't want you to tax yourself.
____________________
Chances are quite high you are not in my Monkeysphere, and I don't care about you. Don't take it personally.
Currently : Royal Enfield 350 Meteor
Previously : CB100N > CB250RS > XJ900F > GT550 > GPZ750R/1000RX > AJS M16 > R100RT > Bullet 500 > CB500 > LS650P > Bullet Electra X & YBR125 > Bullet 350 "Superstar" & YBR125 Custom > Royal Enfield Classic 500 Despatch Limited Edition (28 of 200) & CB Two-Fifty Nighthawk > ER5
 Back to top
View user's profile Send private message You must be logged in to rate posts

Fnatic
World Chat Champion



Joined: 11 May 2008
Karma :

PostPosted: 09:23 - 27 Sep 2009    Post subject: Reply with quote

cba
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts
Old Thread Alert!

The last post was made 16 years, 352 days ago. Instead of replying here, would creating a new thread be more useful?
  Display posts from previous:   
This page may contain affiliate links, which means we may earn a small commission if a visitor clicks through and makes a purchase. By clicking on an affiliate link, you accept that third-party cookies will be set.

Post new topic   Reply to topic    Bike Chat Forums Index -> The Geek Zone All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Read the Terms of Use! - Powered by phpBB © phpBB Group
 

Debug Mode: ON - Server: birks (www) - Page Generation Time: 0.08 Sec - Server Load: 0.74 - MySQL Queries: 13 - Page Size: 62.84 Kb