Arduino – Neopixel + Variable Resistor + Switch – RGB Select & Set #1
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 |
#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=1; int colors1=0; int colors2=0; int colors3=0; unsigned long cnt=0; void setup() { strip.begin(); strip.show(); pinMode(btnNext, INPUT); } void loop() { if (cnt==0 && digitalRead(btnNext)) { cnt=millis(); states++; if (states > 3) { states=1; } } else if (cnt > 0 && millis() > cnt+500) { cnt=0; } switch (states) { case 1: colors1=map(analogRead(varRes), 0, 1023, 0, 255); break; case 2: colors2=map(analogRead(varRes), 0, 1023, 0, 255); break; case 3: colors3=map(analogRead(varRes), 0, 1023, 0, 255); break; } strip.setPixelColor(0, strip.Color(colors1, 0, 0)); strip.setPixelColor(1, strip.Color(0, colors2, 0)); strip.setPixelColor(2, strip.Color(0, 0, colors3)); for (int i=4;i<strip.numPixels();i++) { strip.setPixelColor(i, strip.Color(colors1, colors2, colors3)); } strip.show(); } |