Code Help: Glowing NeoPixels

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Code Help: Glowing NeoPixels

Post by joshuakane »

Hello,

I am working on a project where I have two buttons (inPinA & inPinB)

When you press the first button (inPinA) I want the brightness to increase on the NeoPixel Ring until it reaches the Maximum brightness. When you release the lights will decrease to the base level again (20)

When you press the second button (inPinB) I want the brightness to go to the maximum right away, and then when your release the button the lights will decrease back to the base level again (20)

I don't seem to have it quite right, so any assistance would be greatly appreciated

Code: Select all

void loop() {
  strip.setPixelColor(12, lime);
  strip.setPixelColor(13, lime);
  strip.setPixelColor(14, lime);
  strip.setPixelColor(15, lime);
  strip.setPixelColor(16, lime);

  //if button is pressed and we're under the max of 255 brightness,
  if ( digitalRead(inPinA) == LOW && brightness < 256 ) brightness++; 
 
  //or if the button is released, and we are under the minimum 20 brightness,
  else if ( digitalRead(inPinA) == HIGH && brightness > 20) {brightness--;} //-1, min of 0

if ( digitalRead(inPinB) == LOW && brightness < 256 ) brightness=256; 
   //or if the button is released, and we are under the minimum 20 brightness,
 else if ( digitalRead(inPinB) == HIGH && brightness > 20) {brightness--;} //-1, min of 0


//do your animation stuff here, or re-set your pixels to full-values, in this example, full green.
   for ( uint8_t i=0; i < 12; i++ ) strip.setPixelColor ( i, 0, 0, 255 ); 
   //then update the overall strip brightness, as this function re-writes all color values as: pixel*(brightness/255) == dimmed pixel value;

  strip.setBrightness ( brightness );
  //then push the data to the pixels.
  strip.show ();
  //if you don't add some delay, the brightness will crawl up super fast
  delay ( 15 );
}
 
The complete sketch can be seen here

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN 3
#define yellow strip.Color(255,255,0)
#define green strip.Color(0,255,0)
#define blue strip.Color(0,0,255)
#define lime strip.Color(174,255,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(17, PIN, NEO_GRB + NEO_KHZ800);


uint8_t brightness = 20; //global variable

//Button Push Setup

int inPinA = 4;             // the number of the input pin (RX) - Button "A"
int current_stateA = HIGH;   // the debounced input value
int inPinB = 8;             // the number of the input pin (RX) - Button "A"
int current_stateB = HIGH;   // the debounced input value

void setup() {
  strip.begin();
  strip.setBrightness ( brightness );
  strip.setPixelColor(0, blue);
  strip.setPixelColor(1, blue);
  strip.setPixelColor(2, blue);
  strip.setPixelColor(3, blue);
  strip.setPixelColor(4, blue);
  strip.setPixelColor(5, blue);
  strip.setPixelColor(6, blue);
  strip.setPixelColor(7, blue);
  strip.setPixelColor(8, blue);
  strip.setPixelColor(9, blue);
  strip.setPixelColor(10, blue);
  strip.setPixelColor(11, blue);  
  strip.setPixelColor(12, lime);
  strip.setPixelColor(13, lime);
  strip.setPixelColor(14, lime);
  strip.setPixelColor(15, lime);
  strip.setPixelColor(16, lime);
  strip.show(); // Initialize all pixels to 'off'

  pinMode(inPinA, INPUT_PULLUP);
  pinMode(PIN, OUTPUT); 
  pinMode(inPinB, INPUT_PULLUP);
  pinMode(PIN, OUTPUT); 

  }

void loop() {
  strip.setPixelColor(12, lime);
  strip.setPixelColor(13, lime);
  strip.setPixelColor(14, lime);
  strip.setPixelColor(15, lime);
  strip.setPixelColor(16, lime);

  //if button is pressed and we're under the max of 255 brightness,
  if ( digitalRead(inPinA) == LOW && brightness < 256 ) brightness++; 
 
  //or if the button is released, and we are under the minimum 20 brightness,
  else if ( digitalRead(inPinA) == HIGH && brightness > 20) {brightness--;} //-1, min of 0

if ( digitalRead(inPinB) == LOW && brightness < 256 ) brightness=256; 
   //or if the button is released, and we are under the minimum 20 brightness,
 else if ( digitalRead(inPinB) == HIGH && brightness > 20) {brightness--;} //-1, min of 0


//do your animation stuff here, or re-set your pixels to full-values, in this example, full green.
   for ( uint8_t i=0; i < 12; i++ ) strip.setPixelColor ( i, 0, 0, 255 ); 
   //then update the overall strip brightness, as this function re-writes all color values as: pixel*(brightness/255) == dimmed pixel value;

  strip.setBrightness ( brightness );
  //then push the data to the pixels.
  strip.show ();
  //if you don't add some delay, the brightness will crawl up super fast
  delay ( 15 );
}

 

