Recently I had some problems with my I2C brightness sensor on my clock. It messed up the main routine of smoothly drawing the clock at a rate of 30 updates per second. Analog was a solution to this, but it didn’t solve the main problem of the design. Interrupts where the way to go if I wanted to solve this properly.
So, what is an interrupt in this context? Well, there is a method by which a processor can execute its normal program while continuously monitoring for some kind of event, or interrupt. This event can be triggered by some sort of sensor, or input like a button, or even internally triggered by a timer counting to a particular number.
With an interrupt, the processor temporarily interrupts it’s normal routine to handle the interrupt. It will effectively save its execution state, run a small chunk of code (the interrupt handler or interrupt service routine) and then returns back to whatever it was doing before. How does it know what code to execute? This is set up in the program. The programmer defines where the processor should start executing code if a particular interrupt occurs.
Arduino’s tend to normally only support external interrupts, which is not something I wanted. What would solve all my problems is just being able to run whatever code I’d like (such as I2C readings and fetching internet data) while my clock rendering is guaranteed to run at specific times. After a while I finally managed to hookup an interrupt to a timer the Spark Core provides and after flashing it, it works like a charm! Need to change my power on and off routines a bit and the notifier routines, but this is a vast improvement over the mindless loop my code was doing before.