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.

Haris
  • 12,120
  • 6
  • 43
  • 70
user1175889
  • 141
  • 3
  • 4
  • 13
  • 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 '12 at 17:26
  • @MarkByers: that made my day :) – Constantinius Jul 17 '12 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? http://stackoverflow.com/questions/11512918/is-there-a-way-you-can-decrement-an-array-in-a-periodic-fashion-in-c – Wug Jul 17 '12 at 17:29
  • yes i understand what your saying cheers – user1175889 Jul 17 '12 at 17:31
  • Wug , no those answers didnt work, for the life of me i dont know why – user1175889 Jul 17 '12 at 17:33
  • There are probably more errors in your code. Maybe the other errors are in the code you haven't posted. – Mark Byers Jul 17 '12 at 17:34

3 Answers3

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.

jrad
  • 3,172
  • 3
  • 22
  • 24
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;
}
chepner
  • 497,756
  • 71
  • 530
  • 681
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).

David G
  • 5,408
  • 1
  • 23
  • 19