Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 15:31 29 Mar 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : scale function

     Page 1 of 2    
Author Message
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 05:49pm 26 Sep 2022
Copy link to clipboard 
Print this post

I have been testing a scale or map idea.
Trying floats and integers. I am not good at sums but this is near accurate and you can mix floats and integers.
This code scales 0 to 3.3 to 0 to 270.. for a dial, degrees.
The final text is 3.39  270
so there is errors but rounding and what mmb does is beyond my capabilities.
for next problem.
I am trying to pass parameters to the sub. eg scale 0,3.3,0,270,vartoscale
but do not from manual know how, arg1,arg2
It is interesting. Could it be useful?

option explicit
dim float sc_syscalc , sc_syscalcf , sc_fromHigh,sc_fromLow , sc_toHigh , sc_toLow , lim1 , sc_in',sc_out
dim integer sc_out
sc_fromLow=0 : sc_fromHigh=3.3 : sc_toLow=0 : sc_toHigh=270 'scale (0 to 3.3) to (0 to 270)
cls
for sc_in=0 to 3.3 step 0.01
 scale
 text 0,0,str$(sc_in)
 text 48,0,str$(sc_out) ' lcd display
' pause 20
next sc_in
do
loop
'--------------
sub scale
 'the scale code
 sc_syscalcf = 0
 lim1 = sc_toHigh - sc_toLow
 '
 do while lim1 > 0
sc_syscalcf = sc_syscalcf + ( sc_in - sc_fromLow )
   lim1 = lim1 - 1
 loop
 '
 sc_syscalc = (sc_fromHigh - sc_fromLow )
 sc_out = ((sc_syscalcf / sc_syscalc) + sc_toLow)
end sub
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3641
Posted: 08:33pm 26 Sep 2022
Copy link to clipboard 
Print this post

  stanleyella said  I am trying to pass parameters to the sub. eg scale 0,3.3,0,270,vartoscale
but do not from manual know how, arg1,arg2

If you can't handle the example(s) in the manual, maybe refer to the many examples which have been posted.

John
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 09:20pm 26 Sep 2022
Copy link to clipboard 
Print this post

  JohnS said  
  stanleyella said  I am trying to pass parameters to the sub. eg scale 0,3.3,0,270,vartoscale
but do not from manual know how, arg1,arg2

If you can't handle the example(s) in the manual, maybe refer to the many examples which have been posted.

John

IMHO I should not need the many examples as the manual should explain it all ie real value examples not arg1.arg2 etc. which is open to misinterpretation ie does not explain sub parameters to me. Is that my problem when I try and the manual is my guide?
I find other users code difficult to understand sometimes. I bet people do not understand my code either.

This is just my take on a scale/map function. I looked in mmb maths and again I do not understand the arrays stuff for attempting scaling for a single value in one scale to another value in an other scale.

The open source code I have tried to convert to mmb is useful for me for many applications as just a sub or even in line. If there is a better version a link would be helpful but not all example code is picomite non vga.
stanno
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 09:40pm 26 Sep 2022
Copy link to clipboard 
Print this post

Using integers works. It was for no floats but try it with floats. Interesting.


option explicit
dim integer sc_syscalc , sc_syscalcf , scal_fromHigh,scal_fromLow , scal_toHigh , scal_toLow , lim1 , scal_in , scal_out
'scale (0 to 3300) to (0 to 270)
scal_fromLow=0 : scal_fromHigh=3300 : scal_toLow=0 : scal_toHigh=270
cls
for scal_in = 0 to 3300
 scale
 text 0,0,str$(scal_in)
 text 48,0,str$(scal_out) ' lcd display
' pause 20
next scal_in
do
loop
'--------------
sub scale
 sc_syscalcf = 0
 lim1 = scal_toHigh - scal_toLow
 '
 do while lim1 > 0
sc_syscalcf = sc_syscalcf + ( scal_in - scal_fromLow )
   lim1 = lim1 - 1
 loop
 '
 sc_syscalc = (scal_fromHigh - scal_fromLow )
 scal_out = ((sc_syscalcf / sc_syscalc) + scal_toLow)
end sub

 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 5648
Posted: 09:42pm 26 Sep 2022
Copy link to clipboard 
Print this post

You can think of the PicoMite VGA as being a LCD with a resolution of 320x240 16 colours in mode 2 or as 640x480 in mono colour when in mode 1. Just about everything else is the same.

"Command <arg1>, <arg2>" only means that you have to include the arguments when you call it. It's not some strange magic. ;)

