Sign in to follow this  
Followers 0
albino001

Help with math functions

6 posts in this topic

Alrighty so I have the rest of this working with the arduino and stuff so the leds are just part of another function and all so heres what im trying to do, but cant find the right functions to multiply/divide and stuff like that.

max number for ex is 250

Number changes from 250 to 0

100% of <MAX> is 250

95% of <MAX> is 237.5

...

5% of <MAX> is 12.5

0% of <MAX> is 0

---

If <Num> equals 100% LED 1-20 on

If <Num> equals 95% LED 1-19 on

...

If <Num> equals 5% LED 1 on

If <Num> equals 0% No LED on

I figure ill just use a quick multiplication , say (250*.05) then a function like

If <Num> is > or = to <Percentage1(being 5 %)> than turn led1 on

If <Num> is < <Percentage1> turn led off

...

If <Num> is > or = to <Percentage20(being 100%)> than turn led20 on

If <Num> is < <Percentage1> turn led off

I figure it might be easier mapping each percentage to its own led rather then if its 100 percent have led turn led 1-20 on and if (assuming its at 100%) the numbers are greater then percentages 19-0 then do nothing.

Any help is greatly appreciated.

Share this post


Link to post
Share on other sites



are u working with arduino code or autoit? if autoit u can use a switch to check for what ur wanting or a if statement example

If $max=< .05*250 then ; 1 is 100%   .05 is 5% ect....
          do this code here
elseif $max=<.10*250
       do this  code here
elseif $max=<.15*250
        do this code here
else
do this
endif

Share this post


Link to post
Share on other sites

code example depending what you want as input:

For $i = 1 To 100
ConsoleWrite($i&":"&floor(($i/10)*2) & @LF)
next
For $i = 1 To 250
ConsoleWrite($i&":"&floor(($i/250) * 20) & @LF)
next

Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

Share this post


Link to post
Share on other sites

Thank you everyone for the answers

@themaskedwolf I am using autoit code, this works quite nicely thanks.

@69255 Its not that im using 20 leds, i just needed 1 led per functions

say 10 percent for led 2 ::: If $num >= ($max*10)/100 Then

_CommSendString("2") ;On

EndIf

If $num < ($max*10)/100 Then

_CommSendString("B") ;Off

EndIf

If thats not what you were meaning my bad very little understanding of autoit

@qsek to be honest I only somewhat get what that code is doing.

@everyone im getting data for this number, from another program, temperature of stuff, which constantly changes, cold warm warmer etc so i want to constantly refresh the data in the function, max doesnt need refreshed i set it myself just need the number to be refresh.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

well, we gave you the formula for calculating. You can use this in 20 IF statements if you like.

; Example 1

$num = 47 ; input/temperature value
$led = floor(($num/250) * 20); how much leds are on
ConsoleWrite("LEDs on:" & $led & @LF)

If 1 <= $led Then
   _CommSendString("1") ;On
else
   _CommSendString("A") ;Off
EndIf

If 2 <= $led Then
   _CommSendString("2") ;On
else
   _CommSendString("B") ;Off
EndIf

....
....

Or you can loop through every LED and check if its to be turned on or off:

; Example 2

$num = 47 ; input/temperature value
$led = floor(($num/250) * 20); how much leds are on
ConsoleWrite("LEDs on:" & $led & @LF)

For $i = 1 To 20 ; go through LEDs 1 to 20 and set which are On or Off
    If $i <= $led Then ; if current LED <= LEDs On
        _CommSendString($i) ;On
    else
        _CommSendString(chr(64+$i)) ;Off, chr(65) = "A"
    EndIf
next

Func _CommSendString($sCom)
    ConsoleWrite($sCom & @LF)
EndFunc

I dont know if those ON/Off commands you send are the real one or just examples, but if you need capital letters to turn the LEDs off, chr would be the function you need here.

You could even optimize this by only send commands to those LEDs that change, but that would require a more complicated code.

Edited by qsek

Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ohh i get it now, and 250 was just a example number... i hope... Im gonna try this now.Any reason why, With just my current

If $num >= ($max*50)/100 Then
       _CommSendString("1");on 1
       EndIf
    If $num < ($max*50)/100 Then
       _CommSendString("A");off 1
       EndIf
    If $num >= ($max*100)/100 Then
       _CommSendString("2");on 2
    EndIf
    If $num < ($max*100)/100 Then
       _CommSendString("B");off 2
       EndIf
    If $num <= ($max*0)/100 Then
       _CommSendString("B");off 2
       _CommSendString("A");off 1  
       EndIf

It takes ~10 seconds to read data, send strings, repeat.

I no its not the read data part because i have this in a gui which updates every second, is it my math?

This is a function btw, i call on this function when i click the "read" button i made.

Oh and its still in testing using only 2 leds , one for 50 and up percent the other for 100 percent of the max

Edited by albino001

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0