#include <Wire.h>
#include <rgb_lcd.h>
rgb_lcd lcd;
int etape = 0; // etape actuelle
int points[4] = {0, 0, 0, 0}; // les points
float moyenne_points[2] = {0, 0}; // la moyenne des points
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setRGB(128, 128, 0);
}
void loop() {
if (etape == 0) { // premiere etape donc x1
lcd.setCursor(0, 0);
lcd.print("Entrer x1:");
if (Serial.available()) {
String inputString = ""; // clear la valeur pour quand le void loop recommence
while (Serial.available()) {
char inputChar = Serial.read();
if (inputChar == '\n') { // verifie si la valeur n est que un saut à la ligne
break; // si oui, le void loop recommencera car aucune valeur ajouté dans inputString
}
inputString += inputChar;
}
if (inputString.length() > 0) {
int inputValue = inputString.toInt(); // convertie en int
points[0] = inputValue; // attribue la valeur au point x1
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("x1: ");
lcd.print(points[0]); // affiche aussi la valeur a l ecran
etape++; // passe a l etape suivante
Serial.println(inputValue);
delay(500);
}
}
} else if (etape == 1) { // deuxieme etape donc y1
lcd.setCursor(0, 0);
lcd.print("Entrer y1:");
if (Serial.available()) {
String inputString = "";
while (Serial.available()) {
char inputChar = Serial.read();
if (inputChar == '\n') {
break;
}
inputString += inputChar;
}
if (inputString.length() > 0) {
int inputValue = inputString.toInt();
points[1] = inputValue;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("y1: ");
lcd.print(points[1]);
etape++;
Serial.println(inputValue);
delay(500);
}
}
} else if (etape == 2) { // troisieme etape donc x2
lcd.setCursor(0, 0);
lcd.print("Entrer x2:");
if (Serial.available()) {
String inputString = "";
while (Serial.available()) {
char inputChar = Serial.read();
if (inputChar == '\n') {
break;
}
inputString += inputChar;
}
if (inputString.length() > 0) {
int inputValue = inputString.toInt();
points[2] = inputValue;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("x2: ");
lcd.print(points[2]);
etape++;
Serial.println(inputValue);
delay(500);
}
}
} else if (etape == 3) { // quatrieme etape donc y2
lcd.setCursor(0, 0);
lcd.print("Entrer y2:");
if (Serial.available()) {
String inputString = "";
while (Serial.available()) {
char inputChar = Serial.read();
if (inputChar == '\n') {
break;
}
inputString += inputChar;
}
if (inputString.length() > 0) {
int inputValue = inputString.toInt();
points[3] = inputValue;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("x2: ");
lcd.print(points[3]);
etape++;
Serial.println(inputValue);
delay(500);
}
}
} else if (etape == 4) { // cinquieme etape donc la moyenne
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("p1(");
lcd.print(points[0]); // x1
lcd.print(",");
lcd.print(points[1]); // y1
lcd.print(") p2(");
lcd.print(points[2]); // x2
lcd.print(",");
lcd.print(points[3]); // y2
lcd.print(")");
// calcule le point moyenne
moyenne_points[0] = (points[0] + points[2]) / 2.0;
moyenne_points[1] = (points[1] + points[3]) / 2.0;
delay(1000);
lcd.setCursor(0, 1);
lcd.print("pM(");
lcd.print(moyenne_points[0]); // x3
lcd.print(",");
lcd.print(moyenne_points[1]); // y3
lcd.print(")");
delay(20000);
lcd.clear();
etape = 0; // reset les etapes - recommence du debut
}
}