// Fill the dots one after the other with a color
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);
  }
}


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

Re: Code Help: Glowing NeoPixels

Post by adafruit_support_bill »

You might find this tutorial helpful: https://learn.adafruit.com/multi-taskin ... ino-part-3
What you describe can be done fairly easily with the example code there.

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Code Help: Glowing NeoPixels

Post by joshuakane »

Hi Bill,

I went ahead and made the changes as suggested by the tutorial. I am getting the following error when I go to compile

_2_Button_Test_ino.ino: In member function 'void NeoPatterns::loop()':
_2_Button_Test_ino:86: error: 'class Adafruit_NeoPixel' has no member named 'update'
_2_Button_Test_ino.ino: At global scope:
_2_Button_Test_ino:146: error: expected unqualified-id at end of input
Can you help point out what I am doing wrong?

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN 3
#define yellow strip.Color(255,255,0)
#define green strip.Color(0,255,0)
#define blue strip.Color(0,0,255)
#define lime strip.Color(174,255,0)


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




// Pattern types supported:
enum  pattern { NONE, CHARGE_AND_FIRE, JUST_FIRE };

// NeoPattern Class - derived from the Adafruit_NeoPixel class
class NeoPatterns : public Adafruit_NeoPixel
{
  public:

    // Member Variables:
    pattern  ActivePattern;  // which pattern is running

    unsigned long Interval;   // milliseconds between updates
    unsigned long lastUpdate; // last update of position
    unsigned long brightness; //global variable

    void (*OnComplete)();  // Callback on completion of pattern

    // Constructor - calls base-class constructor to initialize strip
    NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
      : Adafruit_NeoPixel(pixels, pin, type)
    {
      OnComplete = callback;
    }

    void update()
    {
      if ((millis() - lastUpdate) > Interval) // time to update
      {
        lastUpdate = millis();
        switch (ActivePattern)
        {
          case CHARGE_AND_FIRE:
            ChargeAndFire();
            break;
          case JUST_FIRE:
            JustFire();
            break;
          default:
            break;
        }
      }
    }

    void setup() {
      strip.begin();
      brightness = 20;
      strip.setBrightness ( brightness );
      strip.setPixelColor(0, blue);
      strip.setPixelColor(1, blue);
      strip.setPixelColor(2, blue);
      strip.setPixelColor(3, blue);
      strip.setPixelColor(4, blue);
      strip.setPixelColor(5, blue);
      strip.setPixelColor(6, blue);
      strip.setPixelColor(7, blue);
      strip.setPixelColor(8, blue);
      strip.setPixelColor(9, blue);
      strip.setPixelColor(10, blue);
      strip.setPixelColor(11, blue);
      strip.setPixelColor(12, lime);
      strip.setPixelColor(13, lime);
      strip.setPixelColor(14, lime);
      strip.setPixelColor(15, lime);
      strip.setPixelColor(16, lime);
      strip.show(); // Initialize all pixels to 'off'

      pinMode(4, INPUT_PULLUP);
      pinMode(8, INPUT_PULLUP);
    }

    void loop() {
      strip.update;
      if (digitalRead(4) == LOW) // Button #1 pressed
      {
        strip.setPixelColor(12, lime);
        strip.setPixelColor(13, lime);
        strip.setPixelColor(14, lime);
        strip.setPixelColor(15, lime);
        strip.setPixelColor(16, lime);
        ChargeAndFire();
      }

      else if (digitalRead(8) == LOW) // Button #1 pressed
      {
        strip.setPixelColor(12, lime);
        strip.setPixelColor(13, lime);
        strip.setPixelColor(14, lime);
        strip.setPixelColor(15, lime);
        strip.setPixelColor(16, lime);
        JustFire();
      }

      else
      {
        brightness--; //-1, min of 0

        //do your animation stuff here, or re-set your pixels to full-values, in this example, full green.
        for ( uint8_t i = 0; i < 12; i++ ) strip.setPixelColor ( i, 0, 0, 255 );
        //then update the overall strip brightness, as this function re-writes all color values as: pixel*(brightness/255) == dimmed pixel value;

        strip.setBrightness ( brightness );
        //then push the data to the pixels.
        strip.show ();
        //if you don't add some delay, the brightness will crawl up super fast
        delay ( 15 );
      }

    }

