Questions about writing code for Gemma

Wearable electronics: boards, conductive materials, and projects from Adafruit!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Questions about writing code for Gemma

Post by Crystal »

Hi!

I feel a little awkward asking about code here, but I'm not sure where else to go to for help. I originally asked a few freelance Arduino programmers and a friend if they could help me, but they all balked because they'd never worked with a Gemma or NeoPixels before. :(

I decided to tackle the code myself, even though I've never programmed anything before; and after reading through the tutorials and project codes for Flora, Gemma, and the NeoPixels, I copied and pasted some code together and then found some code for a push button switch and tossed that in there too.

My project involves 5 NeoPixels attached in a straight line to a Gemma, and pressing the push button would cause the LEDs to light up in different patterns (e.g., all solid white, all solid red, all solid orange, and then multicolor with each LED as a different color). The code compiled and I uploaded it to the Gemma with the push button and a single NeoPixel attached with alligator clips ... and the only thing that happened was that the LED turned on as the first color pattern in my code.

Because of that, I'm guessing that the defective part of my programming involves the code for the push button. Then again, it wouldn't surprise me if I goofed up the code for the NeoPixels as well. Would someone mind taking a look at my code and tell me what's going on with it? Thank you so very much! :D

Code: Select all

#include <Adafruit_NeoPixel.h>

    int buttonPin = 0;    // momentary push button on pin 0
    int oldButtonVal = 0;
    
    #define PIN 1         // 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 nPatterns = 8;
    int lightPattern = 1;

// the setup routine runs once when you press reset:
    void setup() {
    strip.begin();
    strip.show();                // initialize all pixels to 'off'    
// initialize the BUTTON pin as an input
    pinMode(buttonPin, INPUT);
    digitalWrite(buttonPin, HIGH);  // button pin is HIGH, so it drops to 0 if pressed
}


// Pattern 1 - White light, all LEDs in the strip are white
    void pattern1() {
        strip.setPixelColor(0, strip.Color(255, 255, 255)); // White
        strip.setPixelColor(1, strip.Color(255, 255, 255));
        strip.setPixelColor(2, strip.Color(255, 255, 255));
        strip.setPixelColor(3, strip.Color(255, 255, 255));
        strip.setPixelColor(4, strip.Color(255, 255, 255));
    strip.show();
    }

// 7 more color patterns go here, but I won't post them so that the code stays streamlined

// the loop routine runs over and over again forever;
void loop() {
  // read that state of the pushbutton value;
  int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern == lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal;
  
  switch(lightPattern) {
    case 1:
      pattern1();
      break;
    case 2:
      pattern2();
      break;
    case 3:
      pattern3();
      break;
    case 4:
      pattern4();
      break;
    case 5:
      pattern5();
      break;
    case 6:
      pattern6();
      break;
    case 7:
      pattern7();
      break;
    case 8:
      pattern8();
      break;
  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Questions about writing code for Gemma

Post by adafruit_support_bill »

That code won't compile. You call pattern1 through pattern8, but you only have pattern1 defined.

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

Hi Bill,

I left out patterns 2 through 8 to try to keep the code streamlined since I read that I should only post the minimal code to show what's not working. Perhaps I left out too much, though! Whoops! Here's the whole code.

Code: Select all

#include <Adafruit_NeoPixel.h>

    int buttonPin = 0;    // momentary push button on pin 0
    int oldButtonVal = 0;
    
    #define PIN 1         // 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 nPatterns = 8;
    int lightPattern = 1;

// the setup routine runs once when you press reset:
    void setup() {
    strip.begin();
    strip.show();                // initialize all pixels to 'off'    
// initialize the BUTTON pin as an input
    pinMode(buttonPin, INPUT);
    digitalWrite(buttonPin, HIGH);  // button pin is HIGH, so it drops to 0 if pressed
}


// Pattern 1 - White light, all LEDs in the strip are white
    void pattern1() {
        strip.setPixelColor(0, strip.Color(255, 255, 255)); // White
        strip.setPixelColor(1, strip.Color(255, 255, 255));
        strip.setPixelColor(2, strip.Color(255, 255, 255));
        strip.setPixelColor(3, strip.Color(255, 255, 255));
        strip.setPixelColor(4, strip.Color(255, 255, 255));
    strip.show();
    }
    
// Pattern 2 - Red light, all LEDs in the strip are red
    void pattern2() {
        strip.setPixelColor(0, strip.Color(255, 0, 0)); // Red
        strip.setPixelColor(1, strip.Color(255, 0, 0));
        strip.setPixelColor(2, strip.Color(255, 0, 0));
        strip.setPixelColor(3, strip.Color(255, 0, 0));
        strip.setPixelColor(4, strip.Color(255, 0, 0));
    strip.show();
    }
    
// Pattern 3 - Orange light, all LEDs in the strip are orange
    void pattern3() {
        strip.setPixelColor(0, strip.Color(255, 128, 0)); // Orange
        strip.setPixelColor(1, strip.Color(255, 128, 0));
        strip.setPixelColor(2, strip.Color(255, 128, 0));
        strip.setPixelColor(3, strip.Color(255, 128, 0));
        strip.setPixelColor(4, strip.Color(255, 128, 0));
    strip.show();
    }

// Pattern 4 - Yellow light, all LEDs in the strip are yellow
    void pattern4() {
        strip.setPixelColor(0, strip.Color(255, 255, 0)); // Yellow
        strip.setPixelColor(1, strip.Color(255, 255, 0));
        strip.setPixelColor(2, strip.Color(255, 255, 0));
        strip.setPixelColor(3, strip.Color(255, 255, 0));
        strip.setPixelColor(4, strip.Color(255, 255, 0));
    strip.show();
    }
    
// Pattern 5 - Green light, all LEDs in the strip are green
    void pattern5() {
        strip.setPixelColor(0, strip.Color(0, 255, 0)); // Green
        strip.setPixelColor(1, strip.Color(0, 255, 0));
        strip.setPixelColor(2, strip.Color(0, 255, 0));
        strip.setPixelColor(3, strip.Color(0, 255, 0));
        strip.setPixelColor(4, strip.Color(0, 255, 0));
    strip.show();
    }

// Pattern 6 - Blue light, all LEDs in the strip are blue
    void pattern6() {
        strip.setPixelColor(0, strip.Color(0, 0, 255)); // Blue
        strip.setPixelColor(1, strip.Color(0, 0, 255));
        strip.setPixelColor(2, strip.Color(0, 0, 255));
        strip.setPixelColor(3, strip.Color(0, 0, 255));
        strip.setPixelColor(4, strip.Color(0, 0, 255));
    strip.show();
    }
    
// Pattern 7 - Violet light, all LEDs in the strip are violet
    void pattern7() {
        strip.setPixelColor(0, strip.Color(127, 0, 255)); // Violet
        strip.setPixelColor(1, strip.Color(127, 0, 255));
        strip.setPixelColor(2, strip.Color(127, 0, 255));
        strip.setPixelColor(3, strip.Color(127, 0, 255));
        strip.setPixelColor(4, strip.Color(127, 0, 255));
    strip.show();
    }

// Pattern 8 - Rainbow light, all LEDs in the strip are different colors
    void pattern8() {
        strip.setPixelColor(0, strip.Color(255, 0, 255)); // Red
        strip.setPixelColor(1, strip.Color(255, 255, 0)); // Yellow
        strip.setPixelColor(2, strip.Color(0, 255, 0)); // Green
        strip.setPixelColor(3, strip.Color(0, 0, 255)); // Blue
        strip.setPixelColor(4, strip.Color(127, 0, 255)); // Violet
    strip.show();
    }


// the loop routine runs over and over again forever;
void loop() {
  // read that state of the pushbutton value;
  int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern == lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal;
  
  switch(lightPattern) {
    case 1:
      pattern1();
      break;
    case 2:
      pattern2();
      break;
    case 3:
      pattern3();
      break;
    case 4:
      pattern4();
      break;
    case 5:
      pattern5();
      break;
    case 6:
      pattern6();
      break;
    case 7:
      pattern7();
      break;
    case 8:
      pattern8();
      break;
  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Questions about writing code for Gemma

Post by adafruit_support_bill »

Even if the button were not working, you should at least see pattern1. Can you post a photo showing how you have everything connected?

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

I do see pattern 1. I haven't attached all 5 NeoPixels together yet, and everything is held together by alligator clips so far.
unsewn layout
unsewn layout
IMG_1377.JPG (217.33 KiB) Viewed 788 times

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

Here is what I've been working with so far.
Gemma and NeoPixel
Gemma and NeoPixel
IMG_1378.JPG (212.51 KiB) Viewed 788 times

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

Here's another photo.
closeup of pads
closeup of pads
IMG_1380.JPG (242.95 KiB) Viewed 788 times

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Questions about writing code for Gemma

Post by adafruit_support_bill »

That looks right. What happens if you bypass the switch and connect the jumper directly from D0 to GND?

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

Nothing changes. The LED remains white.
jumper from D0 to GND
jumper from D0 to GND
IMG_1381.JPG (206.42 KiB) Viewed 773 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Questions about writing code for Gemma

Post by adafruit_support_rick »

Try changing the button initialization from this:

Code: Select all

// initialize the BUTTON pin as an input
    pinMode(buttonPin, INPUT);
    digitalWrite(buttonPin, HIGH);  // button pin is HIGH, so it drops to 0 if pressed
to this

Code: Select all

// initialize the BUTTON pin as an input
    pinMode(buttonPin, INPUT_PULLUP);  // button pin is HIGH, so it drops to 0 if pressed
The way you have it now works for an arduino, but it might not work for a gemma.

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

Sorry, Rick, even with that new bit of code, I still can't get the NeoPixel to change color.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Questions about writing code for Gemma

Post by adafruit_support_bill »

Very strange. I'm wondering if pullups work on the Gemma the same as an Arduino. I haven't got a Gemma here yet, but try this test. Bypass the switch again, but this time alternate shorting D0 to 3.3v and to GND.
.

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

I'm not sure if I did it correctly, but if I have one jumper running from D0 to GND and then a second jumper running from D0 to 3.3v, the Gemma doesn't power on (as in the PWR LED doesn't turn on and the connected NeoPixel won't turn on either).

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Questions about writing code for Gemma

Post by adafruit_support_rick »

No - take the jumper that's connected to D0, and alternate touching the other end to 3.3V and to GND. That should make your patterns advance

Crystal
 
Posts: 11
Joined: Mon Sep 30, 2013 2:51 pm

Re: Questions about writing code for Gemma

Post by Crystal »

Still nothing. I took the jumper from D0 and touched the opposite end to GND and then touched it to 3.3v, and nothing happened. I then tried touching it to 3.3v first and then to GND, and still nothing.

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

Return to “Wearables”