Im trying to build a very basic project and my (lack of) programming expertise is letting me down.
First off I have botched together a mini 5 pixel strand in the (very rough) shape of a heart shown below (pixel ID numbered)
Im using a Gemma as my micro controller. When installed in the card this will be powered via the adafruit 2x coin cell battery holder, but for prototyping this is being powered from usb.
The neopixels are connected to D0.
The premise of the card is that the pixels will be embedded behind the page which you usually write your message on. There will be cutouts to make the pixels visible. There will also be holes in the front of the card so that the pixels are still visible when the card is closed.
I basically want to install a rudimentary switch in the card which senses if the card is open or closed (im thinking some wire and tin foil contacts). For prototyping I have a 10K pull down connected between D2 and GND and im using a jumper wire between D2 and 3V3 as my switch. The idea is that this allows me to change what the pixels do if the card is open or closed.
Closed [void tree()]: The card will have a christmas tree on the front - I want to colour pixels 1,2,4 solid green, and 0,3 solid yellow. nothing fancy.
Open [void rainbow()]: The card will have a heart inside, and I will have the pixels pulsing red/pink.
I think I have the two functions behaving as they should - my problem is how to implement the switch. The pulsing of the heart in during the open state is achieved by an infinite for loop - so I thought Id need an interrupt. Ive tried some code but I dont think Im getting anywhere. Can anyone help me??
Code: Select all
#include <Adafruit_NeoPixel.h>
#define PIN 0
// Parameter 1 = number of pixels in strip
// Parameter 2 = 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);
int button = 2;
void setup() {
strip.begin();
strip.show();
pinMode(button, INPUT);
digitalWrite(button, HIGH);
attachInterrupt(button, rainbow, LOW);
}
void loop() {
tree;
}
void tree() {
strip.setPixelColor(0, 50, 50, 0);
strip.setPixelColor(1, 0, 100, 0);
strip.setPixelColor(2, 0, 100, 0);
strip.setPixelColor(3, 50, 50, 0);
strip.setPixelColor(4, 0, 100, 0);
strip.show();
}
void rainbow() {
uint16_t i, j;
for(j=65; j<115; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(25);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}