Arduino – Neopixel + Variable Resistor + Switch – RGB Select & Set #2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#include <Adafruit_NeoPixel.h> #define PIN 2 Adafruit_NeoPixel strip=Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800); int varRes=A1; int btnNext=4; int states=0; int colors1=0; int colors2=0; int colors3=0; int vars=0; unsigned long cnt=0; void setup() { strip.begin(); strip.show(); pinMode(btnNext, INPUT); } void loop() { if (cnt==0 && digitalRead(btnNext)) { cnt=millis(); switch (states) { case 1: colors1=vars; break; case 2: colors2=vars; break; case 3: colors3=vars; break; } states++; if (states > 3) { states=0; } for (int i=0;i<strip.numPixels();i++) { strip.setPixelColor(i, strip.Color(0, 0, 0)); } strip.show(); } else if (cnt > 0 && millis() > cnt+500) { cnt=0; } vars=map(analogRead(varRes), 0, 1023, 0, 255); switch (states) { case 0: for (int i=0;i<strip.numPixels();i++) { strip.setPixelColor(i, strip.Color(colors1, colors2, colors3)); } break; case 1: strip.setPixelColor(0, strip.Color(255, 0, 0)); strip.setPixelColor(2, strip.Color(vars, 0, 0)); for (int i=4;i<strip.numPixels();i++) { strip.setPixelColor(i, strip.Color(vars, colors2, colors3)); } break; case 2: strip.setPixelColor(0, strip.Color(0, 255, 0)); strip.setPixelColor(2, strip.Color(0, vars, 0)); for (int i=4;i<strip.numPixels();i++) { strip.setPixelColor(i, strip.Color(colors1, vars, colors3)); } break; case 3: strip.setPixelColor(0, strip.Color(0, 0, 255)); strip.setPixelColor(2, strip.Color(0, 0, 255)); for (int i=4;i<strip.numPixels();i++) { strip.setPixelColor(i, strip.Color(colors1, colors2, vars)); } break; } strip.show(); } |