예전에 네오픽셀로 조이스틱을 제어하는 것과 관련해 질문 올렸었는데요.
도움을 주신덕에 완성했는데 문제가 생겨서 다시 질문드립니다ㅜ
문제가
조이스틱을 위아래로 꺾으면 값이 바로 LED에 반영되어야 하는데
꺾은 상태로 버튼을 눌러줘야 LED가 바뀌네요...ㅎ
LED에 조이스틱 x,y값이 바로 반영되게 하려면 어떻게 해야하나요?
----------------------------------------------------------------------------------------
#include <Adafruit_NeoPixel.h>
#define PIXEL_COUNT 8
#define BUTTON_PIN 2
#define PIXEL_PIN 13
int xpin = A0;
int ypin = A1;
int xval = 0;
int yval = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, 13, NEO_GRB + NEO_KHZ800);
bool oldState = HIGH;
int showType = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
xval = analogRead(A0);
yval = analogRead(A1);
Serial.print("xval");
Serial.print(xval); //시리얼 모니터에 'X: x값'을 표기
Serial.print(" yval");
Serial.println(yval);
// 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 > 7)
showType=0;
startShow(showType);
}
}
Serial.println(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(100, 255, 0), 50); // Yellow
break;
case 4: colorWipe(strip.Color(0, 255, 0), 50); // Green
break;
case 5: colorWipe(strip.Color(0, 100, 255), 50); // Cyan
break;
case 6: colorWipe(strip.Color(0, 0, 255), 50); // Blue
break;
case 7: colorWipe(strip.Color(70, 0, 255), 50); // Magenta
break;
}
}
void colorWipe(uint32_t c, uint8_t wait) {
if (xval<5) {
if (yval<5) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, c);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=5 && yval<500) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=500 && yval<550) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, c);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=550 && yval<1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, c);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
}
}
if (xval>=5 && xval<500) {
if (yval<5) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=5 && yval<500) {
strip.setPixelColor(0, c);
strip.setPixelColor(1, c);
strip.setPixelColor(2, c);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=500 && yval<550) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, c);
strip.setPixelColor(2, c);
strip.setPixelColor(3, c);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=550 && yval<1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, c);
strip.setPixelColor(3, c);
strip.setPixelColor(4, c);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
}
}
if (xval>=500 && xval<550) {
if (yval<5) {
strip.setPixelColor(0, c);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=5 && yval<500) {
strip.setPixelColor(0, c);
strip.setPixelColor(1, c);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=500 && yval<550) {
strip.setPixelColor(0, c);
strip.setPixelColor(1, c);
strip.setPixelColor(2, c);
strip.setPixelColor(3, c);
strip.setPixelColor(4, c);
strip.setPixelColor(5, c);
strip.setPixelColor(6, c);
strip.setPixelColor(7, c);
strip.show();
delay(wait);
} else if (yval>=550 && yval<1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, c);
strip.setPixelColor(4, c);
strip.setPixelColor(5, c);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, c);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
}
}
if (xval>=550 && xval<1020) {
if (yval<5) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=5 && yval<500) {
strip.setPixelColor(0, c);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, c);
strip.setPixelColor(7, c);
strip.show();
delay(wait);
} else if (yval>=500 && yval<550) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, c);
strip.setPixelColor(6, c);
strip.setPixelColor(7, c);
strip.show();
delay(wait);
} else if (xval>=550 && xval<1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, c);
strip.setPixelColor(5, c);
strip.setPixelColor(6, c);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
}
}
if (xval>=1020) {
if (yval<5) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, c);
strip.show();
delay(wait);
} else if (yval>=5 && yval<500) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=500 && yval<550) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, c);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=550 && yval<1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, c);
strip.setPixelColor(4, c);
strip.setPixelColor(5, c);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
} else if (yval>=1020) {
strip.setPixelColor(0, 0,0,0);
strip.setPixelColor(1, 0,0,0);
strip.setPixelColor(2, 0,0,0);
strip.setPixelColor(3, 0,0,0);
strip.setPixelColor(4, 0,0,0);
strip.setPixelColor(5, 0,0,0);
strip.setPixelColor(6, 0,0,0);
strip.setPixelColor(7, 0,0,0);
strip.show();
delay(wait);
}
}
}
|