0

Gemma M0 Neopixel Code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Gemma M0 Neopixel Code

by jeremyzschau on Fri Aug 23, 2019 11:52 pm

Is there code similar to this for gemma M0? The code here has 3 modes but code with just one mode would be fine.
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
 
#define PIN 1

int mode = 5; //keep track of which mode we're running in
long menuTime;
// Pushbutton setup
int buttonPin = 0;             // the number of the pushbutton pin
int buttonState;               // the current reading from the input pin
int lastButtonState = HIGH;    // the previous reading from the input pin
long buttonHoldTime = 0;         // the last time the output pin was toggled
long buttonHoldDelay = 1000;      // how long to hold the button down
int numberofmodes = 5;


Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_RGB + NEO_KHZ800);

//colors for each mode to be accessed by button presses
//mode 1                  R   G   B
uint8_t mode1Colors[][11] = {
                          {180, 0, 160},
                          {255, 51, 255},
                          {83, 54, 164},
                          {153, 51, 255},
                          {204, 51, 255},
                          {153, 0, 204},
                          {50, 80, 145},
                          {255, 51, 204},
                          {204, 0, 51},
                          {70, 0, 255},
                          {204, 102, 255},
                          };
//mode2
uint8_t mode2Colors[][5] = {
                         {248, 255, 231},
                         {60, 60, 150},
                         {62, 75, 153},
                         {87, 57, 167},
                         {90, 116, 166},
                          };
//mode3
uint8_t mode3Colors[][10] = {
                         {114, 193, 175},
                         {193, 238, 219},
                         {186, 252, 127},
                         {95, 240, 146},
                         {69, 236, 150},
                         {255, 255, 255},
                         {44, 105, 145},
                         {8, 233, 136},
                         {91, 163, 164},
                         {191, 217, 220},                         
                          };

//sizes of each array
#define M1COLORS 11
#define M2COLORS 5
#define M3COLORS 10



void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  if ( mode == 5 ) {  //"sleep" mode
    sleep(50);
  }
  else if ( mode == 4 ) {
    rainbowCycle(20);
  }
  else {
    flashRandom(50, 8);  // first number is 'wait' delay, shorter num == shorter twinkle
  }
 
}

void sleep(uint8_t wait) {
    colorWipe(strip.Color(0, 0, 0), 100);

      //Check for button presses to change mode
      //check for button presses each loop
      int buttonState = digitalRead(buttonPin);
      if (buttonState == LOW) {
        buttonCheck();
      }
      lastButtonState = buttonState;

    strip.show();
    delay(wait);

}

void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
 
  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
      //Check for button presses to change mode
      //check for button presses each loop
      int buttonState = digitalRead(buttonPin);
      if (buttonState == LOW) {
        buttonCheck();
      }
      lastButtonState = buttonState;
      if (mode != 4) {
        break;
      }   
    }
    if (mode != 4) {
    break;
    }
    strip.show();
    delay(wait);
  }
}

// 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);
  }
 
}

// wipe animation to indicate mode changes
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);
  }
  delay(wait);

}



//main function to blink random pixels on and off
void flashRandom(int wait, uint8_t howmany) {
  for(uint16_t i=0; i<howmany; i++) {
    //Check for button presses to change mode
    //check for button presses each loop
    int buttonState = digitalRead(buttonPin);
    if (buttonState == LOW) {
      buttonCheck();
    }
    lastButtonState = buttonState;   
    if (mode == 4) { //switch immediately to rainbow if in this mode
      break;
    }     


    int red = 255;
    int green = 0;
    int blue = 0;
    int c = 0;   

    if ( mode == 1 ) {
      c = random(M1COLORS);
      red = mode1Colors[c][0];
      green = mode1Colors[c][1];
      blue = mode1Colors[c][2];         
    }
    else if ( mode == 2 ) {   
      int c = random(M2COLORS);
      red = mode2Colors[c][0];
      green = mode2Colors[c][1];
      blue = mode2Colors[c][2];         
    }
    else if ( mode == 3 ) {   
      int c = random(M3COLORS);
      red = mode3Colors[c][0];
      green = mode3Colors[c][1];
      blue = mode3Colors[c][2];         
    }   
   
    int j = random(strip.numPixels());
    if ( strip.getPixelColor(j) != 0 ) {//if we just selected a pixel with color
      red =  ((byte)(strip.getPixelColor(j)>> 16));
      green = ((byte)(strip.getPixelColor(j)>> 8));
      blue = ((byte)strip.getPixelColor(j));
      strip.setPixelColor(j, strip.getPixelColor(j));

    // & fade out in 5 steps
      for (int x=20; x >= 0; x--) {
        int r = red * x; r /= 20;
        int g = green * x; g /= 20;
        int b = blue * x; b /= 20;
       
        strip.setPixelColor(j, strip.Color(r, g, b));
        strip.show();
        delay(wait);
      } 
    }

    else {
 
      // if pixel already dark,  will 'fade' it in 20 steps
      for (int x=0; x < 20; x++) {
        int r = red * (x+1); r /= 20;
        int g = green * (x+1); g /= 20;
        int b = blue * (x+1); b /= 20;
       
        strip.setPixelColor(j, strip.Color(r, g, b));
        strip.show();
        delay(wait);
      }
    }
  }
  // LEDs will be off when done (they are faded to 0)
}

void buttonCheck() {
  menuTime = millis();
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && lastButtonState == HIGH ) {
    buttonHoldTime = millis();
  }
 
  if (buttonState == LOW && lastButtonState == LOW ) {
//    Serial.println( "Button Press");
    if ((millis() - buttonHoldTime) > buttonHoldDelay) {
     
      if(mode == numberofmodes) {
        mode = 1;
//        lastMin = 16;
//        lastHour = 16;
//        colorWipe(strip.Color(0, 0, 0), 100);
        colorWipe(strip.Color(255, 255, 255), 20);
        colorWipe(strip.Color(0, 0, 0), 100);

      }
      else {
        mode += 1;

        colorWipe(strip.Color(255, 255, 255), 20);
        colorWipe(strip.Color(0, 0, 0), 100);
      }
      //add mode indicators here?
      buttonHoldTime = millis();
//      buttonLastPress = millis();

    }
  }
}

jeremyzschau
 
Posts: 128
Joined: Fri Feb 27, 2015 11:00 pm

Re: Gemma M0 Neopixel Code

by dastels on Thu Sep 05, 2019 10:59 am

You should be able to just change the number of pixels (the first argument to the Adafruit_NeoPixel constructor) and the pins for the neopixels and button to reflect how you have it connected.

Dave

dastels
 
Posts: 219
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.