///////////////////////////////////////////////
//
// Simulation projet E-FORK, M1 ISEN Yncrea
//
// Informations : ESP32
//
// Composants : potentiomètre -
//
// pour I2C - SCL : pin 22 / SDA : pin 21
// SIG : pin 34
//
///////////////////////////////////////////////
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h> // biblio (a ajouter sur library manager)
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// config de l'ecran
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // car pas de broche de reset utilisée
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_MPU6050 mpu;
#define POTENTIOMETER_PIN 34 // sig
const int btnPin = 15;
void setup() {
Serial.begin(115200);
analogReadResolution(12);
pinMode(btnPin, INPUT_PULLUP);
// init de l'écran
if (!display.begin(SSD1306_PAGEADDR, 0x3C)) { // Adresse I2C : 0x3C
Serial.println("Échec de l'initialisation OLED !");
while (true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE); // c'est bo en rose aussi
display.setCursor(0, 0);
display.println("initialisation...");
display.display();
// init du gyro + acc
if (!mpu.begin()) {
Serial.println("MPU6050 non detecte !");
display.clearDisplay();
display.println("MPU6050 non detecte !");
display.display();
while (true);
}
Serial.println("MPU6050 OK !");
display.clearDisplay();
display.println("MPU6050 OK !");
display.display();
delay(1000);
}
sensors_event_t accelEvent, gyroEvent, tempEvent;
void loop() {
// lecture du potentiomètre
int potValue = analogRead(POTENTIOMETER_PIN);
float voltage = (potValue / 4095.0) * 3.3;
// lecture bouton
int btnState = digitalRead(btnPin);
// lecture des infos du MPU6050
mpu.getAccelerometerSensor()->getEvent(&accelEvent);
mpu.getGyroSensor()->getEvent(&gyroEvent);
mpu.getTemperatureSensor()->getEvent(&tempEvent);
// température mesurée
float temperature = tempEvent.temperature;
// affichage
display.clearDisplay();
// Potentiomètre
display.setCursor(0, 0); // en position 0 donc "1ere ligne"
display.print("Pot. : ");
display.print(potValue);
display.print(" (");
display.print(voltage, 2);
display.println("V)");
// bouton
display.setCursor(0, 10); // en position 10 donc "2eme ligne"
display.print("Bouton : ");
display.println(btnState == LOW ? "Appuye" : "Non appuye");
// accélération
display.setCursor(0, 20);
display.print("Accel X:");
display.print(accelEvent.acceleration.x, 2);
display.print(" Y:");
display.print(accelEvent.acceleration.y, 2);
display.print(" Z:");
display.println(accelEvent.acceleration.z, 2);
// gyro
display.setCursor(0, 40);
display.print("Gyro X:");
display.print(gyroEvent.gyro.x, 2);
display.print(" Y:");
display.print(gyroEvent.gyro.y, 2);
display.print(" Z:");
display.println(gyroEvent.gyro.z, 2);
// température
display.setCursor(0, 55);
display.print("Temp.: ");
display.print(temperature, 2);
display.println(" C");
// maj de l'ecran
display.display();
// affichage dans le moniteur série
Serial.println("=== Donnees ===");
Serial.print("Potentiometre : ");
Serial.print(potValue);
Serial.print(" | Voltage : ");
Serial.print(voltage, 2);
Serial.println(" V");
Serial.print("Bouton : ");
Serial.println(btnState == LOW ? "Appuye" : "Non appuye");
Serial.print("Accel - X:");
Serial.print(accelEvent.acceleration.x);
Serial.print(" Y:");
Serial.print(accelEvent.acceleration.y);
Serial.print(" Z:");
Serial.println(accelEvent.acceleration.z);
Serial.print("Gyro - X:");
Serial.print(gyroEvent.gyro.x);
Serial.print(" Y:");
Serial.print(gyroEvent.gyro.y);
Serial.print(" Z:");
Serial.println(gyroEvent.gyro.z);
// attention, il ne s'agit que de la temp du capteur, faudra rajouter un autre sensor pour la temp ambiante au besoin
Serial.print("Temperature : ");
Serial.print(temperature, 2);
Serial.println(" C");
delay(500);
}