BY BENNA SU
Here is the video demo!
The checkButton() function keeps working to check if the current state of button is different from the looping before. If it is different, it triggers the flipfunction below and allow the Led function move from LOW to HIGH, or HIGH to LOW.
Here is the Code that I wrote for the demo.
#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;
int LedPin = 32;
int pot1 = 0;
int pot2 = 0;
int pot3 = 0;
int map1 = 0;
int map2 = 0;
int map3 = 0;
bool lastButton1State = LOW;
bool button1State = LOW;
bool switchedOn = false;
void setup() {
pinMode(LedPin, OUTPUT);
pinMode(buttonPin, INPUT);
neopixel.begin();
neopixel.clear();
neopixel.show();
}
void loop() {
checkButton();
updateLed();
checkpot1();
checkpot2();
checkpot3();
}
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;
}
}
void updateLed() {
if (switchedOn == true) {
neopixel.setPixelColor(0, pot1, pot2, pot3);
neopixel.setPixelColor(1, pot1, pot2, pot3);
neopixel.show();
} else {
neopixel.setPixelColor(0, 0, 0, 0);
neopixel.setPixelColor(1, 0, 0, 0);
neopixel.show();
}
}
void checkpot1() {
pot1 = analogRead(A14);
map1 = map(pot1, 0, 1023, 0, 255);
Serial.println(map1);
}
void checkpot2() {
pot2 = analogRead(A15);
map2 = map(pot2, 0, 1023, 0, 255);
Serial.println(map2);
}
void checkpot3() {
pot3 = analogRead(A16);
map3 = map(pot3, 0, 1023, 0, 255);
Serial.println(map3);
}