Interrupt Code with Animation Frames and Keypad Input

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 9

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.
volatile int frame=0;
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'A','0','B'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 8, 7, 6, 5 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 4, 3, 2 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
    //set pins as outputs
  pinMode(13, OUTPUT);
  // Fill the dots one after the other with a color


cli();//stop interrupts
//set timer1 interrupt at 1Hz
   TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = 977;// = (16*10^6) / (8*1024) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS10 and CS12 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);  
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);


sei();//allow interrupts

}//end setup

ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
  if(frame==1){ 
    strip.setPixelColor(0,60,0,0);
    strip.show(); 
    frame++;
    }
  else if(frame==2) {  
    strip.setPixelColor(1,60,0,0);
    strip.show(); 
    frame++;
    }
  else if(frame==3) {  
    strip.setPixelColor(2,60,0,0);
    strip.show(); 
    frame++;
    }
  else if(frame==4) {  
    strip.setPixelColor(3,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==5) {  
    strip.setPixelColor(4,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==6) {  
    strip.setPixelColor(5,60,0,0);
    delay(50);
    strip.show(); 
    frame++;
    }
      else if(frame==7) {  
    strip.setPixelColor(6,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==8) {  
    strip.setPixelColor(7,60,0,0);
    delay(50);
    strip.show(); 
    frame++;
    }
      else if(frame==9) {  
    strip.setPixelColor(8,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==10) {  
    strip.setPixelColor(9,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==11) {  
    strip.setPixelColor(10,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==12) {  
    strip.setPixelColor(11,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==13) {  
    strip.setPixelColor(12,60,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==14) {  
    strip.setPixelColor(13,60,0,0);
    strip.show(); 
    frame++;
    }
          else if(frame==15) {  
    strip.setPixelColor(14,60,0,0);
    strip.show(); 
    frame++;
    }
          else if(frame==16) {  
    strip.setPixelColor(15,60,0,0);
    strip.show(); 
    }
      else if(frame==17){ 
    strip.setPixelColor(15,0,0,0);
    strip.show(); 
    frame++;
    }
  else if(frame==18) {  
    strip.setPixelColor(14,0,0,0);
    strip.show(); 
    frame++;
    }
  else if(frame==19) {  
    strip.setPixelColor(13,0,0,0);
    strip.show(); 
    frame++;
    }
  else if(frame==20) {  
    strip.setPixelColor(12,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==21) {  
    strip.setPixelColor(11,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==22) {  
    strip.setPixelColor(10,0,0,0);
    delay(50);
    strip.show(); 
    frame++;
    }
      else if(frame==23) {  
    strip.setPixelColor(9,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==24) {  
    strip.setPixelColor(8,0,0,0);
    delay(50);
    strip.show(); 
    frame++;
    }
      else if(frame==25) {  
    strip.setPixelColor(7,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==26) {  
    strip.setPixelColor(6,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==27) {  
    strip.setPixelColor(5,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==28) {  
    strip.setPixelColor(4,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==29) {  
    strip.setPixelColor(3,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==30) {  
    strip.setPixelColor(2,0,0,0);
    strip.show(); 
    frame++;
    }
      else if(frame==31) {  
    strip.setPixelColor(1,0,0,0);
    strip.show(); 
    frame++;
    }
          else if(frame==32) {  
    strip.setPixelColor(0,0,0,0);
    strip.show(); 
    } else if(frame==33) {
      strip.setPixelColor(random(16),random(0,25),random(0,25),random(0,25));
      strip.setPixelColor(random(16),0,0,0,0);
      strip.setPixelColor(random(16),0,0,0,0);
      delay(30);
     strip.show();
    }
          else if(frame==34){frame=0;}
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '1':
        frame = 1;
        break;
      case '2':
        frame = 17;
        break;
              case '3':
        frame = 33;
        break;
      default:
        break;
    }
  }
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

No Comments

Post a Comment