Aidan Singh
Description of On-Off Button:
The on-off button uses two functions.
The first, checkButton(), detects the moment the button goes from unpressed to pressed. If this is the case, it activates the second function.
The second function is flipButtonState(). When used, it flips the state of an on-off boolean. For example, if the boolean is false before the function runs, it will become true after. If it was true first, it will become false.
The boolean controls whether or not the LED’s are off or updated by the pots.
#include <Adafruit_NeoPixel.h>
// replace the 32 below with whatever pin your Neopixel is connected to
Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(2, 32, NEO_RGB);
int buttonPin = 36;
bool lastButton1State = LOW;
bool button1State = LOW;
bool switchedOn = false;
int tempo = 100;
int red = 0;
int green = 0;
int blue = 0;
int potPins[3] = {A14, A15, A16};
void setup() {
Serial.begin(9600);
neopixel.begin();
neopixel.clear();
neopixel.show();
pinMode(buttonPin, INPUT);
}
void loop() {
checkButton();
changeColor();
}
void changeColor() {
red = map(analogRead(potPins[0]), 0, 1023, 0, 100);
green = map(analogRead(potPins[1]), 0, 1023, 0, 100);
blue = map(analogRead(potPins[2]), 0, 1023, 0, 100);
Serial.println(red);
if(switchedOn == true) {
neopixel.setPixelColor(0, red, green, blue);
neopixel.setPixelColor(1, green, blue, red);
} else {
neopixel.setPixelColor(0, 0, 0, 0);
neopixel.setPixelColor(1, 0, 0, 0);
}
neopixel.show();
delay(tempo);
}
void checkButton() {
lastButton1State = button1State;
button1State = digitalRead(buttonPin);
if(lastButton1State == LOW && button1State == HIGH) {
flipButtonState();
delay(5);
} else if(lastButton1State == HIGH && button1State == LOW) {
delay(5);
}
}
void flipButtonState() {
if(switchedOn == true) {
switchedOn = false;
} else if(switchedOn == false) {
switchedOn = true;
}
}