Pin Interrupt - ATtiny85

Hello, I have created code to program an ATtiny85 to control a Neopixel with the help of a switch. Currently when the switch is clicked once the LED switches on and when clicked again it turns off. When the switch is clicked twice in quick succession, a “doubleclick”, it changes the lighting mode changing between RainbowCycle from Neopixel and static red, green and blue.

Currently once RainbowCycle has started the LED can’t be turned off until the animation has finished to change this I understand that an interrupt can be used but I can’t, despite my own attempts get one to work.

My working code is:

#include "OneButton.h"                              //Allows single and double clicks
#include <Adafruit_NeoPixel.h>                      //Controls the LED

#define PIXEL_PIN 0                                 //Connect LED to Pin 0
#define PIXEL_COUNT 1

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

OneButton button(2, true);                         //Switch is connected to Pin 2

int LightMode;                                     // Value corresponds to which light effect shown
int SwitchMode;                                    // Value corresponds to if light is on or off

void setup() {

  button.attachDoubleClick(doubleclick);            // link the function to be called on a doubleclick event.
  button.attachClick(singleclick);                  // link the function to be called on a singleclick event.

  strip.begin();                                    // Initialise the LED
  strip.show();


}

void loop() {
  button.tick();                                    // check the status of the button

  delay(10);                                        // a short wait between checking the button
} // loop

void singleclick() {                                // what happens when the button is clicked
  SwitchMode = SwitchMode + 1;
  if (SwitchMode > 1) {
    SwitchMode = 0;
  }

  switch (SwitchMode) {
    case 0: strip.setPixelColor(0, 0, 0, 0);
      strip.show();                                 // Black/off
      break;
    case 1: LightModeLoop();
      break;
  }
}

void doubleclick() {                                // what happens when button is double-clicked
  LightMode = LightMode + 1;
  if ( LightMode > 3 ) {
    LightMode = 0;
  }
}

void LightModeLoop() {
  switch (LightMode) {
    case 0: rainbowCycle(20);
      break;
    case 1: strip.setPixelColor(0, 255, 0, 0);
      strip.show();   // Red
      break;
    case 2: strip.setPixelColor(0, 0, 255, 0);
      strip.show();     // Green
      break;
    case 3:  strip.setPixelColor(0, 0, 0, 255);
      strip.show();     // Blue
      break;

  }
}

//////////RAINBOW CYCLE CODE
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
    WheelPos -= 170;
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

I followed this example, attiny - ATTiny85 Interrupt id vs pin when programming with Arduino - Arduino Stack Exchange , but couldn’t seem to adapt it to work with mine.

Also from my understanding if using an interrupt it allows you to put the chip to sleep which can conserve battery life, any guidance on this is also greatly appreciated. Thank you!

I hope this makes sense and any help in getting this to work is greatly appreciated, Thank you!

You need to break up your animation code into simple steps and ensure the loop keep spinning and test the button press in the loop.

If you want to stick to the interrupt, then you need to test the flag you set in the interrupt during the for loops of your animation to break out of the current mode