I am wanting to write a program that will run a 'for loop' when a momentary button is pressed, but the color of that loop changes if a magnet is present on the reed switch. I am using an arduino nano every, but final project will be uno R3. I am attempting to change two things: 1. the color of the void flash() and colorWipe(), and 2. whether darken() or darken2() run. As of this moment, the loop will start when button is pressed, and strip will run white. No change with reed switch. Also, not sure why, but my while loop appears like it may also be broken now, as I can only start the program once, then I have to unplug and replug the arduino if I want to be able to press the button again. I have pulled how I am changing things from a previous tutorial that changed colors based on button counter, however that strip fill was started in the setup(), whereas I will eventually have different loops depending on which of my multiple buttons is pushed. Hardware has been tested, so all buttons appear to work.
#include <FastLED_NeoPixel.h>
const int DATA_PIN = 6;
const int NUM_LEDS = 20;
const int button = 2;
int status = false;
const int reed = 5;
int reedval = false;
int currentColor = 0;
int darkenColor = 0;
int purple = CRGB::Plum;
int white = CRGB::White;
FastLED_NeoPixel<NUM_LEDS, DATA_PIN, NEO_GRB> strip;
void setup() {
pinMode(button, INPUT);
pinMode(reed, INPUT);
strip.begin();
}
void loop() {
int buttonval = digitalRead(button);
if (buttonval == true) {
status = !status;
checkreed();
colorWipe(strip.Color(currentColor), NUM_LEDS); // white
delay(100);
darkencheck();
flash();
blank(1600);
colorWipe(strip.Color(currentColor), NUM_LEDS); // white
delay(100);
darkencheck();
flash();
blank(1600);
}
while (buttonval == true)
;
delay(50);
}
void checkreed() {
reedval = digitalRead(reed);
if (reedval == HIGH) {
currentColor = white;
} else if (reedval == LOW) {
currentColor = purple;
}
}
void darkencheck() {
reedval = digitalRead(reed);
if (reedval == HIGH) {
darken2();
} else if (reedval == LOW) {
darken();
}
}
void colorWipe(uint32_t color, unsigned long wait) {
for (unsigned int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
void blank(unsigned long wait) {
strip.clear();
strip.show();
delay(wait);
}
void darken() {
//Serial.begin(9600)
uint16_t i, j;
for (j = 255; j > 0; j--) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, j, j, j);
}
strip.show();
}
}
void darken2() {
uint16_t i, j;
for (j = 255; j > 0; j--) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, j, j, j);
}
strip.show();
}
}
void flash() {
strip.fill(currentColor, 0, NUM_LEDS);
strip.show();
delay(125);
darken();
}