Go Down

Topic: Trying to control NeoPixel LED Strip with a Roller Switch (Read 83 times) previous topic - next topic

Gayson

I have a roller switch connected as normally open to 5v and Pin 2 on an arduino uno r3.  My goal is to have a rainbow effect start on the strip when the switch is on and have the effect turn off when the switch is off.  I've been googling and googling and I still can't figure this out.  The strip begins lighting up as soon as the arduino is connected. Any help would be greatly appreciated!

I know the Strip is connected to the arduino correctly, but for the switch I haven't added any resistors in the circuit.

This is the strip I am using: http://adafru.it/2541

Here is my code

Code: [Select]

// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__

#endif

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

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 60

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 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)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)


// setup() function -- runs once at startup --------------------------------

  int inPin = 2;


void setup() {

  pinMode(inPin, INPUT);
 
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)


}


// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {

strip.clear();
strip.show();
if (digitalRead(inPin) == HIGH){


  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(10);  // Pause for a moment
   
  } 
}
if (digitalRead(inPin) == LOW){
  strip.clear();
  strip.show();
 
}
delay(10);
}






This is my first time using either ardunio or addressable leds so I hope its just some noob mistake I'm making.

Thank You!!!

vinceherman

Can you post a wiring diagram?  Pen, paper and a camera are good for this.

Can you put in debugging code?  Print the state of the button to the serial monitor.

Oh, kudos on your proper use of code tags on your first post!  Karma++

Gayson

Thank you haha, I saw other people getting slaughtered for not using them.

Here is a picture of the diagram: https://imgur.com/AeBxCUA

And a picture of the actual Circuit: https://imgur.com/WvVgl4e


Here is a diagram, I added the serial monitor lines as well.



Code: [Select]

// A basic everyday NeoPixel strip test program.

// NEOPIXEL BEST PRACTICES for most reliable operation:
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
//   connect GROUND (-) first, then +, then data.
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
//   a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
// (Skipping these may work OK on your workbench but can fail in the field)

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__

#endif

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

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 60

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 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)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)


// setup() function -- runs once at startup --------------------------------

  int inPin = 2;


void setup() {
  Serial.begin(9600);
  pinMode(inPin, INPUT);
  
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)


}


// loop() function -- runs repeatedly as long as board is on ---------------

void loop() {
int sensorValue = digitalRead(2);
Serial.println(sensorValue);
strip.clear();
strip.show();
if (digitalRead(inPin) == HIGH){


  for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(10);  // Pause for a moment
  
  }  
}
if (digitalRead(inPin) == LOW){
  strip.clear();
  strip.show();
 
}
delay(10);
}







Thanks again!

sterretje

You're supposed to tell us what the Serial Monitor tells you ;(

Anyway, your problem is probably caused by the fact that your input pin is floating when the switch is not activated. In that case, it picks up noise from the environment that can result in a HIGH reading.

Add a pull-down resistor (between input pin and gnd). Or change the circuit so you can use the internal pull-up resistor (switch from pin to gnd).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Gayson

The Serial monitor just displays a single "1" at start and then randomly displays 0 or 1 in random intervals.  I'm betting its the pull down resistor I need to add.  Do I just connect a resistor between the ground pin and the input pin (2)?

groundFungus

#5
Sep 18, 2019, 11:14 pm Last Edit: Sep 18, 2019, 11:18 pm by groundFungus
Quote
Do I just connect a resistor between the ground pin and the input pin (2)?
Yes.  But if you connect the switch (NO) to the input and common (C) to ground you can use the aforementioned internal pull up and not need an external resistor.  In setup() set the pinMode() to INPUT_PULLUP to enable the internal pullup.  You will need to change the logic to reflect the pin being HIGH when open (not pressed) and LOW when closed (pressed).


Gayson

Thank You everyone! I just soldered in a resistor cause it was easier but ill remember Input_Pullup for next time! Thanks!

Go Up