Separating a variable
TC
Posts: 1,019
Hello all,
Well the max7219 project is coming along, but I need to find a way to take a variable and split it into its individual digits. I know how to do it on the BS2, but I have never even tried it with the prop. Here is what I mean:
Counter = 12345
digit0 = 1
digit1 = 2
digit2 = 3
digit3 = 4
digit4 = 5
Here is the kicker, I don't want someone just to post a code and say here you go. How can I learn from that? I just want to know where should I look to figure it out. And any advice that would help.
Thanks
TC
Well the max7219 project is coming along, but I need to find a way to take a variable and split it into its individual digits. I know how to do it on the BS2, but I have never even tried it with the prop. Here is what I mean:
Counter = 12345
digit0 = 1
digit1 = 2
digit2 = 3
digit3 = 4
digit4 = 5
Here is the kicker, I don't want someone just to post a code and say here you go. How can I learn from that? I just want to know where should I look to figure it out. And any advice that would help.
Thanks
TC
Comments
-Phil
Ok, so you are saying:
counter = 12345
digit0 := counter//10 = 1
digit1 := counter//100 = 2
digit2 := counter//1000 = 3
digit3 := counter//10000 = 4
digit4 := counter//100000 = 5
Cool, I will get back to you when I figure it out. Thank You
let me know if that is still where I should look. Because I cant figure out how to do what I need to do, yet.
Your parents didn't tell you how to walk, they showed you! Programming can be same. Take the bits given to you and expand on them.
I thought of something like that right as the queen(wife) decided she wanted to go out and get something to eat. Once I get home I will try my idea.
I agree with you, but we all fell down, cried, then got back up and tried again...... then pooped...
I had to throw that in there. I wanted to be shown just the tools, it works for me to figure out how to use those tools.
Note that this routine will extract a 5-digit value to a byte array -- not directly to the MAX7219. Why? For flexibility. I would make a MAX7219 method which allows you to point at an array of bytes. That way you can point to DAT tables or calculated arrays (like we are using here). Again, the idea is flexibility. Let's say you had a byte array called digitsbuf; in your program you might have this:
Note that the extract() method looks for upper and lower boundary values -- no need to run the loop if a value is on or past a boundary. In the loop you see the correct order of things: extract a digit with division; update the value (which removes the digit) with modulus; then update the divisor before running again. This order is critical.
Have fun!
I have so much more to do, like being able to control the DP, and to be able to control LEDs also digi-key.
I do like your way though, because the LEDs are going to have to do a pattern depending on a value. And it would be easy (I think) to make the pattern and just point to where it is at. I also like that the digits goes from 0 to 4, not like mine where I had to go backwards.
This only extracts the value so you don't have to worry about the size of the display you're using -- a separate method call moves the buffer to the display.
Well, so far your object is working great for displaying numbers.
I like to keep things very flexible. Here's are the methods from the object I'm working on that handle the extraction, setting/clearing decimal point, and writing a buffer to any part of the the display. You may wonder why bothering with internal and external buffers. Well, this lets you setup multiple buffers that you can switch back and forth very quickly using the write_buf() method. And it's setup to be very flexible, too, allowing you to write a buffer to any (contiguous) portion of the display.
I've attached the full source of the object I'm working on. As I have no hardware setup at the moment, perhaps you could run it for me.
08-DEC-2013 : Corrected error in code identified in post #16 (fix in #17).
I would be honored to.
everything works, but
I had to do the same thing to clear_dpoint
I made a demo for it, and everything worked out great. If you are happy with it, please add it to the OBEX.
Thanks for all your help
Video of the demo