Digistump Forums

The Digispark => Digispark (Original) Support => Topic started by: Dupl3xxx on January 19, 2014, 06:13:30 pm

Title: [solved]Problem with code. TinySoftPWM shoud work, isn't.
Post by: Dupl3xxx on January 19, 2014, 06:13:30 pm
I'm trying to create a "rainbow" effect with all 6 pins on the digispark. I want the led's to become brighter, then darker. The code should work, but isn't. Anyone see the misstake I made?

This is the effect I'm trying to achive:
(http://i.imgur.com/19b1gFg.gif)

code:
Code: [Select]
#include <TinySoftPwm.h>

int value[] = {
  0,43,86,129,152,193};
boolean rising[] = {
  true,true,true,true,true,true};

void setup()
{
  TinySoftPwm_begin(128, 0); /* 128 x TinySoftPwm_process() calls before overlap (Frequency tuning), 0 = PWM init for all declared pins */
}

void loop()
{
  static uint32_t StartUs=micros();
  static uint32_t StartMs=millis();
  static uint8_t Pwm=0;
  static int8_t  Dir=1;

  /***********************************************************/
  /* Call TinySoftPwm_process() with a period of 60 us       */
  /* The PWM frequency = 128 x 60 # 7.7 ms -> F # 130Hz      */
  /* 128 is the first argument passed to TinySoftPwm_begin() */
  /***********************************************************/
  if((micros() - StartUs) >= 60)
  {
    /* We arrived here every 60 microseconds */
    StartUs=micros();
    TinySoftPwm_process(); /* This function shall be called periodically (like here, based on micros(), or in a timer ISR) */
  }

  /*************************************************************/
  /* Increment/decrement PWM on LED Pin with a period of 10 ms */
  /*************************************************************/
  if((millis()-StartMs) >= 10)
  {
    /* We arrived here every 10 milliseconds */
    StartMs=millis();

    /* for each of the 6 pins*/
    for (int i = 0; i < 6; i++)
    {
      if(rising[i])
      {
        value[i]++;
        if(value[i] == 256)
        {
          rising[i] = false;
          value[i] = 254;
        }
      }
      else
      {
        value[i]--;
        if(value[i] == -1)
        {
          rising[i] = true;
          value[i] = 1;
        }
      }
      TinySoftPwm_analogWrite(value[i], i);
    }
  }
}

Thanks DeuxVis for the code tag :)

Quote
TinySoftPwm_analogWrite(PIN, VALUE) -> TinySoftPwm_analogWrite(VALUE, PIN)
Title: Re: Problem with code. TinySoftPWM shoud work, isn't.
Post by: defragster on January 20, 2014, 02:53:53 am
Code: [Select]
if(rising) might work better as if(rising[ i ])
And that 'i' might help in other array spots too.

Title: Re: Problem with code. TinySoftPWM shoud work, isn't.
Post by: DeuxVis on January 20, 2014, 03:03:14 am
You probably wanted to write

Code: [Select]
rising[i]
anywhere there is only

Code: [Select]
rising
otherwise only the first element of that rising array will ever be used.



Sorry for duplicating your comment defragster, but without the
Code: [Select]
[code] tag it is difficult to read :)
Title: Re: Problem with code. TinySoftPWM shoud work, isn't.
Post by: defragster on January 20, 2014, 10:10:01 am
Code tag helps - was there a real problem that is fixed - or was the code just getting hosed on POST?
Title: Re: Problem with code. TinySoftPWM shoud work, isn't.
Post by: Dupl3xxx on January 20, 2014, 12:50:13 pm
Found the problem:
TinySoftPwm_analogWrite(PIN, VALUE)
and not
TinySoftPwm_analogWrite(VALUE, PIN)

Swithed those around, and now it works! :D