Jiggly Jiggly - 10 days ago 5
C Question

Loop through pins on a Microcontroller in C where the Duty Cycle changes every hour

I have a Real time clock that is keeping time, and I want to change the 10 PWM outputs every hour by an increment. The idea is to stagger the PWM outputs, meaning pin 1 will be 0%, pin 2 is 25% and pin 10 is 100%. I'm trying to loop through the pins on the micro and change the duty cycle so that after one hour, pin 1 = 100% and pin 2 = 0% and pin 3 = 25% and pin 4 = 35%.

I seem to keep messing up the logic so that pins 1-10 all end up with the same duty cycle, or pin 1-10 end up with incremented duty cycles but cannot get the every hour to increment by some value. Any help would be fantastic :)

The code I'm using,

int dutyPWM[10] = {0,26,52,78,104,130,156,182,208,214};
if(Hour > 1+hour)
{
for(int pinnumber = 1; pinnumber < 11; pinnumber++)
{
for(int n=0, n <= 10; n++)
{
PWM_Write(pinnumber, n);
}
}
}

Answer

There is a global variable currentIdxStart and I'm using a RTC clock to keep trac of time.

int sizeOfArr = 10;
  int dutyPWM[sizeOfArr] = {0, 26, 52, 78, 104, 130, 156, 182, 208, 214}; 
  unsigned long nowtime = 0;
  nowtime = get_time();

if (nowtime >= num_unixtime+3600) 
 {
//setpwmfor10pumps(); 
int idx = currentIdxStart;
for (int i = 4; i <= 13; i++)
{
  analogWrite(i, dutyPWM[idx]);
  int duty = dutyPWM[idx];

  idx ++;
  if (idx >= sizeOfArr)
  {
    idx = 0;
  }
  Serial.print(i);
  Serial.print(':');
  Serial.print(duty);
  Serial.println(" ");
}

currentIdxStart++;
if (currentIdxStart >= 10)
{
  currentIdxStart = 0;
}
Serial.println(currentIdxStart); 
num_unixtime = get_time(); 
Serial.print(num_unixtime); 
Serial.println('\n');
}

Hope that helps anyone in the future, thanks.

Comments