0

i'm writing a code for a number on a led segement to decrease by 1 every five seconds

my actual code at the moment is this

FiveSecDelay+=1;
if (FiveSecDelay ==100)
{
    count2--; //decrement count2

    for (uint32_t x = 0; x < 4; x++) //split count to to individual digits
    {
        new_value[x] = count2 % 10;
        count2 = count2 / 10;
    }

    for (uint32_t i = 0; i < 4; i++)
    {
        Segment_Write(Digit[i],new_value[i]); assign  value to segments
    }
    FiveSecDelay =0;
}

I m using a scheduler to call a function every millisecond, in theory this supposed to work as i used the same technique to assign a value to the segments, what happens is that i have a starting value of 8, and it supposed to got 7,6,5,4 and so on till 0, but for some reason it goes from 8 to 42 and stays there

I had tried to fix it but have come up short.

6
  • 1
    "for some reason it goes from 8 to 42" It's trying to tell you the answer to the ultimate question. Now all you need to do is write a program that can tell you the question.
    – Mark Byers
    Jul 17, 2012 at 17:26
  • @MarkByers: that made my day :) Jul 17, 2012 at 17:28
  • Didn't you ask a question very much like this one yesterday EDIT: You did. Is there some reason those answers don't work? stackoverflow.com/questions/11512918/…
    – Wug
    Jul 17, 2012 at 17:29
  • yes i understand what your saying cheers Jul 17, 2012 at 17:31
  • Wug , no those answers didnt work, for the life of me i dont know why Jul 17, 2012 at 17:33

3 Answers 3

0

You alter the value of count2 in this line:

count2 = count2 / 10;

And I get the impression from what you've described that count2 is what is supposed to be going from 8 to 7 to ... to 0.

0
0

You aren't just decrementing count2 every 5 seconds, you're dividing it by 10 repeatedly as well. You'll want a temporary variable:

int temp_count = count2;   // Or whatever type count2 is
for (unit32_t x = 0; x < 4; x++)
{
    new_value[x] = temp_count % 10;
    temp_count /= 10;
}
0
0

I would rewrite the main loop like this:

for(uint32_t div10=1,i=0;i<4;i++,div10*=10)
   Segment_Write(Digit[i],(count2%(10*div10))/div10);

Only one loop and only one auxiliary other than the loop counter (div10).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.