Running out of flash memory on the Attiny2313

Hi All,

Currently I am working on a project using the Attiny2313. The problem I am facing is that the sketch I wrote uses too much flash. The attiny has 2KB of flash for programming code.

A piece of code I have questions about:

void setSignal(int co2) {
  int pwmSpeed = 0;
  switch (range) {
    case low:
      pwmSpeed = calcSpeed(co2,600,1000);
      break;
    case mid:
      pwmSpeed = calcSpeed(co2,800,1200);
      break;
    case high:
      pwmSpeed = calcSpeed(co2,1000,1400);
      break;
  }
  analogWrite( sig_out, pwmSpeed);
}

The total size of my sketch is 104% of the 2KB flash memory, so I cant upload the sketch. When I remove the analogWrite, the program fits inside the 2KB of flash memory (85% used). Apparently this line uses 20% of the flash memory. Strange thing in my opinion is that when I replace

analogWrite( sig_out, pwmSpeed);

with

analogWrite( sig_out, a value between 0 and 255);

the sketch uses 95% instead fo 104%. The calcSpeed() function returns an int so there is no conversion or anything when calling analogWrite();

Does anyone has a clue why the analogWrite uses different amounts of memory in this case?

Thanks in advance.

Hello,

janpaul1992:
Does anyone has a clue why the analogWrite uses different amounts of memory in this case?

The memory used by analogWrite does not change. What happens is the optimizer realizes you do not use pwmSpeed and eliminates all the associated code. In other words, setSignal reduces to this...

void setSignal(int co2) 
{
  analogWrite( sig_out, 231 );
}

Make all the functions except setup and loop static.

Make all the global data static.

Coding Badly, you are right. Thank you for the explanation.

You are welcome.

Please follow-up.