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!
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.
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
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 ?
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.