Hello, I am trying to make code so that when a button is pressed once it turns on the LED and when pressed again it turns off. Double clicking the button should then change the effect that occurs when the light is turned on. Where in the code it says ////NOT SURE WHAT TO DO HERE/// if i put:
strip.setPixelColor(0, 255, 0, 0);
strip.show();
It works with it tuning red and then off on subsequent presses. But to make it work how intended I am not sure how to do it.
#include "OneButton.h" //we need the OneButton library
#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 2
#define PIXEL_PIN 0
#define PIXEL_COUNT 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
OneButton button(2, true);
int i;
int x;
void setup() {
button.attachDoubleClick(doubleclick); // link the function to be called on a doubleclick event.
button.attachClick(singleclick); // link the function to be called on a singleclick event.
button.attachLongPressStop(longclick); // link the function to be called on a longpress event.
strip.begin();
strip.show();
}
void loop() {
button.tick(); // check the status of the button
delay(10); // a short wait between checking the button
} // loop
void doubleclick() { // what happens when button is double-clicked
i = i + 1;
if ( i > 4 ) {
i = 0;
}
}
void singleclick() { // what happens when the button is clicked
x = x +1;
if (x > 1){
x = 0;
}
switch (x) {
case 0: strip.setPixelColor(0, 0, 0, 0);
strip.show(); // Black/off
break;
case 1: //// Not sure what to do here /////
break;
}
}
void longclick() { // what happens when buton is long-pressed
// Red
}
void lightMode() {
switch (i) {
case 0: rainbowCycle(20);
break;
case 1: strip.setPixelColor(0, 255, 0, 0);
strip.show(); // Red
break;
case 2: strip.setPixelColor(0, 0, 255, 0);
strip.show(); // Green
break;
case 3: strip.setPixelColor(0, 0, 0, 255);
strip.show(); // Blue
break;
}
}
//////////RAINBOW
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
///////////////////
Hopefully this makes sense, and thank you for any help anyone can give in advance.