The manual can't possibly include examples of everything you might want to do in MMBasic. All it can do is give you the tools - it's up to you to use them to solve your problem. If you are trying to convert to MMBasic from C or Python or some other BASIC then you first have to fully understand both the source and the capabilities of MMBasic. Some things won't convert directly and you may have to write blocks of code to replace single lines in the source - or it may work the other way.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5867
Posted: 10:33pm 26 Sep 2022
Copy link to clipboard 
Print this post

I am sorry, I have no idea what your SUB is trying to do but given the original problem, I would use a FUNCTION rather than a SUB.
FUNCTIONs are best if you have one value to return.

  Quote  '
  
OPTION EXPLICIT
  
OPTION DEFAULT INTEGER
  
  
DIM temp AS FLOAT
  
DIM Vin AS FLOAT
  
  
FOR temp = 0 TO 100 STEP 10
    
PRINT scale(0,100,32,212,temp)
  
NEXT temp
  
PRINT
  
FOR Vin = 0 TO 3.31 STEP 0.1
    
PRINT STR$(Vin,2,1), INT(scale(0,3.3,0,270,Vin))
  
NEXT Vin
  
FUNCTION scale(fromLow!, fromHigh!, toLow!, toHigh!, in!) AS FLOAT
  scale = (toHigh!-toLow!)/(fromHigh!-fromLow!)*in!+toLow!-fromLow!
END FUNCTION 




With a function, if a variable is declared in the parameters (argumentss) or as LOCAL in the function, the variable is treated as local and doesn't need to be DIMmed as a global.
That makes code easier to manage.

Jim
VK7JH
MMedit   MMBasic Help
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 1721
Posted: 10:45pm 26 Sep 2022
Copy link to clipboard 
Print this post

You are over thinking it.
to re-scale a (0 to 3.3) variable to (0 to 270) just multiply it by 270 and divide by 3.3

Var270 = Var3.3 * 270 / 3.3

That's all there is to it.
Don't use integers, they just mess up the division.
The Pico does not have the limitations of an 8 bit chip, it's very capable, let it do the work.

It only gets a little more complicated if the start of the ranges isn't zero.

If you wanted to scale (0.3 to 3.3) to (30 to 270)

Var270 = (Var3.3 - 0.3) * (270 - 30) / (3.3 - 0.3) + 30
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3641
Posted: 11:04pm 26 Sep 2022
Copy link to clipboard 
Print this post

  stanleyella said  
  JohnS said  
  stanleyella said  I am trying to pass parameters to the sub. eg scale 0,3.3,0,270,vartoscale
but do not from manual know how, arg1,arg2

If you can't handle the example(s) in the manual, maybe refer to the many examples which have been posted.

John

IMHO I should not need the many examples as the manual should explain it all

That is a ridiculous statement. The manual already does what almost anyone needs.

No manual can ever do what you appear to want for any even moderately powerful language.

If you can't handle that fact, you would be wiser to pursue another interest.

Otherwise, put more effort in and in more sensible directions. E.g. write small bits of code and see how things work. But it's daft to ignore working code that others have freely posted.

John
Edited 2022-09-27 09:09 by JohnS
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1584
Posted: 11:44pm 26 Sep 2022
Copy link to clipboard 
Print this post

  stanleyella said  but do not from manual know how, arg1,arg2

Stan, can I please suggest that you read that section "Subroutines and Functions" again and experiment with some examples?

Another good source of information is Geoff's "Getting Started with the Micromite" from https://geoffg.net/micromite.html. Yes, it's for the Micromite but is a good description of using MMBasic.

Usually subroutines are used to perform tasks which are done several times in a program. Passing parameters (arguments) to them allows the subroutine to perform the same operation on different values.

Try this:

option default integer
x = 1
y = 2
print x, y
swap x, y  'swap the numbers
print x, y
swap x, y  'swap them back
print x, y
end
SUB Swap a, b
LOCAL t
t = a
a = b
b = t
END SUB


The variables a, b and t are created for use by the subroutine and the variables x and y are passed by reference to the subroutine.

If I read your program correctly, your 'subroutine' is only called from one place in the program and uses GLOBAL variables. If that is correct, it may as well be included in line in the main body of the program.

(Talk about the blind leading the blind   )

Bill
Keep safe. Live long and prosper.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3641
Posted: 06:32am 27 Sep 2022
Copy link to clipboard 
Print this post

  Turbo46 said  
  stanleyella said  but do not from manual know how, arg1,arg2

Stan, can I please suggest that you read that section "Subroutines and Functions" again and experiment with some examples?

Another good source of information is Geoff's "Getting Started with the Micromite" from https://geoffg.net/micromite.html.

Also the many examples Geoff has on his web site or has posted on this site, and of course Fruit of the Shed.

