BASIC4MCU | 질문게시판 | 네오픽셀 rainbowCycle 질문입니다.
페이지 정보
작성자 순간집착제 작성일2020-09-22 15:42 조회10,109회 댓글2건본문
안녕하세요.. 네오픽셀 rainbowCycle 에 대해서 궁금해서 문의 드립니다.
아래 코드는 네오픽셀 예제 파일에서 조금 수정한 건데요... 버튼 누를때 마다 switch 문이 실행 되는데
지금 코드에서는 5번 반복이 완료되기전에는 버튼을 눌러도 break 실행이 안되더라구요...
제가 원하는건 rainbowCycle 에서 5번 반복하는 중에 버튼을 누르면 바로 break 로 빠져 나가고 싶습니다.
어떻게 수정해야 할까요?
#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 3 // Digital IO pin connected to the button. This will be
// driven with a pull-up resistor so the switch should
// pull the pin to ground momentarily. On a high -> low
// transition the button press logic will execute.
#define PIXEL_PIN 2 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 24
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
bool oldState = HIGH;
int showType = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);
// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) {
showType++;
if (showType > 5)
showType=0;
startShow(showType);
}
}
// Set the last button state to the old state.
oldState = newState;
}
void startShow(int i) {
switch(i){
case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off
break;
case 1: colorWipe(strip.Color(127, 127, 127), 50); // White
break;
case 2: colorWipe(strip.Color(255, 0, 0), 50); // Red
break;
case 3: colorWipe(strip.Color(0, 255, 0), 50); // Green
break;
case 4: colorWipe(strip.Color(0, 0, 255), 50); // Blue
break;
case 5: rainbowCycle(20);
break;
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) {
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);
}
}
댓글 2
조회수 10,109master님의 댓글
master 작성일
if (newState == LOW) {
if (++showType > 5)showType=0;
startShow(showType);
if(digitalRead(BUTTON_PIN)==LOW)return; // <-- 추가하세요
}
master님의 댓글
master 작성일
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, c);
strip.show();
if(digitalRead(BUTTON_PIN)==LOW)return; // <-- 추가하세요
delay(wait);
}
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) {
for(i=0; i< strip.numPixels(); i++) { strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); }
strip.show();
if(digitalRead(BUTTON_PIN)==LOW)return; // <-- 추가하세요
delay(wait);
}
}