#include <Adafruit_NeoPixel.h>
#include <Adafruit_MPU6050.h>
#include <math.h>
#include <Wire.h>
#define LED_PIN1 26
#define LED_PIN2 14
#define LED_COUNT 4
char cmd;
int area1 = 4;
float prev_X_axes = 0; // 직전 x 축 값
float prev_Y_axes = 0; // 직전 y 축 값
Adafruit_NeoPixel strip1(LED_COUNT, LED_PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(LED_COUNT, LED_PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_MPU6050 mpu;
void setup() {
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
Serial.begin(9600);
delay(20);
if(!mpu.begin()){
while(1){
delay(20);
}
strip1.begin();
strip2.begin();
}
}
void loop() {
cmd = Serial.read();
sensors_event_t acc, gcc, temp;
mpu.getEvent(&acc, &gcc, &temp);
float X_axes=(gcc.gyro.x)*180/3.14;
float Y_axes=(gcc.gyro.y)*180/3.14;
strip1.clear();
strip2.clear();
// 직전 값 갱신
prev_X_axes = X_axes;
prev_Y_axes = Y_axes;
if(Y_axes>30 and X_axes>30){
for (int pixel= 0; pixel<area1; pixel++){
strip1.setPixelColor(pixel, strip1.Color(0, 255, 0));
strip1.show();
strip2.setPixelColor(pixel, strip2.Color(255, 0, 0));
strip2.show();
}
}
else if(Y_axes>30){
for (int pixel= 0; pixel<area1; pixel++){
strip1.clear();
strip1.show();
strip2.setPixelColor(pixel, strip2.Color(255, 0, 0));
strip2.show();
}
}
else if(X_axes>30){
for (int pixel= 0; pixel<area1; pixel++){
strip2.clear();
strip2.show();
strip1.setPixelColor(pixel, strip1.Color(0, 255, 0));
strip1.show();
}
}
else{
strip1.clear();
strip1.show();
strip2.clear();
strip2.show();
}
}