|
Forum Index : Microcontroller and PC projects : CONSTRAIN(X,A,B)
| Author | Message | ||||
redrok![]() Senior Member Joined: 15/09/2014 Location: United StatesPosts: 209 |
Hi geoffg; I would like to propose a new function: CONSTRAIN(number, m, n ) This is very useful when used with sensor and AtoD converter. I lifted this from the Arduino Language Reference: constrain(x, a, b) This seems to be an easy function to build and could be quite useful.Description Constrains a number to be within a range. Parameters x: the number to constrain, all data types a: the lower end of the range, all data types b: the upper end of the range, all data types Returns x: if x is between a and b a: if x is less than a b: if x is greater than b What do you think? redrok |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
I use MIN() and MAX() for this so if I am after a value in a range (say between 100 & 200) I use the following: x=Max(100,Min(200,reading)) this is the constrain function you are after. The reading will be limited to 100-200 and either be the reading or one of the boundaries: > ? max(100,min(200,300)) 200 > ? max(100,min(200,50)) 100 > ? max(100,min(200,177)) 177 > Does this help? |
||||
redrok![]() Senior Member Joined: 15/09/2014 Location: United StatesPosts: 209 |
Hi Captain;Yes it does. Combining max and min works very nicely. Here's a small snipit I'm using in my Nixie Clock Experiments to Increment or Decrement a timing value on the fly: If Q$="U" Then Dimm=Max(0,Min(7,Dimm+1)) Print"Dimm";Str$(Dimm,2,1);"mS" EndIf If Q$="D" Then Dimm=Max(0,Min(7,Dimm-1)) Print"Dimm";Str$(Dimm,2,1);"mS" EndIf redrok |
||||
Chopperp![]() Guru Joined: 03/01/2018 Location: AustraliaPosts: 1106 |
Interesting. The only documentation I could find on Min() & Max() was in the blurb on Defined functions unless I've missed something Works well for me as well. Thanks Captain ChopperP |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
MAX() and MIN() were introduced in Ver 5.04.01. My guess is that the manual that you are using is before that. Geoff Graham - http://geoffg.net |
||||
Chopperp![]() Guru Joined: 03/01/2018 Location: AustraliaPosts: 1106 |
Ah, found it in my recently printed edition. Thanks Geoff EDIT. Found my 4 hole paper punch. Manual now in a folder. Much better than loose leafed. ChopperP |
||||
| Phil23 Guru Joined: 27/03/2016 Location: AustraliaPosts: 1667 |
Needed a little brain exercise this morning... [Code]DOS MMBasic Ver 5.04.08 Copyright 2011-2017 Geoff Graham Constr(1,6,10) 6 Constr(8,6,10) 8 Constr(12,6,10) 10 >[/code] Phil. |
||||
| rave Newbie Joined: 24/02/2018 Location: United StatesPosts: 28 |
My 2c: I've been using the following Arduino-inspired functions and subs for my projects (as a MMBasic library), which includes a Constrain(x, lo, hi) function. Granted, these are written for MMBasic 4.5. But should not be too difficult to upgrade these to MMBasic 5.x. A few are simply renamed MMBasic functions. ' BASIC functions and subroutines similar to Arduino math/bit/char functions ' ' Functions ' --------- ' Sq(x) square of x ' Sqrt(x) square root of x ' Pow(x, y) raise x to the power y (same as x^y) ' Constrain(x, lo, hi) constrain x between lo and hi ' Map(x, lo, hi, newlo, newhi) map x between lo and hi to newlo and newhi ' Min(x, y) minimum of x and y ' Max(x, y) maximum of x and y ' Lsh(x, n) shift x left by n bits ' Rsh(x, n) shift x right by n bits ' Inv(x) invert bits (complement of x) ' Bit(n) value of n'th bit (same as 2^n) ' BitRead(x, n) n'th bit of x ' HighByte(x) high byte of word x ' LowByte(x) low byte of word x ' IsAlpha(a) true if a is alphabetic ' IsAlnum(a) true if a is alphanumeric ' IsCntrl(a) true if a is a control character ' IsDigit(a) true if a is a digit ' IsGraph(a) true if a is a graph character ' IsPrint(a) true if a is printable ' IsPunct(a) true if a is punctuation ' IsLower(a) true if a is lower case ' IsUpper(a) true if a is upper case ' IsSpace(a) true if a is white space ' ' Subroutines ' ----------- ' BitSet(x, n) set n'th bit of x ' BitClear(x, n) clear n'th bit of x ' BitToggle(x, n) toggle n'th bit of x ' BitWrite(x, n, b) set n'th bit of x to b ' RandomSeed(x) randomize with seed value x ' returns square of x Function Sq(x) Sq = x * x End Function ' returns square root of x Function Sqrt(x) Sqrt = Sqr(x) End Function ' returns x to the power y Function Pow(x, y) Pow = x ^ y End Function ' returns value of x constrained between lo and hi Function Constrain(x, lo, hi) If x < lo Then Constrain = lo ElseIf x > hi Then Constrain = hi Else Constrain = x EndIf End Function ' returns value of x between lo and hi mapped to range newlo and newhi Function Map(x, lo, hi, newlo, newhi) Map = (x - lo) * (newhi - newlo) / (hi - lo) + newlo End Function ' returns minimum value of x and y Function Min(x, y) If x < y Then Min = x Else Min = y End Function ' returns maximum value of x and y Function Max(x, y) If x > y Then Max = x Else Max = y End Function ' returns x shifted left by n bits Function Lsh(x, n) Lsh = x * 2 ^ n End Function ' returns x shifted right by n bits Function Rsh(x, n) Rsh = x \ 2 ^ n End Function ' returns the complement of x Function Inv(x) Inv = -1 - x End Function ' returns n'th bit value Function Bit(n) Bit = 2 ^ n End Function ' returns n'th bit of x Function BitRead(x, n) BitRead = x And 2 ^ n End Function ' set n'th bit of x Sub BitSet(x, n) x = x Or 2 ^ n End Sub ' clear n'th bit of x Sub BitClear(x, n) x = x And -1 - (2 ^ n) End Sub ' toggle n'th bit of x Sub BitToggle(x, n) x = x Xor 2 ^ n End Sub ' set n'th bit of x to b Sub BitWrite(x, n, b) If b Then x = x Or 2 ^ n Else x = x And -1 - 2 ^ n End Sub ' returns the high byte of a word Function HighByte(x) HighByte = x \ 256 And 255 End Function ' returns the low byte of a word Function LowByte(x) LowByte = x And 255 End Function ' returns random number between lo and exclusive hi Function Random(lo, hi) Random = lo + (hi - lo) * Rnd() End Function ' set seed of random number generator Sub RandomSeed(x) Randomize x End Sub ' returns true if a is alphabetic Function IsAlpha(a) IsAlpha = (a >= 65 And a <= 90) Or (a >= 97 And a <= 122) End Function ' returns true if a is alphanumeric Function IsAlnum(a) IsAlnum = (a >= 48 And a <= 57) Or (a >= 64 And a <= 90) Or (a >= 97 And a <= 122) End Function ' returns true if a is a control character Function IsCntrl(a) IsCntrl = (a >= 0 And a <= 31) Or a = 127 End Function ' returns true if a is a digit Function IsDigit(a) IsDigit = a >= 48 And a <= 57 End Function ' returns true if a is a graph character Function IsGraph(a) IsGraph = a >= 33 And a <= 126 End Function ' returns true if a is a printable character Function IsPrint(a) IsPrint = a >= 32 And a <= 126 End Function ' returns true if a is punctuation Function IsPunct(a) IsPunct = (a >= 33 And a <= 47) Or (a >= 58 And a <= 64) Or (a >= 91 And a <= 96) Or (a >= 123 And a <= 126) End Function ' returns true if a is lower case Function IsLower(a) IsLower = a >= 97 And a <= 122 End Function ' returns true if a is upper case Function IsUpper(a) IsUpper = a >= 65 And a <= 90 End Function ' returns true if a is white space Function IsSpace(a) IsSpace = (a >= 9 And a <= 13) Or a == 32 End Function - Rob |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |