2 way link with control???

boelle

Member
i think i have mentioned that before, but anyways:

on the input there are a divider network with a 1K and a 1K5 Resistor to bring a 12V input down to 4,8 V

this i need any reading to be multiplied by 2.5 to get it back where it should be.

Or am i total of track?
 

boelle

Member
of course i have lost track of where i got the source for my code, but as you can see the org code used to multiply by 4.88 and not 2.50... i simply replaced 4 8 8 (seen from first line to last one) with 2 5 0

but math was not where i scored big points so wanted to be sure
 

hippy

Ex-Staff (retired)
It might be easier to use 3K top, 1K bottom, turn 12V into 3V and then multiply by 4. You could make the maths easy that way and also have headroom for above 12V; a six cell lead acid battery may actually be about 13.8V and the vehicle supply may rise nearer 15V when running and charging the battery. Putting more than 5V in could damage the PICAXE when it is runing.
 

BeanieBots

Moderator
I for one was confused by your equations and hence refrained from an immediate reply.
I go along with Hippy regarding the divider. You really to be able to handle 15v for a charging battery.

PICAXE maths is strictly left to right. Somthing that catches many out initially.
Have a play in the simulator.
To multiply by 2.5, first multiply by 25 then divide by 10 to get around the integer only aspect of PICAXE maths.
 

boelle

Member
upps on the above 12V,

but yep the way math are done on the picaxe are a bit new to me, i did try it in the simulator and was wondering why no output, i figured it was because of an overflow of some kind but never solved it before now

thanks a lot on that one
 

hippy

Ex-Staff (retired)
I got misled by the 'TF tenths of a degree' comment in the calculation, assumed Fahrenheit temperature then re-read it as wanting voltage, hence sought clarification.

There's also the two-part nature of the calculations; obtaining a reading, scaling then converting to display format. Whether multiplied by 2.5 or 4, as BeanieBots says, multiply by 25 ( or 40 ) then divide by 10. That gives the integer (whole) part of the value, multiply by 25 ( or 40 ) then modulo 10 gives the 'after the decimal point' digit.

ReadAdc VOLT_PIN, b0
vWhole = b0 * 25 * 5 / 255 / 10
vTenths = b0 * 25 * 5 / 255 // 10

ReadAdc VOLT_PIN, b0
vWhole = b0 * 25 / 51 / 10
vTenths = b0 * 25 / 51 // 10

You can get better resolution with READADC10 -

ReadAdc10 VOLT_PIN, w0
vWhole = w0 * 25 * 5 / 1023 / 10
vTenths = w0 * 25 * 5 / 1023 // 10

But then you have to adjust the calculations to avoid overflow, intermediate results being greater than 16-bit.

Added : This assumes an exact 5V reference. If something else the equations will change.
 
Last edited:

boelle

Member
So if I understand correct my maths should be:

ReadADC10 2, ADVal

Volts = ADVal * 4


Whole = Volts / 10 ' whole
Fract = Volts % 10 ' tenths

or should the % be // ?
 

hippy

Ex-Staff (retired)
So if I understand correct my maths should be:

ReadADC10 2, ADVal

Volts = ADVal * 4
Not sure how that ties up with what has been written in the past few posts. I would suggest you work it out from first principles ...

1) Vref is 5V ... if 5V is your supply
2) ReadAdc10 gives a value 0 to 1023 representing 0V to Vref
3) Each step value of the adc represents Vref/1023 volts
4) So if you put 3V into the pin, what will ReadAdc give as a value ?
5) How would you convert that value back to "3" ?
6) How would you convert that value to "30" to have a decimal digit ?

For modulo, % and // are the same.
 
Last edited:

boelle

Member
each step is 0,01960784313725490196078431372549 Volts (5/255)

hmm.. math is not my strong side so divide the number in to 3 volts should give amount of steps about 153 steps if i'm not wrong

multiply that by 5 and divide by 255 gives me 3

since the 3 volts at the chip is relly 12V we need to multiply again by 4

a test: 15V in should give 3,75 V at the chip. 3,75 divided by the long number above gives 191,25 steps... now i know the chip would say either 191 or 192 or an overflow. but lets assume it says 191

191*5/255*4=14,98V

Very close

am i doing it total wrong?
 

hippy

Ex-Staff (retired)
Bang on, well done.

so with 191 being your ADVal, you can calculate volts from ...

ReadAdc 2, ADVal
volt = ADVal * 5 / 255 * 4

To maximise accuracy you can do multiply before divides, so ..

ReadAdc 2, ADVal
volt = ADVal * 5 * 4 / 255
 

boelle

Member
got the cold so might sound drunk :D

would a calc where the output is decimals not cause an overflow? or will the picaxe just discard anything but the whole number?
 

hippy

Ex-Staff (retired)
PICAXE only deals with integer numbers. The best you can have are numbers which are a factor of 10, 100 and so on larger than they really are to represent a decimal number; for example 123, this could be considered to represent -

123
1.23
12.3
0.123

The largest integer the PICAXE can handle is 65535, so you need to ensure that calculatins never exceed that.
 

boelle

Member
what i had in mind is:

191 * 5 * 4 / 255 would give 14,980392156862745098039215686275 on a normal calculator

but what happens inside the picaxe?
 

Dippy

Moderator
Calculations are left --> right

191 * 5 * 4 / 255
will be treated as:
191 * 5 = 955
955 * 4 = 3820
3820 /255 = 14 (remember it's integer so things drop off...)

As hippy has said (and it's in the Manuals if you read them) the limit for a word variable is 65535.
So, use that to your advantage.

As hippy has suggested try multiplying by 10

Now:
191 * 5 * 4 * 10 / 255
ends up as = 149
See! A third digit has appeared. You've gained a decimal place.

And then you can use the leftovermodulus // function to effect too.

Have a go for yourself. Try something. Use the Simulator. Experiment and learn.
 

boelle

Member
its just the cold that make my brain work sh*t

but i got the last piece of the puzzle now, i have read the manuals but never understood it that tad clear to make it work
 

boelle

Member
btw... are there a timeout for how long i'm logged in? it seems that i have to enter my username and pass several times a day even thou i put a checkmark in "remember me" i can understand a timeout on public pc's but home on my own pc that is not even shared with my girlfiend it's a pain to enter both things every now and then
 

Dippy

Moderator
Yes, there is a timeout. I don't know how long.
if you want to stay logged on for long periods (that's sad) then leave it on "Who's Online".

If you are a slow typer like me then type in Notepad and cut'n'paste.
 

techElder

Well-known member
not a slow typer, just a pain to enter pass and username several times a day :D
If you have to do that, then perhaps you aren't allowing cookies on your local machine.

Mine stays logged in all the time, but I don't stay "on" here all that time. I come and go and use the "New Posts" menu item.
 

hippy

Ex-Staff (retired)
it seems that i have to enter my username and pass several times a day even thou i put a checkmark in "remember me"
Could be your browser, cookie or other browser settings. I have "remember me" ticked and never have to login.
 
Top