I solved the problem with my else if statement (it was an extraneous semi-colon) but left the descriptions of the problem, having typed them out already, on the off chance that it helps someone solve some problem in the future. Thanks for your help (will use serial monitor for debugging in the future instead of LEDs!)
UKHeliBob:
What do you see if you print the values of patternSwitchState and whiteSwitchState immediately before testing their values ?
HINT : print text labels before the values so that you know which value you are seeing and a linefeed after each pair of values to make interpretation easier
Not part of the problem but
if (digitalRead(whiteSwitchPin) == HIGH)
{
whiteSwitchState = 1;
}
else
{
whiteSwitchState = 0;
}
can be simplified to
whiteSwitchState = digitalRead(whiteSwitchPin);
digitalWrite(potentiometerPinPlus1, LOW); // leave this low to give a ground pin conveniently next to the potentiometer input pin
digitalWrite(potentiometerPinMinus1, HIGH); // leave this high to give a 5 V pin conveniently next to the potentiometer input pin
Why not simply wire the pot to 5V and GND ?
It's nice to be able to just shove a potentiometer into my breadboard at the right location without using jumpers and I don't need all the pins on the Arduino right now. When I have got everything working right, I will use soldered wires.
I have modified the code to print the states of patternSwitchState and whiteSwitchState to the serial monitor and the switch states printed to the serial monitor match the phyisical reality of the switches on my breadboard. However...
With patternSwitchState at 0 and whiteSwitchState at 1, I expect to get solidColoursRGBW (in the NEO_RGBW format) and nothing else. Instead I get solidColoursRGBW (good) followed by whiteOverRainbow (bad! naughty!), both in the NEO_RGBW format (good)!
With patternSwitchState at 1 and whiteSwitchState at 1, I expect to get whiteOverRain in the NEO_RGBW format and nothing else and that's exactly what happens!
With patternSwitchState at 0 and whiteSwitchState at 0, I expect to get solidColoursRGB (in the NEO_GRB format) and nothing else. Instead I get solidColoursRGB (good) followed by what I think is an attempt at whiteOverRainbow (bad; in the NEO_GRB format).
With patternSwitchState at 1 and whiteSwitchState at 0, I expect to get rainbowCycle (in the NEO_GRB format) and nothing else. Instead I get rainbowCycle (good) followed by what I think is an attempt at whiteOverRainbow (bad; in the NEO_GRB format).
Setup and loop are now:
void setup() {
pinMode(patternSwitchPin, INPUT_PULLUP);
pinMode(potentiometerPin, INPUT_PULLUP);
pinMode(potentiometerPinPlus1, OUTPUT);
pinMode(potentiometerPinMinus1, OUTPUT);
pinMode(whiteSwitchPin, INPUT_PULLUP);
digitalWrite(potentiometerPinPlus1, LOW); // leave this low to give a ground pin conveniently next to the potentiometer input pin
digitalWrite(potentiometerPinMinus1, HIGH); // leave this high to give a 5 V pin conveniently next to the potentiometer input pin
Serial.begin(9600);
whiteSwitchState = digitalRead(whiteSwitchPin);
Serial.print("Setup whiteSwitchState: ");
Serial.println(whiteSwitchState);
strip = new Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, (whiteSwitchState ? NEO_RGBW : NEO_GRB) + NEO_KHZ800);
strip->begin();
strip->show(); // Initialize all pixels to 'off'
strip->setBrightness(2); // Set BRIGHTNESS to about 1/5 (max = 255)
}
void loop() {
NUM_RAINBOW_LEDS = map(analogRead(potentiometerPin), 0, 1023, 1, 60);
patternSwitchState = digitalRead(patternSwitchPin);
Serial.print("whiteSwitchState: ");
Serial.println(whiteSwitchState);
Serial.print("patternSwitchState: ");
Serial.println(patternSwitchState);
if ((patternSwitchState == 0) && (whiteSwitchState == 0)) {
solidColoursRGB();
} else if ((patternSwitchState == 1) && (whiteSwitchState == 0)) {
rainbowCycle(2);
} else if ((patternSwitchState == 0) && (whiteSwitchState == 1)) {
solidColoursRGBW();
} else if ((patternSwitchState == 1) && (whiteSwitchState == 1))[color=red][b];[/b][/color] {
whiteOverRainbow(20, 75, (NUM_RAINBOW_LEDS / 8 + 1));
}
}
The problem was the semi-colon that I've highlighted in red, at the end of the last else if line. Edit: lol. I didn't highlight it in red but you can see where I tried!
CrossRoads:
This needs to be in loop(), otherwise it is only read once in setup(), then never again
if (digitalRead(whiteSwitchPin) == HIGH) {
whiteSwitchState = 1;
} else {
whiteSwitchState = 0;
}
I'm not sure I can do that because
strip = new Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, (whiteSwitchState ? NEO_GRB : NEO_RGBW) + NEO_KHZ800);
needs to be set up in the setup section of the code after which I think it's not possible to change without resetting the Arduino. Do you think it should be possible to change the setting from NEO_GRB to NEO_RGBW without resetting the Arduino?
Deva_Rishi:
what is the purpose of this ?
strip->show();
delay(250);
strip->show();
delay(250);
strip->show();
delay(250);
strip->show();
delay(250);
strip->show();
delay(250);
strip->show();
delay(250);
strip->show();
delay(250);
strip->show();
delay(250);
The reason for this is that I want to use pogo pins to test boards without soldering them first. I want the board to change colours every two seconds so I can check that each LED on the board lights up in each colour. If I do strip->show; delay(2000); then I have to wait up to two seconds after pressing the board on the pogo pins to find out if I have aligned the board correctly on the pogo pins. By repeating strip->show(); every quarter second, I don't have to wait very long to find out whether I'm holding the board properly against the pogo pins. I realise I could make it tidier using a for loop though.