The following code:

struct l{
    uint8_t pin;
    uint8_t sensePin;
    float brightnessTarget;
    uint8_t PWMvalue;
    uint8_t inc;
typedef struct l led;

led* redLed;
led* whiteLed;
unsigned long secondsOfDay = 79140;
unsigned long minutesOfDay = 0;
unsigned int hoursOfDay = 0;
uint8_t interrupted=0;
uint8_t runPID;
int pin13=0;

void setup() {
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = OCR1A_val;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS10 and CS12 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);  
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);

void loop() {
  if (runPID){
    pin13 = (~pin13 & 1);
    digitalWrite(13, pin13);

SIGNAL(TIMER1_COMPA_vect){ //1 second timer fire
  runPID = 1;

Toggles a pin. However, if I add:

redLed->pin = 3;
whiteLed->pin = 4;  
redLed->sensePin = 0;
whiteLed->sensePin = 1;

In the setup anywhere (before cli(), after sei(), or within the block), the interrupt no longer toggles.

I'm absolutely baffled, and I have no idea what's going on.

Edit: everything but whiteLed->sensePin is fine. That line is what is causing the issue (and I still don't understand)

1 Answer 1



led* redLed;

defines a global variable implicitly initialized to zero (i.e. a NULL pointer). This is an invalid pointer you are not allowed to use until you properly initialize it.


redLed->pin = 3;

you are writing to the memory pointed to by this invalid pointer. This invokes what the C and C++ standards call undefined behavior. What that means is that, according to the definition of the C++ language, the behavior of your program is not defined. In other words, anything can happen. Typically you get a program crash, but even that behavior is not guaranteed.

Do not ever access memory through an invalid pointer.

  • Ugh. I knew it was a dumb oversight. Thank you! Commented Oct 31, 2019 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.