0

Need help with a NeoPixel project
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Need help with a NeoPixel project

by SvendErichsen on Mon Jan 11, 2021 1:12 pm

Hi!

I need help with my project. I'm trying to build a LED strip setup with three connected strips. I want to be able to control the lights with button pushes. Everything worked fine when testing with an arduino on a breadboard, with only one button connected. After soldering everything I get power, but the buttons are not working. I have connected all power and ground wires from the buttons together, and soldered them to the 5V and GND on the back of the trinket. The buttons also have extra ground wires that go to pin 9 and 10 on the trinket. The data wires from two of the LED strips are connected to pin 5 and the third strip is connected to pin 7.

The parts I am using:
Itsy Bitsy 32u4 5V
Trinket Pro Backpack
4400mAh Lipo battery

One slide switch to turn off power
Two push switches connected to 10K ohm resistors
Three NeoPixel Led strips
3-pin Jumper cables

Here is my code:
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

//Button Pins
#define BUTTON_PIN 9
#define BUTTON_PIN2 10

//LED Strip Pins
#define PIXEL_PIN 5
#define PIXEL_PIN2 7

#define PIXEL_COUNT 30
#define PIXEL_COUNT2 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(PIXEL_COUNT2, PIXEL_PIN2, NEO_RGBW + NEO_KHZ800);

const int  buttonPin = 3;
bool onAndOff = false;           
int buttonState = 0;

const int  buttonPin2 = 4;
bool onAndOff2 = false;           
int buttonState2 = 0;

void setup()
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
 
  strip2.begin();
  for(uint16_t i = 0; i < strip2.numPixels(); i++) {
    strip2.setPixelColor(i, 255, 100, 0);
  }
  strip2.show(); // Initialize all pixels to 'off'
 
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
 
  pinMode(buttonPin2, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
   
    if (buttonState == HIGH)
     
      {
      if (onAndOff == false)
        {
        onAndOff = true;
        rainbowCycle(20);     
        }
      else
        {
        onAndOff = false;
        colorWipe(strip.Color(0, 0, 255), 0); // Blue
        }
      }
   
    else
   
      if (buttonState2 == HIGH)
     
        {
        if (onAndOff2 == false)
        {
        onAndOff2 = true;
        colorWipe(strip.Color(0, 0, 0, 255), 0); // White     
        }
      else
        {
        onAndOff2 = false;
        brighten ();
        darken ();
        }
      }
            }

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 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));
    }
    strip.show();
    delay(wait);
  }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

void brighten() {
  uint16_t i, j;
 
  for (j = 20; j < 200; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j/2, 0);
    }
    strip.show();
    delay (1);
    }
    //delay (1500);
  }

void darken() {
  Serial.begin(9600);
  uint16_t i, j;
 
  for (j = 200; j > 20; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j/2, 0);
    }
    strip.show();
    // delay (10);
    Serial.println(j);
    }
    delay(1);
}


I appreciate any help. Thank you.

Svend
Attachments
LED-trinket.jpg
Picture of the soldered project without the backpack
LED-trinket.jpg (13.66 KiB) Viewed 75 times
Billies sverd_bb.png
Fritzing plan - Red wires to the back of the buttons are supposed to be yellow.
Billies sverd_bb.png (303.85 KiB) Viewed 75 times

SvendErichsen
 
Posts: 3
Joined: Mon Jan 11, 2021 12:49 pm

Re: Need help with a NeoPixel project

by dastels on Mon Jan 11, 2021 1:31 pm

I'm concerned by how your buttons look to be wired. With mode INPUT_PULLUP one side should be wired to the input pin as you show and the other side directly to ground. None of the button should be wired to 5v. In the code when a button is pushed the input will read as LOW, otherwise as HIGH.

Addressing that is a good start... and could be all that's wrong.

Dave

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

Re: Need help with a NeoPixel project

by SvendErichsen on Wed Jan 13, 2021 10:56 am

Thank you!

So here is an update.

I tried to remove the 5V and move the ground to the opposite side of the input, and the buttons started to react, but the LED/ItsyBitsy went crazy with the code. It also picked up a lot of interference and the lights would change if I just put my hand close to one of the buttons or the ItsyBitsy. If I helped ground the button with my fingers it would work better. So I figured there had to be a wiring problem.

Therefore I unsoldered everything and rewired every joint, but the problem persisted. I went back to my breadboard and every way I tried to wire it produced strange results, unless I used a 5V on the button, which worked except that it turned off the LEDs prematurely. I also tried it with a simpler code with only one button and fixed some errors in my previous code, more specifically that I had some wrong code on my button inputs. Nothing worked.

My guess is that there is something wrong with the buttons I have. So I will try to change them.

Thank you so much for your quick reply. I will try to fix this and come back if nothing works. Im completely new to small electronics and Arduino, so I am certain that I am going to make more mistakes. ;)

Svend

SvendErichsen
 
Posts: 3
Joined: Mon Jan 11, 2021 12:49 pm

Re: Need help with a NeoPixel project

by dastels on Wed Jan 13, 2021 11:40 am

I doubt there is a problem with the buttons, but who knows.

You might already know, but the 2 pins on the left and right sides of the buttons are connected internally (based on their orientation on your wiring diagram).

Triggering on moving your hand close would indicate that the input is floating and the pullup isn't being done right. You still have the mode set to INPUT_PULLUP?

Your code is reading the raw button state, which is usually a problem. Switches bounce a lot when you press and release them, changing value between HIGH and LOW repeastedly each time. Looking at your code you might (but I'm not sure exactly what behavior you want) want to trigger on state change (e.g. when the button is pressed rather than while it is being held down). Both of these can be addressed by debouncing the switch. The basic ideas are discussed in my guide: https://learn.adafruit.com/debouncer-library-python-circuitpython-buttons-sensors. The code examples are in CircuitPython, but I based the debouncing library on the Bounce2 arduino library: https://www.arduino.cc/reference/en/libraries/bounce2/.

Making mistakes is how we learn... embrace them.

Dave

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

Re: Need help with a NeoPixel project

by SvendErichsen on Wed Jan 13, 2021 4:39 pm

Thanks again. ;)

Yes, Im aware of the buttons internal connections, and the INPUT_PULLUP is still in the code. But Im not sure that it is working. On my breadboard I also need a resistor to make the button work at all. Shouldnt PULLUP make up for the resistor?

I will check out your tutorial, it sounds like something I might use. I want the pushbuttons to act like a switch, so that I dont need to press them continously for them to keep a HIGH state. With two buttons I want to be able to control 4 different light programmes, two for each button that will switch from LOW to HIGH with each press.

Svend

SvendErichsen
 
Posts: 3
Joined: Mon Jan 11, 2021 12:49 pm

Re: Need help with a NeoPixel project

by dastels on Wed Jan 13, 2021 5:05 pm

That's the way to do it: pin mode is INPUT_PULLUP, the switch connects the pin to ground when it's pressed, and a pressed button reads as LOW.

Dave

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

Please be positive and constructive with your questions and comments.