Trying to do a brightness control pattern using PWM

Hi I've been trying to control my LEDs brightness using PWM but not all my LEDs are connected to a PWM port. How do I do it? I'm using an arduino uno, and I have a button on PD3. I'm quite new to this so there might be some errors in my code below. (Edit: I can't use normal arduino IOs such as digitalWrite, analogWrite and etc)

void setupTimer2FastPWM(void) {
  TCCR2A = 0x23;
  TCCR2B = 0x04;
  DDRD = 0xF0;
  DDRB = 0x0F;
}
void brightness_control() {
  if ((PIND&0x08)==0)
  {
    if(OCR2B<255) {
      OCR2B-=5;
    } else {
      OCR2B+=5;
    }
  }
  Serial.print(OCR2B);
  Serial.print(" ");
  delay(200);
  if (cnt != 5)
    return;
}

You can do simple pwm using "bit banging" but if you expect to have different behavior on each pin you will not be able to use delay(), you'll need to use millis().
Look at the blink without delay example.

why not?

direct control of registers can make your code machine specific.

My project doesn't allow it.

So instead of wasting our time can we see the specification for the whole project?

2 Likes

How to blink an LED without digitalWrite()...