// LCD
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// Gyroscope
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
// RTC
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Push button
volatile byte state = LOW;
int ButtonPin = 14;
const int timeThreshold = 150; //Temps entre interruptions pour eviter bounce
volatile int ISRCounter = 0;
int counter = 0;
long startTime = 0;
void setup() {
Serial.begin(115200);
// LCD Init
lcd.init();
lcd.backlight();
// Gyro Init
while (!mpu.begin(0x69))
{
Serial.println("MPU6050 not connected!");
delay(1000);
}
Serial.println("MPU6050 ready!");
// RTC Init
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
// Push button Init
pinMode(ButtonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ButtonPin), debounce, FALLING);
}
// Declare sensor event to get readings
sensors_event_t event;
void loop() {
//We enter the push button condition
if (counter != ISRCounter)
{
counter = ISRCounter;
state = !state; //Inverser etat du LED
lcd.clear();
}
if(state==LOW)
{
printRTC();
}
else
{
printAccelerometer();
}
//delay(10); // this speeds up the simulation
}
void debounce() //Interrupt function to treat push button
{
if (millis() - startTime > timeThreshold)
{
ISRCounter++;
startTime = millis();
}
}
void printGyro()
{
mpu.getAccelerometerSensor()->getEvent(&event);
lcd.setCursor(0, 0);
lcd.print("mpu6050 gyro");
lcd.setCursor(1, 1);
lcd.print("X: ");
lcd.setCursor(4, 1);
lcd.print(event.accelerometer.x);
lcd.setCursor(1, 2);
lcd.print("Y: ");
lcd.setCursor(4, 2);
lcd.print(event.accelerometer.y);
lcd.setCursor(1, 3);
lcd.print("Z: ");
lcd.setCursor(4, 3);
lcd.print(event.accelerometer.z);
//delay(10); // this speeds up the simulation
}
void printRTC()
{
// Print RTC data
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("RTC date");
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.setCursor(6, 1);
lcd.print(now.year(), DEC);
lcd.setCursor(10, 1);
lcd.print("/");
lcd.setCursor(11, 1);
lcd.print(now.month(), DEC);
lcd.setCursor(13, 1);
lcd.print("/");
lcd.setCursor(14, 1);
lcd.print(now.day(), DEC);
lcd.setCursor(0, 2);
lcd.print("Day: ");
lcd.setCursor(6, 2);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(0, 3);
lcd.print("Time: ");
lcd.setCursor(6, 3);
lcd.print(now.hour(), DEC);
lcd.setCursor(8, 3);
lcd.print(":");
lcd.setCursor(9, 3);
lcd.print(now.minute(), DEC);
//lcd.setCursor(11, 3);
//lcd.print(":");
//lcd.setCursor(12, 3);
//lcd.print(now.second(), DEC);
}