0

NeoPixelRing code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

NeoPixelRing code

by darylary on Thu Jan 12, 2017 10:43 am

Hi,

I'm trying to control my 16 LED RGB NeoPixel ring, but I can't seem to make it work. I've tried using a lot of different codes, but none have worked for me.
First oft all, I am a beginner at coding and prototyping, so bare with me.

I am Using the Arduino Yun with the grove base shield. Also I have connected a grove touch button and of course the NeoPixel Ring.

I want to do this:

1. If I push the button, all oft the LEDs are supposed to light up in bright green.
2.If I push the button again, 2 LED's (next to each other)should go out. I want to be able to push the button until all of them are out.
3.lastly, if I push the button again, it's supposed to turn on all of the green lights again.

I'm a total noob and I'd really appreciate your help!

darylary
 
Posts: 4
Joined: Thu Jan 12, 2017 10:36 am

Re: NeoPixelRing code

by adafruit_support_bill on Thu Jan 12, 2017 10:53 am

If you post the code you have so far and describe what parts do and don't work we'll take a look.

adafruit_support_bill
 
Posts: 66776
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by darylary on Thu Jan 12, 2017 11:04 am

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#define PIN 7
#define NUM_LIGHTS  16

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

// this constant won't change:
const int  buttonPin = 3;    // the pin that the pushbutton is attached to
const int ledPin = 7;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button



void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
 // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);

    lastButtonState = buttonState;

 
    uint32_t low = strip.Color(0, 0, 0);
    uint32_t high = strip.Color(0, 255, 0);
 
    // Turn them off
    for( int i = 0; i<NUM_LIGHTS; i++){
        strip.setPixelColor(i, high);
        strip.show();
    }   
    delay(1000);
   
    for( int i = 0; i<NUM_LIGHTS; i++){
        strip.setPixelColor(i, low);
        strip.show();
    }   
  }
}



So the statechange code worked on its own and the Neopixel code too. But together they don't seem to work.

darylary
 
Posts: 4
Joined: Thu Jan 12, 2017 10:36 am

Re: NeoPixelRing code

by adafruit_support_bill on Thu Jan 12, 2017 12:20 pm

You need to put your led code in a 'switch' statement to handle the various cases.

The structure of it would be something like this:
Code: Select all | TOGGLE FULL SIZE
   switch(buttomPushCounter)
   {
      case 0:
      {
         // code for 0 pushes
      }
      break;
      case 1:
      {
         // code for 1 push
      }
      break;
      case 2:
      {
         // code for 2 pushes
      }
      break;
      default
      {
         // code for everything else
      }
      break:
   }

adafruit_support_bill
 
Posts: 66776
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by darylary on Wed Jan 18, 2017 10:21 am

Hi, thanks a lot for the advice.
I did different cases now, but I think there's a problem with the counter.
All of the leds are on, but the cases don't seem to work. just two off them turn off at some point.

Code: Select all | TOGGLE FULL SIZE
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      16

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

//int delayval = 500; // delay for half a second



const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;
int showType = 0;



void setup() {


 // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

 
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  strip.begin(); // This initializes the NeoPixel library.



 
}

void loop() {

buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
 /* if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }*/

  button_press();

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.

 /* for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }*/
}

void startShow(int i) {
  switch(i){
    case 0:
            seg1();
            seg2();
            seg3();
            seg4();
            seg5();
            seg6();
            seg7();
            seg8();
            strip.show();
            break;
    case 1:
            seg2();
            seg3();
            seg4();
            seg5();
            seg6();
            seg7();
            seg8();
              //strip.show();
            break;
    case 2:
            seg3();
            seg4();
            seg5();
            seg6();
            seg7();
            seg8();
             // strip.show();
            break;
    case 3: seg4();
            seg5();
            seg6();
            seg7();
            seg8();
            break;
    case 4: seg5();
            seg6();
            seg7();
            seg8();
            break;
    case 5: seg6();
            seg7();
            seg8();
            break;
    case 6:
            seg7();
            seg8();
            break;
    case 7: seg8();
            break;
    case 8: seg9();
            break;         
   
  }
}






