Hi, pretty new to the Arduino stuff and I need some help. Been fumbling around for a while trying to figure out how to code a way to make a 2 position toggle switch turn on and off a neopixel strip when it's switched. I only need on and off and for them to be rainbow only. I have nothing to show because nothing I've made up has come even close to working.
Are you talking about hardware or software switch?
This is the code I've used to make the rainbow work, as for switch I've tried several and none have worked at all.
#include <Adafruit_NeoPixel.h>
#define LED_COUNT 6
#define LED_PIN PIN_NEOPIXEL
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show();
strip.setBrightness(50);
}
void loop() {
rainbow(10);
}
void rainbow(int wait) {
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) {
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show();
delay(wait);
}
}
Right now I'm using a 5v USB power supply and a hardware toggle switch. Neos into Ground, 5v and pin 6. Switch is in ground and pin 4
What kind of switch? Could you show one as example?
It's just a normal physical toggle switch with 2 positions and 2 posts underneath. I have 60 LED's I'm trying to make this work with.
So you're still talking about a physical switch?
How can a physical switch not work?
Please show your schematic
I may not have explained too well what I'm trying to do with the switch. I want the power supply to continually power the Arduino, but the switch only turns the pixels on and off, if that makes more sense.
Right now I have the power supply running to the USB port, the neopixel are wired to the 5v pin, Ground, and number 6 pin. The switch I had in pin 4 and ground.
I'm using an uno rev3 if that helps
Why?
Assuming the strip stops working with its 5V disconnected from Arduino 5V then that's where the switch should go.
Well this is not what you said about how the switch is wired up.
Assuming you have a push button switch on pin 4 between input and ground. Then this code will turn on or off your rainbow effect when you push it.
#include <Adafruit_NeoPixel.h>
#define LED_COUNT 60
#define LED_PIN 6
uint8_t changePin = 4;
bool commuted = false;
uint8_t patternCount = 0; // initial pattern value
uint8_t patternCountLimit = 2; // how many patterns are they
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// Serial.begin(250000); // for debug printing
pinMode(changePin, INPUT_PULLUP); // for change pin wired between input and ground
strip.begin();
strip.show();
strip.setBrightness(50);
}
void loop() {
if (patternCount == 0) rainbow(10);
if (patternCount == 1) commutableDelayPin(200, changePin, LOW);
commutableCatchPin(changePin, LOW);
}
void rainbow(int wait) {
int pixelHue;
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
for(int i=0; i<strip.numPixels(); i++) {
pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
}
strip.show();
if(commuted) firstPixelHue = 5*65536; // commutate outer loop
//delay(wait);
commutableDelayPin(wait, changePin, LOW);
}
}
void commutableDelayPin(uint32_t delayTime, uint8_t changePin, bool commuteState){ // this will return when the delay time has expired or the change button is pushed.
uint32_t startTime = millis(); // the millis count when we start;
// now hold until either time is up or commute pin is pressed
while((millis() - startTime < delayTime) && (commuted != true)) { // keep the loop going until time is up or delay is commutated
if ( digitalRead(changePin) == commuteState){ // if you see the button being held down abandon the delay
commuted = true;
}
}
}
void commutableCatchPin(uint8_t changePin, bool commuteState) { // check to see if the delay has been commuted and take action if it has been
if(commuted){ // we have had a commute state deal with it
commuted = false; // reset the global flag
//Serial.println("button pressed");
while(digitalRead(changePin) == commuteState) {
delay(50); // do nothing until button is released
}
//Serial.println("button released");
delay(200); // allow any button bouncing to stop
// now take action for when a delay has been commutated
// this could be to wipe an addressable LED string or to turn off the LED
if(patternCount == 0) {
for(int i = 0; i<strip.numPixels() ; i++) {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
}
// move on the pattern to produce
patternCount += 1 ; // move onto next pattern
if(patternCount >= patternCountLimit) {
patternCount = 0; // wrap round patternCount
}
//Serial.print("count on button ");Serial.println(count);
} // end of dealing with a commute
}
This will explain what is going on.
commutable delay
@darktooth777 Can you provide some feedback please. Does this code do what you wanted?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.