#include "MPU6050.h" // подключение библиотеки
#include <LiquidCrystal_I2C.h>
MPU6050 mpu; //датчик
int16_t ax, ay, az;
enum AxelNumToLett {
X_axel = 0,
Y_axel = 1,
Z_axel = 2
};
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const char text[11] = {132, 72, 75, 134, 132, 72, 79, 77, 69, 84, 80};
const byte symbol[8] = // ь
{
B00000,
B00000,
B10000,
B10000,
B11110,
B10001,
B10001,
B11110,
};
float RunnigMid(float incline, byte axel){
static byte index = 0;
static float angleArr[3][10];
angleArr[axel][index] = incline;
index++;
if(index >= 10){
index = 0;
}
float mid[3] = {0, 0, 0};
for(int i = 0; i < 10; i++){
mid[axel] += angleArr[axel][i];
}
return (float)mid[axel]/10;
}
void PrintAxel(float axel, char mark, byte index){
lcd.setCursor(0, index + 1);
lcd.print(" ");
lcd.setCursor(0, index + 1);
lcd.print("Oc"); lcd.write(byte(0)); lcd.print(" ");
lcd.print(mark); lcd.print(": ");
lcd.print(axel); lcd.write(176);
}
void setup()
{
Wire.begin();
Serial.begin(9600);
mpu.initialize();
Serial.println(mpu.testConnection() ? "MPU6050 OK" : "MPU6050 FAIL"); // состояние соединения
lcd.init();
lcd.backlight();
lcd.createChar(0, symbol);
lcd.setCursor(4, 0);
for(int i = 0; i < 11; i++){
lcd.write(text[i]);
}
delay(1000);
}
void loop()
{
static float axels[3] = {0.001, 0.001, 0.001};
const char axelsLetter[3] = {'X', 'Y', 'Z'};
mpu.getAcceleration(&ax, &ay, &az);
if(axels[X_axel] != RunnigMid((float)ax/32768*2, X_axel)){
axels[X_axel] = RunnigMid((float)ax/32768*2, X_axel);
PrintAxel(axels[X_axel], axelsLetter[X_axel], X_axel);
}
if(axels[Y_axel] != RunnigMid((float)ay/32768*2, Y_axel)){
axels[Y_axel] = RunnigMid((float)ay/32768*2, Y_axel);
PrintAxel(axels[Y_axel], axelsLetter[Y_axel], Y_axel);
}
if(axels[Z_axel] != RunnigMid((float)az/32768*2, Z_axel)){
axels[Z_axel] = RunnigMid((float)az/32768*2, Z_axel);
PrintAxel(axels[Z_axel], axelsLetter[Z_axel], Z_axel);
}
delay(100);
}