Hey just wondering if anyone out there could help point me in the direction

Hey just wondering if anyone out there could help point me in the direction of a solid tutorial on using a button interrupt on an arduino or attiny85 that I could use to adjust an entire strips brightness level… ?

Any help would be awesome I have been stuck on trying to find the right direction with oracle google but I have failed and am getting lost…!

This is how I trigger brightness on my POVpoi unit. It’s using an ATmega32U4, but the same principle applies to a Tiny85 (which has two interrupts versus the 32U4’s five.) First the interrupt is set in the setup() portion, I’m using interrupt 4, and the button is configured as an active-high:

void setup() {
attachInterrupt(4, intHandler, RISING);
// other setup stuff
}

Then, the interrupt handler itself which in my case I named ‘intHandler’ - you can call it whatever you want as long as the function also has the same name:

void intHandler() {
// Interrupt for brightness button
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// deBounce the button!
if (interrupt_time - last_interrupt_time > 200) {
brightness();
last_interrupt_time = interrupt_time;
}
}

Note the button debounce! This is important, otherwise you will register false triggers very easily. I’m doing a 200ms debounce which works great.

Note that I’m calling a function called ‘brightness’. This is because an interrupt handler needs to be as short and quick as possible. It can not have (or should not have) any large code and certainly no delay() in it. So if you take away all the debounce lines, all you’re left with is the single call to the brightness() function.

The brightness() function itself looks like this:

void brightness() {
brightnessLevel++;
if (brightnessLevel > 6) brightnessLevel = 1;
switch (brightnessLevel) {
case 1: LEDS.setBrightness(64); break; // 25% - default
case 2: LEDS.setBrightness(128); break; // 50%
case 3: LEDS.setBrightness(255); break; // 100%
case 4: LEDS.setBrightness(8); break; // 3%
case 5: LEDS.setBrightness(16); break; // 6%
case 6: LEDS.setBrightness(32); break; // 12%
}
}

So by default, I start at 25% (this is set in the main setup(0 section) and subsequent clicks will increase towards 100% then loop back to the lowest setting. I keep a single variable, brightnessLevel, that the case statement then checks. Every time the brightness() function is called, it increases the variable. When it reaches max (6), it loops back to 1 which is the lowest setting.

The advantage of doing it this way is that I don’t have to constantly be calling the brightness nor interrupt handler while the main loop() is running. It only gets called whenever the button is pressed and the interrupt triggered.

Hopefully this helps.

thank you, that helps me grasp it. I have been very inspired with your POVpoi work that i’ve been watching for a while now. so awesome

Thanks. Glad the code will help. Let me know if you get stuck.

I have read a few times on here that the FastLED library disables interrupts and I have been meaning to ask about that as they will be very important for me. So I assume as you have it working @Ashley_M_Kirchner_No that is not the case?

Depends on which driver you are using. I’m using LPD8806s and it works wonderfully.

@Mark_Kriegsman , I think it was possibly you that mentioned interrupts being dissabled, is Ashley correct and it’s dependent on the type of LEDs/drivers being used?

Does it really make a difference to the time taken by the interrupt handler if you break brightness() out into a separate function? It’s all called from the same interrupt handler.

It doesn’t @Robert_Atkins , except that you will constantly be calling some kind of function then to check for the button press and then call brightness(). It’s an unnecessary overhead. Note, unnecessary does not mean it’s wrong. You can do it any way you want, with or without interrupts. It’s the overhead that I look at. With an interrupt, nothing gets called till the interrupt is triggered.