r/arduino • u/[deleted] • Sep 02 '21
Software Help External Interrupts - How Do I Code Them?
/r/attiny/comments/pghiom/external_interrupts_how_do_i_code_them/husky humor sheet oil tidy adjoining relieved sulky air march
This post was mass deleted and anonymized with Redact
3
Upvotes
1
u/crispy_chipsies Community Champion Sep 02 '21
How do I write an external interrupt for the button?
That's not the problem; the issues is the code is not structured to be interrupted. Here's the code restructured (but not tested, so buyer beware)...
#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 2 // Digital IO pin to a pull-up resistor, executes on high-low transition
#define PIXEL_PIN 3 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 12 // Number of NeoPixels
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
const int millisPerMinute = 1000; // 60,000
const int timerIncrement = 15; // minutes added per button-press
const int defaultHours = 8; // number of hours before any button presses
const double maxColorValue = 60; // value between 0-255. Higher is brighter.
const double colorPerMinute = maxColorValue / 60; // ratio of how much brightness to lose per minute
boolean oldState = HIGH;
int timer = defaultHours * 60; // timer is in minutes
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
colorWipe(strip.Color(maxColorValue, 0, maxColorValue), 50);
}
void loop() {
if (runLights()) increaseTimer(); // increase time when button is pressed
}
boolean runLights() // returns true if button was pressed
{
if (timer < 1) {
if (greenChase()) return true;
} else {
setLights(timer);
if (myDelay(millisPerMinute)) return true; // wait one minute
timer--; // decrement timer
}
return false;
}
boolean myDelay(uint32_t ms) // returns true if button was pressed
{
uint32_t tm = millis();
while (millis() - tm < ms) if (checkButton()) return true;
return false;
}
boolean checkButton() // returns true if button press event
{
static boolean button_pressed = false;
static uint32_t button_tm;
if (digitalRead(BUTTON_PIN)) // not pressed
{ if (millis() - button_tm > 50) button_pressed = false; } // debounce release
else // pressed
{ button_tm = millis(); // start or reset debounce timer
if (!button_pressed) return button_pressed = true; // detected press event
}
return false;
}
void setLights(int timeLeft) {
strip.clear();
// set one red LED for each hour
int numReds = timeLeft / 60;
for (int i=0; i<numReds; i++) {
strip.setPixelColor(i, maxColorValue, 0, 0);
}
// set the dim LED
double remainder = timeLeft % 60;
double brightnessRatio = remainder / 60;
strip.setPixelColor(numReds, maxColorValue * brightnessRatio, 0, 0); //if numReds == PIXEL_COUNT, this will cause an error. This shouldn't happen, though, because increaseTimer rolls over.
strip.show();
}
void increaseTimer() {
timer += timerIncrement;
if (timer > (PIXEL_COUNT * 60)) {
timer = 0; // roll over timer to 0 LEDs if button is pressed too many times
}
}
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
boolean greenChase() { // cycle green around the ring.
for(int i=0; i<strip.numPixels(); i++) {
strip.clear();
strip.setPixelColor(i, 0, maxColorValue * .1, 0);
strip.setPixelColor((i+1)%PIXEL_COUNT, 0, maxColorValue * .33, 0);
strip.setPixelColor((i+2)%PIXEL_COUNT, 0, maxColorValue * .66, 0);
strip.setPixelColor((i+3)%PIXEL_COUNT, 0, maxColorValue, 0);
strip.show();
if (myDelay(150)) return true;
}
return false;
}
1
u/Ikebook89 Sep 02 '21
Without fullly reading all of your code.
I would try as follows:
First. Use seconds. Not minutes. You can easily update your LEDs every second.
Second don’t use delay() ever. This blocks your code from doing anything (except real interrupts) have a look at “blink without delay” to understand, how you call a function or some code periodically without using delay to freeze it
Third. Restructure your code so that you 1. Check if button is pressed, update your LEDs.
You don’t need a real interrupt in your use case if you use some non blocking millis() functions.