Code: Select all | TOGGLE FULL SIZE
void button_press(){

   buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  bool newState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    if (newState == HIGH) {
      showType++;
      if (showType > 7)
        {showType=0;}
      startShow(showType);
      strip.show();
    }
    delay(500);
    //seg1();
    //zaehler++
    //evtl: delay
    strip.show();
  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
    //led_aus();
  }
  }



Code: Select all | TOGGLE FULL SIZE
void seg1(){
  strip.setPixelColor(0, strip.Color(0, 150, 0, 10));
  strip.setPixelColor(1, strip.Color(0, 150, 0));
  }

void seg2(){
  strip.setPixelColor(2, strip.Color(0, 150, 0));
  strip.setPixelColor(3, strip.Color(0, 150, 0));
  }

void seg3(){
  strip.setPixelColor(4, strip.Color(0, 150, 0));
  strip.setPixelColor(5, strip.Color(0, 150, 0));
  }
void seg4(){
  strip.setPixelColor(6, strip.Color(0, 150, 0));
  strip.setPixelColor(7, strip.Color(0, 150, 0));
  }
  void seg5(){
  strip.setPixelColor(8, strip.Color(0, 150, 0));
  strip.setPixelColor(9, strip.Color(0, 150, 0));
  }
  void seg6(){
  strip.setPixelColor(10, strip.Color(0, 150, 0));
  strip.setPixelColor(11, strip.Color(0, 150, 0));
  }
  void seg7(){
  strip.setPixelColor(12, strip.Color(0, 150, 0));
  strip.setPixelColor(13, strip.Color(0, 150, 0));
  }
  void seg8(){
  strip.setPixelColor(14, strip.Color(0, 150, 0));
  strip.setPixelColor(15, strip.Color(0, 150, 0));
  }
void seg9(){
  strip.setPixelColor(0, strip.Color(0, 0, 0));
  strip.setPixelColor(1, strip.Color(0, 0, 0));
  strip.setPixelColor(2, strip.Color(0, 0, 0));
  strip.setPixelColor(3, strip.Color(0, 0, 0));
  strip.setPixelColor(4, strip.Color(0, 0, 0));
  strip.setPixelColor(5, strip.Color(0, 0, 0));
  strip.setPixelColor(6, strip.Color(0, 0, 0));
  strip.setPixelColor(7, strip.Color(0, 0, 0));
  strip.setPixelColor(8, strip.Color(0, 0, 0));
  strip.setPixelColor(9, strip.Color(0, 0, 0));
  strip.setPixelColor(10, strip.Color(0, 0, 0));
  strip.setPixelColor(11, strip.Color(0, 0, 0));
  strip.setPixelColor(12, strip.Color(0, 0, 0));
  strip.setPixelColor(13, strip.Color(0, 0, 0));
  strip.setPixelColor(14, strip.Color(0, 0, 0));
  strip.setPixelColor(15, strip.Color(0, 0, 0));

}

darylary
 
Posts: 4
Joined: Thu Jan 12, 2017 10:36 am

Re: NeoPixelRing code

by adafruit_support_bill on Wed Jan 18, 2017 10:25 am

Other than your "seg9()" all the functions just turn pixels on. They don't turn off any pixels that might already be on.. If you want pixels to turn off when you the counter changes, you have to write code for that.

adafruit_support_bill
 
Posts: 66776
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by darylary on Wed Jan 18, 2017 10:43 am

it works! thank you so much for your help!

darylary
 
Posts: 4
Joined: Thu Jan 12, 2017 10:36 am

Re: NeoPixelRing code

by adafruit_support_bill on Wed Jan 18, 2017 10:49 am

Great! Thanks for the follow-up.

adafruit_support_bill
 
Posts: 66776
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by mqdean on Fri Feb 02, 2018 8:33 am

Hi

I am trying something similar to this post. I am using a FSR to switch on the led light. I am trying to switch on one led light whenever the serial value goes below 20. So when I apply the required force once, I want one pixel to light up.. 2nd time when force is applied, 2 lights to light up and so on.

I am using FLORA and neopixel ring (16)

Below is my code for your reference. Hope you could help me on this. Thank you!

Code: Select all | TOGGLE FULL SIZE
#include <EasyNeoPixels.h>

#include <EasyNeoPixels.h>

#include <Adafruit_NeoPixel.h>

#define PIN 6

// 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(16, PIN, NEO_GRB + NEO_KHZ800);