    // Fill the dots one after the other with a color
    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);
      }
    }


    void ChargeAndFire()
    {
      //if button is pressed and we're under the max of 255 brightness,

      while ( digitalRead(4) == LOW && brightness < 256 ) brightness++;
    }


    void JustFire()
    {
      while ( digitalRead(8) == LOW && brightness < 256 ) brightness = 256;
      //or if the button is released, and we are under the minimum 20 brightness,
    }
}

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

Re: Code Help: Glowing NeoPixels

Post by adafruit_support_bill »

Code: Select all

Adafruit_NeoPixel strip = Adafruit_NeoPixel(17, PIN, NEO_GRB + NEO_KHZ800);
You are declaring your strip as Adafruit_NeoPixel instead of NeoPatterns. If you want to use the NeoPatterns functions, you have to declare your strip as a one.

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Code Help: Glowing NeoPixels

Post by joshuakane »

Hi Bill,

I went through and kept playing with the code you suggested, but it seems to be making things very complicated for something that is a pretty simple setup. I just have one string of Pixels that I am trying to light. I am just trying to have the button push from Button a slowly light up the LEDS while a button push from Button B goes to full brightness. I realized that I needed to separate out the logic for button A and button B, and I have done that in the code below. Button A now works as expected, but button B still have no change in the LED's. I also verified that the buttons are connected correctly to the hardware.

Here is the main loop, can you please let me know if there is something I am doing that is preventing the code from executing properly?

Code: Select all

void loop() {
  strip.setPixelColor(12, lime);
  strip.setPixelColor(13, lime);
  strip.setPixelColor(14, lime);
  strip.setPixelColor(15, lime);
  strip.setPixelColor(16, lime);

	
	if( digitalRead(inPinA) == LOW ) pinAlastpushed == true;
	if( digitalRead(inPinB) == LOW ) pinAlastpushed == false;
	
	if( pinAlastpushed )
	{
  	//if button is pressed and we're under the max of 255 brightness,
  	if ( digitalRead(inPinA) == LOW && brightness < 256 ) brightness++;

  	//or if the button is released, and we are under the minimum 20 brightness,
  	else if ( digitalRead(inPinA) == HIGH && brightness > 20) {
    	brightness--; //-1, min of 0
  	}
	}
	else
	{
  	if ( digitalRead(inPinB) == LOW) brightness = 256;

  	//or if the button is released, and we are under the minimum 20 brightness,
  	else if ( digitalRead(inPinB) == HIGH && brightness > 20) {
    	brightness--; //-1, min of 0
  	}
	}

  //do your animation stuff here, or re-set your pixels to full-values, in this example, full green.
  for ( uint8_t i = 0; i < 12; i++ ) strip.setPixelColor ( i, 0, 0, 255 );
  //then update the overall strip brightness, as this function re-writes all color values as: pixel*(brightness/255) == dimmed pixel value;

  strip.setBrightness ( brightness );
  //then push the data to the pixels.
  strip.show ();
  //if you don't add some delay, the brightness will crawl up super fast
  delay ( 15 );
}

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

Re: Code Help: Glowing NeoPixels

Post by adafruit_support_bill »

Actually, it should make things quite simple. What you need is already there. No need to re-invent it.

The Fader pattern is part of the NeoPattern class. Just trigger that with button A,
And when button B is pressed, call the ColorSet function.

User avatar
joshuakane
 
Posts: 282
Joined: Sat Apr 13, 2013 4:40 pm

Re: Code Help: Glowing NeoPixels

Post by joshuakane »

I needed the patterns to do something slightly different. I figured out what I was doing wrong, it was a logic flaw in my program. I went ahead and added some debugging statements and was able to get it resolved.

Thanks for pointing me to the other guide, it looks like it would be very useful if I end up using several types of NeoPixel hardware accessories.

For what it is worth, I am going to paste my code below on the off chance someone might find it useful to them. I also wanted to comment on how much I am enjoying the Trinket Pro micro-processors. You guys make awesome stuff!

-- Joshua

Code: Select all

/*
Halo Hunter Mgalekgolo Arm Cannon
http://halo.wikia.com/wiki/Mgalekgolo

Designed for Alexis Smith's Mgalekgolo Hunter Cosplay
https://www.facebook.com/HaloHunters

*/



#include <Adafruit_NeoPixel.h>

#define PIN1 3
#define PIN2 12
#define yellow strip.Color(255,255,0)
#define green strip.Color(0,255,0)
#define blue strip.Color(0,0,255)
#define lime strip.Color(174,255,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 Cannon = Adafruit_NeoPixel(12, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel Arm = Adafruit_NeoPixel(5, PIN2, NEO_GRB + NEO_KHZ800);


uint8_t brightness = 20; //global variable

//Button Push Setup

int inPinA = 4;             // the number of the input pin (RX) - Button "A"
int current_stateA = HIGH;   // the debounced input value
int inPinB = 8;             // the number of the input pin (RX) - Button "A"
int current_stateB = HIGH;   // the debounced input value