Many languages use subroutines / functions in a very similar manner to MMBasic. It's hard to imagine someone who's used other languages hasn't already discovered that.

John
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3422
Posted: 07:12am 27 Sep 2022
Copy link to clipboard 
Print this post

  TassyJim said  I am sorry, I have no idea what your SUB is trying to do but given the original problem, I would use a FUNCTION rather than a SUB.
FUNCTIONs are best if you have one value to return.

  Quote  '
  
OPTION EXPLICIT
  
OPTION DEFAULT INTEGER
  
  
DIM temp AS FLOAT
  
DIM Vin AS FLOAT
  
  
FOR temp = 0 TO 100 STEP 10
    
PRINT scale(0,100,32,212,temp)
  
NEXT temp
  
PRINT
  
FOR Vin = 0 TO 3.31 STEP 0.1
    
PRINT STR$(Vin,2,1), INT(scale(0,3.3,0,270,Vin))
  
NEXT Vin
  
FUNCTION scale(fromLow!, fromHigh!, toLow!, toHigh!, in!) AS FLOAT
  scale = (toHigh!-toLow!)/(fromHigh!-fromLow!)*in!+toLow!-fromLow!
END FUNCTION 




With a function, if a variable is declared in the parameters (argumentss) or as LOCAL in the function, the variable is treated as local and doesn't need to be DIMmed as a global.
That makes code easier to manage.

Jim


Well Jim, that is almost exactly the MAP() function from Arduino. Just move the
"in!" parameter as first argument and you are done.....

MAP() in MMBasic

FUNCTION map(in!, fromLow!, fromHigh!, toLow!, toHigh!) AS FLOAT
  scale = (toHigh!-toLow!)/(fromHigh!-fromLow!)*in!+toLow!-fromLow!
END FUNCTION

Edited 2022-09-27 17:14 by Volhout
PicomiteVGA PETSCII ROBOTS
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5867
Posted: 07:44am 27 Sep 2022
Copy link to clipboard 
Print this post

  Volhout said  
Well Jim, that is almost exactly the MAP() function from Arduino. Just move the
"in!" parameter as first argument and you are done.....

MAP() in MMBasic

FUNCTION map(in!, fromLow!, fromHigh!, toLow!, toHigh!) AS FLOAT
  map = (toHigh!-toLow!)/(fromHigh!-fromLow!)*in!+toLow!-fromLow!
END FUNCTION


I left it in stan's order.

A slight correction. map = , not scale =

Jim

edit: MAP is used on the CMM2 for colour mapping so should be avoided.
Edited 2022-09-27 17:50 by TassyJim
VK7JH
MMedit   MMBasic Help
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 03:26pm 27 Sep 2022
Copy link to clipboard 
Print this post

This is in demos

Map() Function
Print
Modified on 2022/07/09 12:10 by CaptainBoing
Categorized as Uncategorized
» AFTER, EVERY, AT, REMAIN Flexible State Machine Timers using Semaphores. (MMBasic) » BLITDEMO.BAS (MMBasic) » Map() Function (MMBasic)
Function maps a value in a given linear input range to it's counterpart in the linear output range.

Often it is required to take a value in a specific range and "Map" it onto the equivalent value in some alternative range. e.g. a current measuring module might return a value in the range of 0-255 which corresponds to a current reading of 0-6Amps. The Map() function will take the measures value and the input/output ranges and return the equivalent value.

The input value is constrained so that it conforms to the input range, i.e. if the lower limit is zero and a value of negative 5 is supplied, the value of zero will be used. Likewise the upper limit.

Written to use Floats due to the likely nature of the application.

Syntax:
=Map(Value,InputRangeLowerLimit,InputRangeUpperLimit,OutputRangeLowerLimit,OutputRangeUpperLimit)

Example:
mAmps=Map(n,0,255,0,6000)'current in mA

in the above, 0 to 255 is the measured value from the current sensor, 0 to 6000 is the equivalent current reading in mA

Function Map(x As Float,rLo As Float,rHi As Float,oLo As Float,oHi As Float) As Float
Map=((Min(Max(x,rLo),rHi))-rLo)*((oHi-oLo)/(rHi-rLo))+oLo
End Function
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 5648
Posted: 03:42pm 27 Sep 2022
Copy link to clipboard 
Print this post

But it probably wasn't written for the CMM2, Stan. :)

Be aware of the capabilities of your target machine.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 05:10pm 27 Sep 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  But it probably wasn't written for the CMM2, Stan. :)

Be aware of the capabilities of your target machine.

It seems to works fine.

OPTION EXPLICIT
dim vdeg!
vdeg=Map(3.3,0,3.3,0,270)
cls
text 0,0,str$(vdeg)
do
loop