int force;

void setup() {
  pinMode(9, INPUT_PULLUP);
  strip.begin();
  strip.show();
}

void loop() {
  // put your main code here, to run repeatedly:
force = analogRead(9);
Serial.println(force);
if(force <= 20)
{
strip.setPixelColor(7, 250, 130, 1);
strip.setBrightness(10);
strip.show();
}
}
Last edited by adafruit_support_bill on Fri Feb 02, 2018 9:03 am, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Fri Feb 02, 2018 9:10 am

A couple of problems there:
Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(7, 250, 130, 1);

Your pixel number is hard-coded (7). So you are setting the same pixel every time.
Code: Select all | TOGGLE FULL SIZE
strip.setBrightness(10);

Calling this repeatedly can cause problems. You should only call this once in setup().

To get your pixels to increment with each press, you need to declare a global variable to keep count of the pixels:

Code: Select all | TOGGLE FULL SIZE
int pixelCount = 0;


Then in your code you need to use that value to set the pixel. And you need to increment it after each press:
Code: Select all | TOGGLE FULL SIZE
int pixelCount = 0;

void loop()
{
   // put your main code here, to run repeatedly:
   force = analogRead(9);
   Serial.println(force);
   if(force <= 20)
   {
      strip.setPixelColor(pixelCount++, 250, 130, 1);
      strip.show();
   }
}

adafruit_support_bill
 
Posts: 66776
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by mqdean on Fri Feb 02, 2018 9:33 am

I did not expect such a prompt reply. You guys are incredible.
Below is what I have coded. All the pixels light up when I hit the FSR once. What am I doing wrongly?

Code: Select all | TOGGLE FULL SIZE
#include <EasyNeoPixels.h>

#include <EasyNeoPixels.h>

#include <Adafruit_NeoPixel.h>

#define PIN 6

// 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(16, PIN, NEO_GRB + NEO_KHZ800);

int force;

void setup() {
  pinMode(9, INPUT_PULLUP);
  strip.setBrightness(10);
  strip.begin();
  strip.show();
}

int pixelCount = 0;

void loop() {
   // put your main code here, to run repeatedly:
   force = analogRead(9);
   Serial.println(force);
   if(force <= 20)
   {
      strip.setPixelColor(pixelCount++, 250, 130, 1);
      strip.show();
   }
}
Last edited by adafruit_support_bill on Fri Feb 02, 2018 9:54 am, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by mqdean on Fri Feb 02, 2018 9:39 am

Its working. Thank you man!

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Fri Feb 02, 2018 9:55 am

All the pixels light up when I hit the FSR once. What am I doing wrongly?

You may need to 'debounce' the input. Adding a short delay inside the 'if' statement should work.

adafruit_support_bill
 
Posts: 66776
Joined: Sat Feb 07, 2009 10:11 am

Re: NeoPixelRing code

by mqdean on Fri Feb 02, 2018 10:35 am

Thanks a lot. Great Help. It is working. Just want to confirm that I have written the delay correctly.

Code: Select all | TOGGLE FULL SIZE
#include <EasyNeoPixels.h>

#include <EasyNeoPixels.h>

#include <Adafruit_NeoPixel.h>

#define PIN 6

// 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(16, PIN, NEO_GRB + NEO_KHZ800);

int force;

void setup() {
  pinMode(9, INPUT_PULLUP);
  strip.setBrightness(255);
  strip.begin();
  strip.show();
}

int pixelCount = 0;

void loop() {
   // put your main code here, to run repeatedly:
   force = analogRead(9);
   Serial.println(force);
   if(force < 20)
   {
      delay(100);
      strip.setPixelColor(pixelCount++, 0, 255, 0);
      strip.show();
   }
}
Last edited by adafruit_support_bill on Fri Feb 02, 2018 11:09 am, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

mqdean
 
Posts: 27
Joined: Mon Jan 29, 2018 11:54 pm

Re: NeoPixelRing code

by adafruit_support_bill on Fri Feb 02, 2018 11:12 am

Yes. The delay looks fine. That will wait 100 milliseconds (1/10th second) before reading the sensor again. So as long as the pressure is applied for 1/10th second or less, it will only light one more pixel.

adafruit_support_bill
 
Posts: 66776
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.