Author Topic: Newb question - RGB Example Problem  (Read 3003 times)

wgm4321

  • Newbie
  • *
  • Posts: 1
Newb question - RGB Example Problem
« on: February 24, 2013, 07:31:49 am »
Two very newbish questions.
If you reduce the delays and leave the RGB example run long enough, it seems to enter a strange state.
I think it's do to this code
Code: [Select]
  int i = 0;
  while(1)
  {
    fade(COLORS[i%3], dir);
    i++;
    dir = !dir;
  }
I think the variable i will eventually overflow going negative.   I changed it to this and it seems to work longer.
Code: [Select]
  int i = 0;
  while(1)
  {
    fade(COLORS[i], dir);
    if (++i > 2) i = 0;
    dir = !dir;
  }
Does this seem correct?
Second question:
Why would you do this:
Code: [Select]
void DigisparkRGBDelay(int ms) {
  while (ms) {
    _delay_ms(1);
    ms--;
  }
}
Instead of this:
Code: [Select]
void DigisparkRGBDelay(int ms) {
    _delay_ms(ms);
}
 
Is there some side effect like blocking interrupts or something?
Thanks.

BStrauss3

  • Newbie
  • *
  • Posts: 8
Re: Newb question - RGB Example Problem
« Reply #1 on: February 24, 2013, 08:18:58 am »
Sure - int is a 16-bit value, +32767 to -32768
So it will wrap to negative, your code fixes that (or you could use a long instead of an int)