Getting 32u4 timers right

I am adapting Blinking Eyes by Brad Blumenthal from a 2013 Make article to an Adafruit Feather 32u4 (Leonardo compatible). I have my LEDs working ok for regular PWM on pins 9 and 10 so I know the circuit is ok. It’s just getting the timers right to get the outputs firing like on an 8 MHz ATtiny85 on the faster 32u4. With my scope I’m not getting any output on the timer 1 A/B pins (9 and 10).

Datasheet: 32u4 datasheet Atmel

Hopefully a timer familiar person will see issue(s)

Here is the code:

/* 
Name: Blinking Eyes  - based on code by Brad Blumenthal, MAKE Magazine  
License: GPLv3

Modified for Feather 32u4 (Leonardo) October 2016 Mike Barela
*/
 
#define SENSITIVITY 550        // photocell sensitivity (changeable)
#define CELL_PIN     A0        // CdS Photocell voltage divider on Pin A0
#define EYE1          9        // Timer output pin OC1A Arduino Pin 9
#define EYE2         10        // Timer output pin OC1B Arduino Pin 10
                               
uint8_t eyes_open;
volatile uint8_t  blink_count; 
volatile uint8_t  blink_flag;
volatile uint8_t  tick_flag;
volatile uint8_t  getting_brighter = 0;
const uint8_t    min_bright=16;
const uint8_t    max_bright=128;
volatile uint8_t brightness;
uint8_t          lfsr;                    // Linear Feedback Shift Register 
const uint8_t    min_blink = 64u;         // don't blink more than once every 3 secs or so
const uint16_t   Int_Timer_Ticks = 1000;  // quarter second (8 hz)
 
void setup() {
  PRR1 |= 1<<PRUSB; // turn off USB interrupts 32u4 only, press reset to load new sketch!
  pinMode(EYE1, OUTPUT); pinMode(EYE2, OUTPUT);                   // Eyes set as output
  pinMode(CELL_PIN, INPUT);                                       // Photocell as input
  analogWrite(EYE1, max_bright);  analogWrite(EYE2, max_bright);  // Light eyes

  eyes_open = 1;
  blink_flag = 0;
  init_timer0_PWM();
  lfsr = random(100);                                       // initialize "blinking"
  blink_count = (lfsr & 0x3F) | min_blink;
  lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xF0u);              // pseudorandom blinking
  
  // Timer1 set to CK/256  8 Hz at 16 MHz clock rate for blinking action
  TCCR1A = 0;
  TCCR1B = (1 << WGM12) | (1 << CS12) | (1 << CS10); // 1024
  TCCR1C = 0;
//  OCR1A  = Int_Timer_Ticks;
  TIMSK1 = (1 << TOIE1); //(1 << OCIE1A);  
  sei();
}
 
void loop() {
    uint16_t photocell;
/*    photocell = analogRead(CELL_PIN);  // value can be 0-1023 (space dark to laser bright)
    if(photocell > SENSITIVITY) {  // if too light, shut down eyes until it gets darker on photocell
       tick_flag=0;
       analogWrite(EYE1,0);  analogWrite(EYE2,0);  // Turn off eyes if too light out
      } */
    if (tick_flag) {  // if too bright or we've counted enough ticks (clocks for blink)
      tick_flag = 0;
      if (blink_flag) {
        blink_flag = 0;
        if (eyes_open) {
          eyes_open = 0;
          analogWrite(EYE1,0); analogWrite(EYE2,0);  // Turn off eyes by stopping PWM
          blink_count = (lfsr & 0x01) + 1;           //  off for 1-2 ticks
          }
        else {
          eyes_open = 1;
          analogWrite(EYE1,brightness); analogWrite(EYE2,brightness);   // Turn eyes on 
          blink_count = max(blink_count, min_blink);
          lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xF0u);  // regenerate pseudorandom blink
          }
      }
      else { // One "tick,"  but we didn't blink...  work on brightness control
        if (getting_brighter) {
          brightness += 2;  // increase brightness
          analogWrite(EYE1, brightness);  analogWrite(EYE2, brightness);
          if (brightness >= max_bright) getting_brighter = 0;
        } else {
          brightness -= 2;  // decrease brightness
          analogWrite(EYE1, brightness); analogWrite(EYE2, brightness);
          if (brightness <= min_bright) getting_brighter = 1;
        }
      }
    }
}
 
ISR (TIMER1_OVF_vect) {           // Every 64 times a second, check blink
  noInterrupts();
  tick_flag = 1;
  blink_count--;
  if (!blink_count) {
    blink_flag = 1;
  }
  interrupts();
}

void init_timer0_PWM(void) {
//  DDRB |= (1<<DDB0) | (1<<DDB1); // Data direction even for PWM
  // Setting up the Timer / Counter Prescalar and Clock Source (TTCR0A/B)
  TCCR0A |= (1<<WGM01) | (1<<WGM00); // Fast PWM
  TCCR0A |= (1<<COM0A1) | (1<<COM0B1); // Clear on match; set at BOTTOM
  TCCR0B |= (1<<CS00); // No pre-scaler 
  
  brightness = max_bright;
  OCR0A = brightness; // OCR0A/B are the registers that control PWM on 
  OCR0B = brightness; // pins OC0A/B
  getting_brighter = 0;
}

Thanks for a look

I think there are several problems with your sketch.

First, Timer1 is not set up for 8Hz. Here’s how I would set the timer up and the ISR.

// Timer1 set 8 Hz at 16 MHz clock rate for blinking action
// Mode4 CTC to OCR1A  with prescaler 64 -> 4us per tick

TCCR1A = 0;//clear all default presets
TCCR1B = 0;
TCCR1C = 0;

TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); 

OCR1A  = 31250; //31250*.000004 = 125ms
TIMSK1 |= (1 << OCIE1A);//enable output compare interrupt


ISR (TIMER1_COMPA_vect)
{
  tick_flag = 1;
  blink_count--;
  if (!blink_count) {
    blink_flag = 1;
  }
}

With my scope I’m not getting any output on the timer 1 A/B pins (9 and 10).

The sketch is set up for pwm output on Timer 0 A and B pins which in the Leonardo are pins 11 and 3.

Try those pins for your pwm output. Declare them as outputs.