![]() |
Forum Index : Microcontroller and PC projects : separating a number
Author | Message | ||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
Trying to do a countdown timer in which I have to separate the individual digits from thousands, hundreds, tens and ones (counting from 1 to 9999). This method works, but is there a better way that anyone has come across??? Z = 0 FOR Z = 0 TO 9999 THOU = INT(Z/1000) HUND = INT((Z-(THOU * 1000))/100) TENS = INT((Z-((THOU * 1000) + (HUND * 100)))/10) ONES = Z - ((THOU * 1000) + (HUND * 100) + (TENS * 10)) 'DO SOME STUFF NEXT Z I hope I won't be too embarrassed with the answers. Thanks again!!! |
||||
BobD![]() Guru ![]() Joined: 07/12/2011 Location: AustraliaPosts: 935 |
Jim MMBasic has an integer divide implemented by a backslash \ Careful use of it could save a bit of time and code. THOU = Z\1000 etc. apart from that I have no suggestions Bob |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 723 |
Nice thing, Could not find any documentation on ebay?! |
||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
EBAY??? |
||||
Gizmo![]() Admin Group ![]() Joined: 05/06/2004 Location: AustraliaPosts: 5111 |
Convert it to a string and use mid$ ? The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3269 |
MOD would also come in handy, for example: ONES = Z MOD 10 Geoff Graham - http://geoffg.net |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6220 |
This a string version. I am not sure which method will be the fastest or most memory conservative. DO
INPUT x xstring$=RIGHT$("000"+STR$(x),4) ' this should work for all versions of MMBasic units=VAL(MID$(xstring$,4,1)) tens=VAL(MID$(xstring$,3,1)) hundreds=VAL(MID$(xstring$,2,1)) thousands=VAL(MID$(xstring$,1,1)) PRINT thousands,hundreds,tens,units LOOP UNTIL x = -1 END Jim VK7JH MMedit |
||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
Quick time test using numbers and strings... test method courtesy of Peter G8JCF - this is running on a uMite 28 at 48mhz... L=TIMER FOR Z= 1 TO 9999 THOU = z\1000 HUND = ((Z-(THOU * 1000))\100) TENS = ((Z-((THOU * 1000) + (HUND * 100)))\10) ONES = Z - ((THOU * 1000) + (HUND * 100) + (TENS * 10)) NEXT Z PRINT "Method 1 "; TIMER-L L=TIMER FOR Z= 1 TO 9999 xstring$=RIGHT$("000"+STR$(x),4) ' this should work for all versions of MMBasic units=VAL(MID$(xstring$,4,1)) tens=VAL(MID$(xstring$,3,1)) hundreds=VAL(MID$(xstring$,2,1)) thousands=VAL(MID$(xstring$,1,1)) NEXT Z PRINT "Method 2 "; TIMER-L results... Method 1 7090 Method 2 11544 I'm going to have to try the MOD method next... |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6220 |
L=TIMER
FOR Z= 1 TO 9999 THOU = z\1000 HUND = ((Z-(THOU * 1000))\100) TENS = ((Z-((THOU * 1000) + (HUND * 100)))\10) ONES = Z - ((THOU * 1000) + (HUND * 100) + (TENS * 10)) 'PRINT thou,hund,tens,ones NEXT Z PRINT "Method 1 "; TIMER-L L=TIMER FOR Z= 1 TO 9999 xstring$=RIGHT$("000"+STR$(x),4) ' this should work for all versions of MMBasic units=VAL(MID$(xstring$,4,1)) tens=VAL(MID$(xstring$,3,1)) hundreds=VAL(MID$(xstring$,2,1)) thousands=VAL(MID$(xstring$,1,1)) ' NEXT Z PRINT "Method 2 "; TIMER-L L=TIMER FOR Z= 1 TO 9999 THOU = z\1000 HUND = (Z MOD 1000)\100 TENS = (Z MOD 100)\10 ONES = Z MOD 10 'PRINT thou,hund,tens,ones NEXT Z PRINT "Method 3 "; TIMER-L Results (uMite at 40MHz): [13:12:37]Method 1 8320 [13:12:50]Method 2 13376 [13:12:55]Method 3 5248 Using MOD is a big improvement! Jim VK7JH MMedit |
||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
Hell yeah TassyJim... Method 1 6978 Method 2 11271 Method 3 4456 uMite at 48mhz That absolutely rocks!!!!! This will allow for an easy 100th of a second accuracy on a timer with display update, etc... Very Cool. Here is my code for quick timer with 100th of a second display on adafruit i2c led display. DIM T(10) T(0)=&H3F:T(1)=&H06:T(2)=&H5B:T(3)=&H4F:T(4)=&H66 T(5)=&H6D:T(6)=&H7D:T(7)=&H07:T(8)=&H7F:T(9)=&H6F I2C OPEN 400,100 I2C WRITE &H70, 0, 1, &H21 I2C WRITE &H70, 0, 1, &H81 I2C WRITE &H70, 0, 1, &HEF Z=0 SETTICK 10, UPDATE, 1 TM=TIMER DO:LOOP UPDATE: Z = Z + 1 THOU = z\1000 HUND = (Z MOD 1000)\100 TENS = (Z MOD 100)\10 ONES = Z MOD 10 I2C WRITE &H70, 0, 2, &H00, T(THOU) I2C WRITE &H70, 0, 2, &H02, T(HUND) I2C WRITE &H70, 0, 2, &H06, T(TENS) I2C WRITE &H70, 0, 2, &H08, T(ONES) IF Z = 9999 THEN TM=TIMER-TM PRINT "Z = ";Z;" TIMER = ";TM TM=TIMER Z=0 ENDIF IRETURN |
||||
halldave![]() Senior Member ![]() Joined: 04/05/2014 Location: AustraliaPosts: 121 |
would this code work for MAX7219 8-Bit Red LED Tube Display Module?? regards David |
||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
Hi David, The max7219 has an spi interface, which the uMite can do. I am experimenting with the 8 digit tube display and the 8x8 matrix that uses that chip and will report back soon (i hope) with code for those. Forum member Boss has written code for the tube display for Maximite here. A few changes are required for uMite as it has dedicated pins for spi interface and Maximite can use just about any pins for that. Still playing and will keep you posted. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |