Skip to content

Instantly share code, notes, and snippets.

@Threeethan
Created February 2, 2014 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Threeethan/8772775 to your computer and use it in GitHub Desktop.
Save Threeethan/8772775 to your computer and use it in GitHub Desktop.
BlinkyTape Interrupt Example
#include <FastSPI_LED2.h>
#define DEBUG
#define LED_COUNT 60 // BlinkyTape has 60 LEDs!
struct CRGB leds[LED_COUNT]; // this struct contains 60 CRGB values. This is where
#define LED_MAX_BRIGHTNESS 55
#define PIN_BUTTON 10
#define PIN_IO_A 7
#define PIN_IO_B 11
#define PIN_SIGNAL 13
#define PIN_INPUT 10
const int debounceTime = 20; // debounce in milliseconds
volatile boolean button_pressed; // set when button pressed from withing the ISR for the main loop to action on
byte light_status; //to toggle the led colors when button is pressed, need to remember previous status
inline void deBounce () //try inline
{
unsigned long now = millis ();
do
{
// on bounce, reset time-out
if ((PINB & (1<<PINB6)) == LOW)
now = millis ();
}
while ((PINB & (1<<PINB6)) == LOW ||
(millis () - now) <= debounceTime);
} // end of deBounce
void wait (unsigned int wait_time) // wait time in milli seconds
{
unsigned int time_end=millis()+wait_time;
unsigned int time_now=0;
while (time_end>millis() ){
}
}
void buttonDown ()
{
if( digitalRead(PIN_BUTTON) == LOW)
{
button_pressed = true;
}
} // end of buttonDown
ISR(PCINT0_vect) // PCINT0 ISR
{
// byte saveSREG = SREG; // Save SREG
// noInterrupts(); // disable interrupts shouldn't be necessary as we use PCINT0_vect with
// no attribute = ISR_BLOCK. other options requiring this are ISR_NOBLOCK, ISR_NAKED
if ((PINB & (1<<PINB6)) == LOW){
button_pressed=true; // set flag to indicate BUTTON Pushed
}
// SREG = saveSREG; // restore SREG
// interrupts(); //not necessary with this ISR call
}
void setup()
{
// Serial.begin(9600);
Serial.begin(115200);
#ifdef DEBUG
Serial.println("Entering setup()" );
#endif
LEDS.addLeds<WS2811, PIN_SIGNAL, GRB>(leds, LED_COUNT); // this configures the BlinkyBoard - leave as is.
LEDS.showColor(CRGB(0, 0, 0)); // set the color for the strip all at once.
LEDS.setBrightness(50); // start out with LEDs at 50
#ifdef DEBUG
wait (2000);
LEDS.showColor(CRGB(100, 100, 100));
light_status=true;
wait (2000);
#endif
#ifdef DEBUG
Serial.println("Setting up button interruption)" );
#endif
// works very well tested ok
// Setup for test using low level registers
EIFR = 4; // cancel any existing falling interrupt 4 to avoid entering immediately into the interrupt once attached
DDRB &= ~(1 << DDB6); // Clear the PB6 pin
// PB6 is now an input
PORTB |= (1 << PORTB6); // turn On the Pull-up
// PB6 is now an input with pull-up enabled
//PCICR = PCIE0 = Bit0,
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT6); // set PCINT0 to trigger an interrupt on state change of D10/PCINT6
// End setup for test using low level registers
/* uncomment this section if you want to test with standard arduino functions and comment out the previous low lever register section
// Setup for test using "standard" arduino functions, not as responsive as the low level register coding
EIFR=4; // cancel any existing falling interrupt 4 to avoid entering immediately into the interrupt once attached
pinMode(PIN_BUTTON, INPUT_PULLUP);
attachInterrupt (4, buttonDown, FALLING); // ready for button press. is it interrupt 4 for change pin status, interested in the press
// End Setup for test using "standard" arduinio functions
*/
#ifdef DEBUG
Serial.println("End button interruption setup" );
#endif
#ifdef DEBUG
Serial.println ("End Setup");
#endif
}
uint8_t button_state;
uint8_t last_button_state = 0;
uint8_t color_b = 0;
void loop() {
// Test using the button digitalread in the main loops works well
/*
#ifdef DEBUG
Serial.print ("in Loop. "); Serial.print ("Checking if the button is pressed in the loop");
#endif
#ifdef DEBUG
Serial.println ("Reading PIN_BUTTON (not interrupt)");
#endif
button_state = digitalRead(PIN_BUTTON);
if((button_state != last_button_state) && (button_state == LOW)) {
#ifdef DEBUG
Serial.print("PIN_BUTTON pressed button_state = ");Serial.print(button_state);Serial.println(" calling Debounce()");
#endif
deBounce ();
#ifdef DEBUG
Serial.print("Turning on the lights 1/2 blue, color_b = ");Serial.println (color_b);
#endif
LEDS.showColor(CRGB(0, 0, color_b));
color_b +=50;
}
last_button_state = button_state;
*/
#ifdef DEBUG
Serial.print ("in Loop. Testing interrupt "); Serial.print ("button_pressed = "); Serial.println (button_pressed);
#endif
if (button_pressed)
{
#ifdef DEBUG
Serial.println("Button pressed, calling deBounce()");
#endif
deBounce ();
button_pressed = false;
#ifdef DEBUG
Serial.print("light_status =");Serial.println(light_status);
#endif
if (light_status) {
#ifdef DEBUG
Serial.print("light_status =");Serial.print(light_status);Serial.println(" turning on the lights red, light_status = false");
#endif
LEDS.showColor(CRGB(255, 0, 0));
light_status=false;
} else {
#ifdef DEBUG
Serial.print("light_status =");Serial.print(light_status);Serial.println(" turning on the lights green, light_status = true");
#endif
LEDS.showColor(CRGB(0, 255, 0));
light_status=true;
}
}
wait (750);
// testing something additional
Serial.println("calling ASM NOP");
__asm__("nop\n\t");
Serial.println("returned from ASM NOP");
/* #ifdef DEBUG
Serial.println ("Bottom of Loop. after Testing interrupt code ");
#endif
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment