Go Down

Topic: Need help porting a demo program from basic stamp (Read 4 times) previous topic - next topic

Jona

Hey JJ, pretty sweet sketch. Thanks for posting it.

I have successfully modified your sketch to do what I was looking for, display the digits from 0 to 1000 and back to 0 again but I have a few questions for you:

1. My digits only display on the first 3 LEDs (from left to right) instead of accross all 4. Where is this controlled in your sketch?

2. Can you explain what the function numOut is doing exactly?

3. What is the deal with the variable numStr?

4. Why exactly do we need stlib?

Again, thanks for all of the help on this!

JJ

So in order:

1. When the digits convert from a number to a char* array (a string), it will convert the number literaly to a string from left to right. When we iterate through the first four characters in the array, the first one is still on the far left hand side, and then we get to an unrecognized member of the array (the fantom fourth digit) which is likely failing in the program but fortunately does so in a way that still works. You could write a test in numOut for numbers less than 1000 to have a space written as the first digit.

When we send the character to digitOut, the digit is now not an int but a char, and so the letter 0 has a different value than the number 0. To convert the value of the char back to the int value, we subtract 48.

If anyone knows a simpler way to convert an int to an array witht the values of each digit in decimal as a number in the array, I'd love to hear it. I'm sure there are some recursive checks we could do to determine the value of each digit, but I thought this would be simpler. I don't really want to check each value, or divide each by 10, 100, 1000. This seems cleaner, but I'm sure there is a graceful mathematical solution.

The other thing that numOut does is provide the correct timing on the clock pulse for the digitOut function. On the fourth digit, the clock pulse is considerably longer. The long clock pulse causes the IC on the HC4LED board to display what is in memory. If we use the long pulse on each character, it will display each to the screen immediately and push the digit from right to left with each new character, giving a horible flicker at high speed, almost unreadable. The timing is something it took me a very long time to perfect, as it is hard to tell if the program is actually working correctly with bad timing (and the IC is fussy).

The variable numStr allocates enough space to contain the contents of our conversion from int to char. Because the function itoa() accepts a pointer as the target for the conversion, we are writing directly to RAM. Previously I allocated four characters, which I thought would be exactly enough, but it wasn't. When the program ran, other areas in memory would be overwritten and this caused the program to produce very strange output and eventually to crash. Therefore, I allocate more than enough memory (8 chars).

stlib provides the function itoa() which is integer to ascii, and then we access the ascii string as an array.



Jona

Okay, thanks for the explanations.

With that all being said (and read) I am still having trouble getting the output that I would like to see. For example I can't seem to get the display to show this "0101", or even this "0001".

Another question I have is related to the values that I am sending out to the display. In my current sketch, I am simply sending out the current value of the for loop control variable as it increments up to 1000. This is easy, but it doesn't show me how to add a value to another value and then send that to the display.

In my application, I am essentially counting the number of times a push button is pressed and displaying the appropriate number on the display. So if the display is currently at "1" and the button is pressed once, I need to update the display with a "2".

I am guessing I need to keep the existing value around in a variable so that I might use it for addition.

I know your input is coming from a thermistor, but I am wondering if you might have any insight into how my sketch might need to be written for my application?

Go Up