本文使用TinkerCad Circuits來示範NeoPixel Ring的應用:「水流模擬」
材料(Materials):
- Arduino UNO R3 x 1
- NeoPixel Ring x 1
- Small Breadboard x 1
- 1K ohm Resistor x 2
- Pushbutton x 2
電路(Circuit):
兩顆按鈕分別接在UNO的D3與D4,NeoPixel的In接在UNO的D2。
程式碼(The Code):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#define F_BTN_PIN 3 // forward button | |
#define B_BTN_PIN 4 // backward button | |
#define PIN 2 // input pin Neopixel is attached to | |
#define NUMPIXELS 24 // number of neopixels in Ring | |
#define HALF_NUMPIXELS 12 | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); | |
int delayval = 50; // timing delay | |
int redColor = 255; | |
int dimRedColor = 64; | |
int fBtnState = 0; | |
int bBtnState = 0; | |
void setup() { | |
pixels.begin(); // Initializes the NeoPixel library. | |
Serial.begin(9600); | |
pinMode(F_BTN_PIN, INPUT); | |
pinMode(B_BTN_PIN, INPUT); | |
} | |
void loop() { | |
// read the state of the pushbutton value: | |
fBtnState = digitalRead(F_BTN_PIN); | |
bBtnState = digitalRead(B_BTN_PIN); | |
// check if the pushbutton is pressed. If it is, the buttonState is HIGH: | |
if (fBtnState == HIGH) { | |
// | |
// Light up each LED in clockwise direction | |
// | |
for(int i=0;i<NUMPIXELS;i++){ | |
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 | |
pixels.setPixelColor(i, pixels.Color(redColor, 0, 0)); // Moderately bright green color. | |
pixels.show(); // This sends the updated pixel color to the hardware. | |
delay(delayval); // Delay for a period of time (in milliseconds). | |
} | |
for(int t = 0; t < 20; t++) { | |
for(int i=HALF_NUMPIXELS;i<NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(dimRedColor, 0, 0)); // Moderately bright green color. | |
} | |
for(int i=HALF_NUMPIXELS+1;i<NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(redColor, 0, 0)); // Moderately bright green color. | |
} | |
pixels.show(); | |
delay(delayval); | |
for(int i=HALF_NUMPIXELS+1;i<NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(dimRedColor, 0, 0)); // Moderately bright green color. | |
} | |
for(int i=HALF_NUMPIXELS;i<NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(redColor, 0, 0)); // Moderately bright green color. | |
} | |
pixels.show(); | |
delay(delayval); | |
} | |
for(int i=0;i<NUMPIXELS;i++){ | |
// turn off led | |
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color. | |
pixels.show(); | |
delay(delayval); // Delay for a period of time (in milliseconds). | |
} | |
} else if (bBtnState == HIGH) { | |
// | |
// Light up each LED in counter-clockwise direction | |
// | |
for(int i=NUMPIXELS-1;i>=0;i--){ | |
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 | |
pixels.setPixelColor(i, pixels.Color(redColor, 0, 0)); // Moderately bright green color. | |
pixels.show(); // This sends the updated pixel color to the hardware. | |
delay(delayval); // Delay for a period of time (in milliseconds). | |
} | |
for(int t = 0; t < 20; t++) { | |
for(int i=0; i <= HALF_NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(dimRedColor, 0, 0)); // Moderately bright green color. | |
} | |
for(int i=1; i <= HALF_NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(redColor, 0, 0)); // Moderately bright green color. | |
} | |
pixels.show(); | |
delay(delayval); | |
for(int i=1; i <= HALF_NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(dimRedColor, 0, 0)); // Moderately bright green color. | |
} | |
for(int i=0; i <= HALF_NUMPIXELS;i+=2){ | |
pixels.setPixelColor(i, pixels.Color(redColor, 0, 0)); // Moderately bright green color. | |
} | |
pixels.show(); | |
delay(delayval); | |
} | |
for(int i=NUMPIXELS-1;i>=0;i--){ | |
// turn off led | |
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color. | |
pixels.show(); | |
delay(delayval); // Delay for a period of time (in milliseconds). | |
} | |
} | |
} | |
示範影片(Demo Video):
範例連結(Example Link):
https://www.tinkercad.com/things/9p6iQyMyfO6