/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_MPU6050 mpu;
int x = 15;
int y = 55;
int pot;
int convert;
void setup() {
pinMode(A0, INPUT);
tft.begin();
// Serial.begin(9600);
Serial.begin(115200);
while (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
tft.drawRoundRect(6, 16, 50, 50, 25, ILI9341_WHITE);
// tft.drawLine(15, 55, 30, 40, ILI9341_RED);//line 1
// tft.drawLine(10, 40, 30, 40, ILI9341_RED);//line 2
// tft.drawLine(15, 25, 30, 40, ILI9341_RED);//line 3
// tft.drawLine(30, 20, 30, 40, ILI9341_RED);//line 4
// tft.drawLine(45, 25, 30, 40, ILI9341_RED);//line 5
// tft.drawLine(50, 40, 30, 40, ILI9341_RED);//line 6
// tft.drawLine(45, 55, 30, 40, ILI9341_RED);//line 7
// Meme reference: https://english.stackexchange.com/questions/20356/origin-of-i-can-haz
}
sensors_event_t event;
void loop() {
sensors_event_t a, g, temp;
mpu.getGyroSensor()->getEvent(&event);
mpu.getAccelerometerSensor()->getEvent(&event);
convert = event.acceleration.z * 3600 / 1000;
pot = map(convert, 0, 70.596, 1, 7);
MeterRefresh();
Serial.print(convert);
Serial.print(" ");
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.print("[");
Serial.print(millis());
Serial.print("] X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(event.gyro.y);
Serial.print(", Z: ");
Serial.print(event.gyro.z);
Serial.println(" m/s^2");
tft.drawLine(x, y, 30, 40, ILI9341_WHITE);
tft.setCursor(70, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.println(convert);
tft.setCursor(90, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(1);
tft.println("KM/h");
delay(100);
tft.drawLine(x, y, 30, 40, ILI9341_BLACK);
tft.setCursor(70, 20);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(1);
tft.println(convert);
delay(10);
}
void MeterRefresh() {
if(pot == 1) {
x = 15;
y = 55;
} else if(pot == 2) {
x = 10;
y = 40;
} else if(pot == 3) {
x = 15;
y = 25;
} else if(pot == 4) {
x = 30;
y = 20;
} else if(pot == 5) {
x = 45;
y = 25;
} else if(pot == 6) {
x = 50;
y = 40;
} else if(pot == 7) {
x = 45;
y = 55;
}
}