Gemma Neopixel Christmas card

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
cokebottles
 
Posts: 10
Joined: Wed Nov 20, 2013 4:53 pm

Gemma Neopixel Christmas card

Post by cokebottles »

Hey guys,

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)
Image

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);
  }
}
Right now this does nothing and I cant figure out why. In other iterations Ive had it stuck in the 'rainbow' loop and using the switch made no difference. Im struggling and Im not sure if Im using the interrupt correctly or where to go next with regards to figuring this one out.

User avatar
michaelmeissner
 
Posts: 1822
Joined: Wed Aug 29, 2012 12:40 am

Re: Gemma Neopixel Christmas card

Post by michaelmeissner »

IIRC, only pins 0/1 can be attached for external interrupts on the ATtiny85 processors like the Gemma. Pin 1 might be problematical since it is hooked up to the builtin LED (which has a resistor on it). I would try putting the neopixels on pin 1 and your switch on pin 0.

Inside of loop did you forget the () to call the tree function? The way C interprets tree; is load up the address of the tree function. And then because nothing uses the address, just throw it away, and the optimizer will remove the code.

I don't see a delay in the code other than a delay (25). That is likely much faster than people can see.

<edit>
I suspect you probably shouldn't use interrupts at this stage of the game, rather check the button state at every stage of the loop.

cokebottles
 
Posts: 10
Joined: Wed Nov 20, 2013 4:53 pm

Re: Gemma Neopixel Christmas card

Post by cokebottles »

Thanks for the advice, it was all really helpful.

I kept it simple and did the following:

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN 1

Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);

int button = 0;
int state = 0;


void setup()
{
  strip.begin();
  strip.show(); 
  
  pinMode(button, INPUT);
  digitalWrite(button, LOW);
}



void loop()
{
  
  state = digitalRead(button);
  
  if (state == HIGH)
  {
    tree();
  }
  
  else
  {
    rainbow();
  }
  

}





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);
  }
}
It has prototyped nicely. I will now build the project - if its worth showing Ill post some pictures :)

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: Gemma Neopixel Christmas card

Post by adafruit_support_mike »

Oh yeah.. we love seeing pictures of stuff people have made! ;-)

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”