You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
4.1 KiB
179 lines
4.1 KiB
#include"LIS3DHTR.h"
|
|
#include "TFT_eSPI.h"
|
|
LIS3DHTR<TwoWire> lis;
|
|
#define R 5 //Rayon de la boule donc diamètre 10
|
|
#define XMAX 240 // Définit la taille en longueur de l'écran
|
|
#define YMAX 320 // Définit la taille en largeur de l'écran
|
|
#define BUZZER_PIN WIO_BUZZER
|
|
|
|
TFT_eSPI tft;
|
|
unsigned int modif=1 ; //si déplacement, on affiche. 1 pour premier affichage
|
|
int x=XMAX/2, y=YMAX/2;
|
|
unsigned long tempsDepart = 0;
|
|
bool enCours = false;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
lis.begin(Wire1);
|
|
|
|
if (!lis) {
|
|
Serial.println("ERROR");
|
|
while(1);
|
|
}
|
|
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); //Data output rate
|
|
lis.setFullScaleRange(LIS3DHTR_RANGE_2G); //Scale range set to 2g
|
|
|
|
tft.begin();
|
|
tft.fillScreen(TFT_WHITE); // fond blanc
|
|
|
|
pinMode(WIO_KEY_A, INPUT_PULLUP);
|
|
pinMode(WIO_KEY_B, INPUT_PULLUP);
|
|
pinMode(WIO_KEY_C, INPUT_PULLUP);
|
|
|
|
pinMode(WIO_BUZZER, OUTPUT);
|
|
|
|
}
|
|
|
|
void chrono(int temps) {
|
|
int i;
|
|
for(i = temps; i>=0; i--) {
|
|
Serial.print(i);Serial.print(" secondes.");
|
|
delay(1000);
|
|
}
|
|
}
|
|
|
|
void perdu() {
|
|
tft.fillScreen(TFT_RED); // fond rouge
|
|
}
|
|
|
|
void loop() {
|
|
|
|
bool demarrer = false;
|
|
int temps = 30;
|
|
int temps2;
|
|
|
|
while (!demarrer) {
|
|
if (digitalRead(WIO_KEY_A) == LOW) {
|
|
Serial.print("Rajoutter 10 secondes");
|
|
Serial.println();
|
|
}
|
|
else if (digitalRead(WIO_KEY_B) == LOW) {
|
|
Serial.print("Lancer la partie sur le niveau choisit");
|
|
Serial.println();
|
|
|
|
demarrer = true;
|
|
}
|
|
else if (digitalRead(WIO_KEY_C) == LOW) {
|
|
Serial.print("Choisir le niveau");
|
|
Serial.println();
|
|
}
|
|
}
|
|
|
|
if (!enCours && digitalRead(WIO_KEY_B) == LOW) {
|
|
enCours = true;
|
|
tempsDepart = millis();
|
|
Serial.println("Chronomètre démarré !");
|
|
delay(300); // Anti-rebond
|
|
}
|
|
|
|
// Réinitialiser
|
|
if (digitalRead(WIO_KEY_B) == LOW) {
|
|
enCours = false;
|
|
Serial.println("Chronomètre réinitialisé.");
|
|
delay(300);
|
|
}
|
|
|
|
// Affichage du temps écoulé
|
|
if (enCours) {
|
|
unsigned long tempsActuel = millis();
|
|
unsigned long secondes = (tempsActuel - tempsDepart) / 1000;
|
|
Serial.print("Temps écoulé : ");
|
|
Serial.print(secondes);
|
|
Serial.println(" s");
|
|
delay(1000); // Affiche chaque seconde
|
|
}
|
|
|
|
float x_a,y_a;
|
|
x_a = lis.getAccelerationX() * 100-4;
|
|
y_a = lis.getAccelerationY() * 100+1;
|
|
|
|
Serial.print(" X: "); Serial.print(x_a); Serial.print(" x: "); Serial.print(x);
|
|
Serial.print(" Y: "); Serial.print(y_a); Serial.print(" y: "); Serial.print(y);
|
|
Serial.println();
|
|
|
|
|
|
//tft.fillCircle(x,y,R + 1,TFT_WHITE);
|
|
//on affiche le nouveau cercle
|
|
//tft.fillCircle(x_a,y_a, R, TFT_RED);
|
|
|
|
if (x_a > 0 ) { // penche vers nous
|
|
modif=1;
|
|
Serial.print("mouvement vers nous");
|
|
Serial.println();
|
|
x++;
|
|
if (x >= XMAX) {
|
|
Serial.print("MUR TOUCHÉ VERS NOUS TOUCHÉ!");
|
|
perdu();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if (x_a < 0 ) { // penche vers l'arrière
|
|
modif=1;
|
|
Serial.print("mouvement vers l'arrière");
|
|
Serial.println();
|
|
x--;
|
|
if (x <= 0) {
|
|
Serial.print("MUR TOUCHÉ A L'ARRIÈRE TOUCHÉ!");
|
|
perdu();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if (y_a < 0 ) { // penche vers l'arrière
|
|
modif=1;
|
|
Serial.print("mouvement vers la droite");
|
|
Serial.println();
|
|
y--;
|
|
if (y <= 0) {
|
|
Serial.print("MUR TOUCHÉ VERS LA DROITE TOUCHÉ!");
|
|
perdu();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if (y_a > 0 ) { // penche vers l'arrière
|
|
modif=1;
|
|
Serial.print("mouvement vers la gauche");
|
|
Serial.println();
|
|
y++;
|
|
if (y >= YMAX) {
|
|
Serial.print("MUR TOUCHÉ VERS LA GAUCHE TOUCHÉ!");
|
|
perdu();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if (modif == 1){
|
|
// on affiche le nouveau cercle
|
|
tft.fillCircle(x,y,R + 2,TFT_WHITE);
|
|
tft.fillCircle(x,y, R, TFT_RED);
|
|
modif=0;
|
|
}
|
|
delay(50);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// déplacement vers l'arrière : x diminue
|
|
// déplacement vers nous : x augmente
|
|
// déplacement vers la gauche : y augmente
|
|
// déplacement vers la droite : y diminue
|
|
// Exemple de résultat : X: 0.08 Y: -0.09 Z: -1.02
|
|
// En pourcentage donc convertir.
|
|
|
|
// x_a = lis.getAccelerationX() * 100-4; // Correspond au y de l'écran
|
|
// y_a = lis.getAccelerationY() * 100+1; // Correspond au -x de l'écran
|