Blink with PWM - handmade


In case you want to dim the built-in LED connected to Pin-13 you have to do the PWM yourself. You don't even have to use a timer. Play with the constants as you like.

/*
  Blink-11
  PWM - handmade
 */

const int STEP = 150;
const int MAX = 75 * STEP;
int dt = STEP;
int inc = STEP;
const byte mask = B00100000;

void setup() {
  // initialize digital pin 13 as an output.
  DDRB = mask;
}

void loop() {
  PINB = mask;
  delayMicroseconds(2+MAX-dt);       
  PINB = mask;
  delayMicroseconds(2+dt);             
  dt = dt + inc;
  if (dt >= MAX) inc = -STEP;
  if (dt <= 0) inc = STEP;
}



contact: nji(at)gmx.de