bool pinAlastpushed = true;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  Serial.println("Serial Debugging Enabled");  // prints hello with ending line break 
  
  Cannon.begin();
  Arm.begin();
  
  Cannon.setBrightness ( brightness );
  Arm.setBrightness ( brightness );
  colorWipeCannon(Cannon.Color(0, 0, 255), 0 ); //Blue
  colorWipeArm(Arm.Color(174, 255, 0), 0 ); //Lime
  Cannon.show(); // Initialize all cannon pixels to Blue
  Arm.show(); // Initialize all arm pixels to Lime

// Setting InputPin Modes
  pinMode(inPinA, INPUT_PULLUP);
  pinMode(PIN1, OUTPUT);
  pinMode(inPinB, INPUT_PULLUP);
  pinMode(PIN2, OUTPUT);

}

void loop() {
        if( digitalRead(inPinA) == LOW ) pinAlastpushed == true;
	if( digitalRead(inPinB) == LOW ) pinAlastpushed == false;

	if( pinAlastpushed )
	{
        Serial.println("Button A is Pressed");  
  	//if button is pressed and we're under the max of 255 brightness,
  	if ( digitalRead(inPinA) == LOW && brightness < 256 ) { 
        brightness= (brightness + 3);
        Serial.print("Increasing Brightness to: ");  
        Serial.println (brightness);  
        SetBrightnessCannon();
        SetBrightnessArm();
        }
       //or if the button is released, and we are under the minimum 20 brightness,
  	else if ( digitalRead(inPinA) == HIGH && brightness > 20) {
 	brightness--; //-1, min of 0
        Serial.print("Decreasing Brightness to: ");  
        Serial.println (brightness);  
        SetBrightnessCannon();
        SetBrightnessArm();
  	}
	}
        if ( digitalRead(inPinB) == LOW) {
        Serial.println("Button B is Pressed");  
        brightness=255;
        Serial.print("Increasing Brightness to: ");  
        Serial.println (brightness);  
        SetBrightnessCannon();
        SetBrightnessArm();
        colorWipeCannon(Cannon.Color(0, 0, 255), 0 ); //Blue
        colorWipeArm(Arm.Color(174, 255, 0), 0 ); //Lime
        Cannon.show(); // Initialize all cannon pixels to Blue
        Arm.show(); // Initialize all arm pixels to Lime
    	//or if the button is released, and we are under the minimum 20 brightness,
  	}
        else 
        {
        if ( digitalRead(inPinA) == HIGH && digitalRead(inPinB) == HIGH && brightness > 20) {
        Serial.println("No buttons Pushed");  
    	AtRest();
          /* brightness--; //-1, min of 0
        SetBrightnessCannon();
        SetBrightnessArm();
        Cannon.show ();
        Arm.show (); */
        }
      }	
}

/*
// Fill the dots one after the other with a color
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);
  }
}
  // Initialize for a ColorWipe
*/

  // Fill the dots on the cannon one after the other with a color
void colorWipeCannon(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < Cannon.numPixels(); i++) {
    Cannon.setPixelColor(i, c);
    Cannon.show();
    delay(wait);
  }
}

  // Fill the dots on the Arm one after the other with a color
void colorWipeArm(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < Arm.numPixels(); i++) {
    Arm.setPixelColor(i, c);
    Arm.show();
    delay(wait);
  }
}

  void AtRest(){
    brightness = 20;
    Cannon.setBrightness ( brightness );
    Arm.setBrightness ( brightness );
    colorWipeCannon(Cannon.Color(0, 0, 255), 0);
    colorWipeArm(Arm.Color(174, 255, 0), 0);
  }

void SetBrightnessCannon() {
    for ( uint8_t i = 0; i < 13; i++ ) Cannon.setPixelColor ( i, 0, 0, 255 );
    //then update the overall strip brightness, as this function re-writes all color values as: pixel*(brightness/255) == dimmed pixel value;

    Cannon.setBrightness ( brightness );
    //then push the data to the pixels.
    Cannon.show ();
    //if you don't add some delay, the brightness will crawl up super fast
    delay ( 20 );
}

void SetBrightnessArm() {
    for ( uint8_t i = 0; i < 13; i++ ) Arm.setPixelColor ( i, 174, 255, 0 );
    //then update the overall strip brightness, as this function re-writes all color values as: pixel*(brightness/255) == dimmed pixel value;
    Arm.setBrightness ( brightness );
    //then push the data to the pixels.
    Arm.show ();
    //if you don't add some delay, the brightness will crawl up super fast
    delay ( 20 );
}


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

Return to “Arduino”