Function Map(x As Float,rLo As Float,rHi As Float,oLo As Float,oHi As Float) As Float
Map=((Min(Max(x,rLo),rHi))-rLo)*((oHi-oLo)/(rHi-rLo))+oLo
End Function
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 05:42pm 27 Sep 2022
Copy link to clipboard 
Print this post

  phil99 said  You are over thinking it.
to re-scale a (0 to 3.3) variable to (0 to 270) just multiply it by 270 and divide by 3.3

Var270 = Var3.3 * 270 / 3.3

That's all there is to it.
Don't use integers, they just mess up the division.
The Pico does not have the limitations of an 8 bit chip, it's very capable, let it do the work.

It only gets a little more complicated if the start of the ranges isn't zero.

If you wanted to scale (0.3 to 3.3) to (30 to 270)

Var270 = (Var3.3 - 0.3) * (270 - 30) / (3.3 - 0.3) + 30

Late my reply but thanks. good idea. stan
Would integers be ok for integers?
Edited 2022-09-28 03:48 by stanleyella
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 2989
Posted: 06:17pm 27 Sep 2022
Copy link to clipboard 
Print this post

  stanleyella said  Would integers be ok for integers?


Would 3 divided by 2 times 2 yielding 2 be good enough for you? What's the fear of floats? If you're doing division, floats are your friend.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 06:24pm 27 Sep 2022
Copy link to clipboard 
Print this post

  JohnS said  
  stanleyella said  
  JohnS said  
  stanleyella said  I am trying to pass parameters to the sub. eg scale 0,3.3,0,270,vartoscale
but do not from manual know how, arg1,arg2

If you can't handle the example(s) in the manual, maybe refer to the many examples which have been posted.

John

IMHO I should not need the many examples as the manual should explain it all

That is a ridiculous statement. The manual already does what almost anyone needs.

No manual can ever do what you appear to want for any even moderately powerful language.

If you can't handle that fact, you would be wiser to pursue another interest.

Otherwise, put more effort in and in more sensible directions. E.g. write small bits of code and see how things work. But it's daft to ignore working code that others have freely posted.

John

"The manual already does what almost anyone needs
No manual can ever do what you appear to want for any even moderately powerful language.
If you can't handle that fact, you would be wiser to pursue another interest."

Try it from a new users experience  
The sub explanation and using local vars and their explanation that they disappear when sub exits.
The example map function in demos works and is simple to understand.. well for me.
Can a sub be like a function as per passing parameters?

vdeg=Map(3.3,0,3.3,0,270)
Function Map(x As Float,rLo As Float,rHi As Float,oLo As Float,oHi As Float) As Float
Map=((Min(Max(x,rLo),rHi))-rLo)*((oHi-oLo)/(rHi-rLo))+oLo
End Function

If it was map(a,b,c,d,e) as sub map how to implement. Are sub arg1.arg2 the same as a function?
It is silly to try code that one does not know how it works and get meaningless error messages.
If you want new users you got to have empathy. Learning a new system takes a while and some users use previous experience, not start at page 1, which is my point that the manual is organised in a way one has to jump from the info at the start to info near the end. I do not think I will pursue yet another interest.
Is there a new use like me that finds the manual could be clearer?
Edited 2022-09-28 05:14 by stanleyella
 
pwillard
Senior Member

Joined: 07/06/2022
Location: United States
Posts: 272
Posted: 06:53pm 27 Sep 2022
Copy link to clipboard 
Print this post

Subs are like Functions but you don't get a return value.

  Quote  Functions are similar to subroutines with the main difference being that the function is used to return a value in an expression


A SUB will just act on what it is given or that it can gather based on what you have it doing.

Here is what I do... I printed the PICOMITE manual... and I keep a copy of it on my nightstand and then read it before I fall asleep.
Edited 2022-09-28 04:57 by pwillard
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1567
Posted: 07:42pm 27 Sep 2022
Copy link to clipboard 
Print this post

  pwillard said  Subs are like Functions but you don't get a return value.

  Quote  Functions are similar to subroutines with the main difference being that the function is used to return a value in an expression


A SUB will just act on what it is given or that it can gather based on what you have it doing.

Here is what I do... I printed the PICOMITE manual... and I keep a copy of it on my nightstand and then read it before I fall asleep.


I watch tv and have the manual pdf on a monitor next to me and mix the both for all evening. I copy and collate parts that are of special interest and test them on a breadboard.
It was nice guy on this forum that pointed out using read data that you do not use data just read. I just used the manual but misinterpreted the info was getting nowhere.
 
     Page 1 of 2    
Print this page
© JAQ Software 2024