Switching between 2 commands with a toggle switch

Hey all, I'm trying to make a lamp, but I'm having trouble figuring out how to toggle between 2 commands with a toggle switch. I want the LEDs to rainbow cycle on one side and able to be controlled with potentiometers on the other. The code for both commands work fine on their own but don't switch when put together in an if command, any help would be great, thanks!

#include <Adafruit_NeoPixel.h>
#define LED_COUNT 60
#define LED_PIN 6
uint8_t changePin = 2;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_BGRW + NEO_KHZ800) ;
uint8_t patternCount = 0;
#define p1 analogRead(1)
#define p2 analogRead(0)

int pot1;
int pot2;


void setup() {
  strip.begin();
  strip.show();
  Serial.begin(115200);
 pinMode(changePin, INPUT_PULLUP);
}

void loop() {
  if (patternCount == 0) potentiometer(0);
  if (patternCount == 1) delay(10); rainbow(50);

  strip.show();
  pot1=p1;
  pot2=p2;
}

void setPixel(int pixelz, int colorz, long unsigned fadez) {
  colorz%=1024;
  if(fadez>255){
    fadez=511-fadez;
}  
  fadez*=fadez;
  fadez/=255;
  strip.setPixelColor(pixelz,strip.ColorHSV(colorz*64,255,fadez));
}

void rainbow(uint16_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

void potentiometer(uint16_t wait) {
  uint16_t i,j;
  for(int a=0;a<LED_COUNT;a++){
    setPixel(a,pot1,pot2/4);
  }
}
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);
}


Whereabouts in your sketch do you read the state of the toggle switch ?

You intialized patternCount to 0 and never change it. You also never read changePin.

How's this? Still not working but I think it's reading the switch now

#include <Adafruit_NeoPixel.h>
#define LED_COUNT 60
#define LED_PIN 6
int changePin = 2;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_BGRW + NEO_KHZ800) ;
#define p1 analogRead(1)
#define p2 analogRead(0)

int pot1;
int pot2;


void setup() {
  strip.begin();
  strip.show();
  Serial.begin(115200);
 pinMode(2, INPUT);
 pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if (changePin = LOW) potentiometer(20);
  if (changePin = HIGH) rainbow(50);

  strip.show();
    if (changePin = LOW) potentiometer(20);
    if (changePin = HIGH) rainbow(50);

changepin will always equal 2 so it will never be HIGH or LOW. You need to do a digitalRead() if changepin to get its state and in any case you need to use == for comparison

Woyncha use your variable here instead of the literal 2 to which it is equal?

And you might prefer to use INPUT_PULLUP mode, with your switch wired between the input pin and ground, just anticipating the next thing that will throw you a curve.

a7

Ok, I've gotten some progress. If I start the code while to switch is LOW, it will be controlled by the potentiometers, then when I switch to HIGH, the rainbow sequence will play. However, switching from HIGH back to LOW, will have it stay on rainbow, and won't go back to the potentiometers. Is it possible to pause the rainbow sequence while the switch is on LOW? Anyways, here's the new code

#include <Adafruit_NeoPixel.h>
#define LED_COUNT 60
#define LED_PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_BGRW + NEO_KHZ800) ;
const int changePin = 2;
int switch_state;
#define p1 analogRead(1)
#define p2 analogRead(0)

int pot1;
int pot2;


void setup() {
  strip.begin();
  strip.show();
  Serial.begin(115200);
 pinMode(changePin, INPUT_PULLUP);
 pinMode(LED_PIN, OUTPUT);
 digitalRead(changePin);
}

void loop() {
 switch_state = digitalRead(changePin);
  if (switch_state == LOW) potentiometer(20);
  if (switch_state == HIGH) rainbow(50);

  strip.show();
  pot1=p1;
  pot2=p2;
}

Please post your full sketch

#include <Adafruit_NeoPixel.h>
#define LED_COUNT 60
#define LED_PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_BGRW + NEO_KHZ800) ;
const int changePin = 2;
int switch_state;
#define p1 analogRead(1)
#define p2 analogRead(0)

int pot1;
int pot2;


void setup() {
  strip.begin();
  strip.show();
  Serial.begin(115200);
 pinMode(changePin, INPUT_PULLUP);
 pinMode(LED_PIN, OUTPUT);
 digitalRead(changePin);
}

void loop() {
 switch_state = digitalRead(changePin);
  if (switch_state == LOW) potentiometer(20);
  if (switch_state == HIGH) rainbow(50);

  strip.show();
  pot1=p1;
  pot2=p2;
}

void setPixel(int pixelz, int colorz, long unsigned fadez) {
  colorz%=1024;
  if(fadez>255){
    fadez=511-fadez;
}  
  fadez*=fadez;
  fadez/=255;
  strip.setPixelColor(pixelz,strip.ColorHSV(colorz*64,255,fadez));
}

void rainbow(uint16_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

void potentiometer(uint8_t wait) {
  uint16_t i,j;
  for(int a=0;a<LED_COUNT;a++){
    setPixel(a,pot1,pot2/4);
  }
}
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);
}

The rainbow() function takes over 12 seconds to run and the state of changePin is not read during that time. Is that what you are describing or does the rainbow effect never end even after 12 seconds ?

Ok it switches back to potentiometers just with a delay. Thanks for the help from you all!

In general, it is certainly possible to have a faster response to the input which is your toggle switch. Here to suspender one effect and begin immediately running the other. As well as having the potentialometers change as the effect runs, at a glance it appears that now they do not.

And again, in general, it requires rethinking how both effects are implemented, and is probably one of the top twenty questions on these fora.

I can't now, but say if that intrigues you, and you will get plenty of advices on the methods that can be used to do.

a7