Franchement, cette IA, c'est parfois n'importe quoi... Heureusement qu'elle s'auto-corrige après une réclamation. Après plusieurs itérations, voici donc le code qui passe l'étape de compilation :
#include <HID-Project.h> // Inclure la bibliothèque HID-Project
#include <HID-Settings.h> // Inclure les paramètres HID
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // 12 boutons
const int joystickClick = A2; // Clic du joystick
const int joystickX = A0; // Axe X
const int joystickY = A1; // Axe Y
const int calibrateButton = A3; // Bouton de calibration
const int buzzerPin = A4; // Buzzer
int xMin = 1023, xMax = 0;
int yMin = 1023, yMax = 0;
const unsigned long longPressDuration = 500;
const unsigned long debounceDelay = 50;
unsigned long lastDebounceTime[12] = {0}; // 12 boutons
unsigned long lastJoystickDebounceTime = 0;
unsigned long lastCalibrateDebounceTime = 0;
bool buttonStates[12]; // 12 boutons
bool lastButtonStates[12]; // 12 boutons
bool lastArrowStates[4] = {false, false, false, false}; // UP, DOWN, LEFT, RIGHT
bool lastJoystickClickState = HIGH;
bool lastCalibrateButtonState = HIGH;
void setup() {
for (int i = 0; i < 12; i++) { // 12 boutons
pinMode(buttonPins[i], INPUT_PULLUP);
buttonStates[i] = HIGH;
lastButtonStates[i] = HIGH;
}
pinMode(joystickClick, INPUT_PULLUP);
pinMode(calibrateButton, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
Gamepad.begin(); // Initialisation du joystick avec HID-Project
Keyboard.begin(); // Initialisation du clavier
calibrateJoystick();
}
void loop() {
readButtons();
handleJoystickClick();
handleCalibrateButton();
handleJoystickMovement();
delay(10);
}
void readButtons() {
for (int i = 0; i < 12; i++) { // 12 boutons
bool reading = digitalRead(buttonPins[i]);
if (reading != lastButtonStates[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != buttonStates[i]) {
buttonStates[i] = reading;
if (buttonStates[i] == LOW) {
Gamepad.press(i + 1); // Utilisez Gamepad pour les boutons
} else {
Gamepad.release(i + 1); // Relâcher le bouton
}
}
}
lastButtonStates[i] = reading;
}
}
void handleJoystickClick() {
bool reading = digitalRead(joystickClick);
if (reading != lastJoystickClickState) {
lastJoystickDebounceTime = millis();
}
if ((millis() - lastJoystickDebounceTime) > debounceDelay) {
if (reading == LOW && lastJoystickClickState == HIGH) {
unsigned long pressStart = millis();
while (digitalRead(joystickClick) == LOW) {
// Attendre que le bouton soit relâché
}
unsigned long pressDuration = millis() - pressStart;
if (pressDuration < longPressDuration) {
Keyboard.press(KEY_RETURN); // Utilisez Keyboard pour émuler la touche Entrée
delay(50);
Keyboard.release(KEY_RETURN);
} else {
Keyboard.press(KEY_ESC); // Utilisez Keyboard pour émuler la touche Échap
delay(50);
Keyboard.release(KEY_ESC);
}
}
}
lastJoystickClickState = reading;
}
void handleCalibrateButton() {
bool reading = digitalRead(calibrateButton);
if (reading != lastCalibrateButtonState) {
lastCalibrateDebounceTime = millis();
}
if ((millis() - lastCalibrateDebounceTime) > debounceDelay) {
if (reading == LOW && lastCalibrateButtonState == HIGH) {
calibrateJoystick();
}
}
lastCalibrateButtonState = reading;
}
void handleJoystickMovement() {
int xValue = analogRead(joystickX);
int yValue = analogRead(joystickY);
int xAxis = map(xValue, xMin, xMax, -32767, 32767); // HID-Project utilise des valeurs 16 bits
int yAxis = map(yValue, yMin, yMax, -32767, 32767);
xAxis = constrain(xAxis, -32767, 32767);
yAxis = constrain(yAxis, -32767, 32767);
Gamepad.xAxis(xAxis); // Mettre à jour l'axe X du joystick
Gamepad.yAxis(yAxis); // Mettre à jour l'axe Y du joystick
bool arrowStates[4] = {
yAxis < -16384, // UP
yAxis > 16384, // DOWN
xAxis < -16384, // LEFT
xAxis > 16384 // RIGHT
};
const uint8_t arrowKeys[4] = {KEY_UP_ARROW, KEY_DOWN_ARROW, KEY_LEFT_ARROW, KEY_RIGHT_ARROW};
for (int i = 0; i < 4; i++) {
if (arrowStates[i] != lastArrowStates[i]) {
if (arrowStates[i]) {
Keyboard.press(arrowKeys[i]); // Utilisez Keyboard pour émuler les touches fléchées
} else {
Keyboard.release(arrowKeys[i]);
}
lastArrowStates[i] = arrowStates[i];
}
}
}
void calibrateJoystick() {
xMin = 1023; xMax = 0; yMin = 1023; yMax = 0;
for (int i = 0; i < 100; i++) {
int xValue = analogRead(joystickX);
int yValue = analogRead(joystickY);
xMin = min(xMin, xValue);
xMax = max(xMax, xValue);
yMin = min(yMin, yValue);
yMax = max(yMax, yValue);
delay(10);
}
if (xMin >= xMax || yMin >= yMax) {
buzzError();
} else {
buzzSuccess();
}
}
void buzzSuccess() {
tone(buzzerPin, 1000, 1000); // 1 kHz pendant 1 seconde
}
void buzzError() {
for (int i = 0; i < 3; i++) {
tone(buzzerPin, 500, 200); // 500 Hz pendant 200 ms
delay(400); // Pause de 200 ms entre les buzz
}
}
J'ai donné le même code à une autre IA, et elle a proposé des améliorations que je trouve intéressantes :
#include <HID-Project.h> // Inclure la bibliothèque HID-Project
#include <HID-Settings.h> // Inclure les paramètres HID
#include <Bounce2.h> // Inclure la bibliothèque Bounce2 pour le débounce
// Déclaration des broches
const int buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // 12 boutons
const int joystickClick = A2; // Clic du joystick
const int calibrateButton = A4; // Bouton de calibration
const int buzzerPin = A3; // Buzzer
const int joystickX = A0; // Axe X
const int joystickY = A1; // Axe Y
// Variables pour la calibration du joystick
int xMin = 1023, xMax = 0;
int yMin = 1023, yMax = 0;
// Constantes pour la gestion des appuis longs et du débounce
const unsigned long longPressDuration = 500; // Durée pour un appui long
const unsigned long debounceDelay = 50; // Délai de débounce
// Variables pour le débounce des boutons
Bounce debouncer[12]; // Débounceurs pour les 12 boutons
Bounce joystickClickDebouncer; // Débounceur pour le clic du joystick
Bounce calibrateButtonDebouncer; // Débounceur pour le bouton de calibration
// États des boutons
bool buttonStates[12]; // États actuels des 12 boutons
bool lastButtonStates[12]; // États précédents des 12 boutons
bool lastArrowStates[4] = {false, false, false, false}; // États précédents des directions (UP, DOWN, LEFT, RIGHT)
bool lastJoystickClickState = HIGH; // État précédent du clic du joystick
bool lastCalibrateButtonState = HIGH; // État précédent du bouton de calibration
// Seuil pour détecter les directions du joystick
const int joystickThreshold = 16384; // Seuil configurable
// Fonction pour émettre un son de succès (1 bip très long)
void buzzSuccess() {
tone(buzzerPin, 1000, 2000); // 1 kHz pendant 2 secondes
}
// Fonction pour émettre un son d'erreur (3 bips courts)
void buzzError() {
for (int i = 0; i < 3; i++) {
tone(buzzerPin, 500, 200); // 500 Hz pendant 200 ms
delay(400); // Pause de 200 ms entre les bips
}
}
// Fonction pour émettre un son d'avertissement (4 bips : long, court, long, court)
void buzzWarning() {
tone(buzzerPin, 1000, 500); // 1 kHz pendant 500 ms (long)
delay(600); // Pause de 600 ms
tone(buzzerPin, 500, 200); // 500 Hz pendant 200 ms (court)
delay(400); // Pause de 400 ms
tone(buzzerPin, 1000, 500); // 1 kHz pendant 500 ms (long)
delay(600); // Pause de 600 ms
tone(buzzerPin, 500, 200); // 500 Hz pendant 200 ms (court)
}
void setup() {
// Initialisation des broches
for (int i = 0; i < 12; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
debouncer[i].attach(buttonPins[i], INPUT_PULLUP);
debouncer[i].interval(debounceDelay); // Configurer le délai de débounce
buttonStates[i] = HIGH;
lastButtonStates[i] = HIGH;
}
pinMode(joystickClick, INPUT_PULLUP);
joystickClickDebouncer.attach(joystickClick, INPUT_PULLUP);
joystickClickDebouncer.interval(debounceDelay);
pinMode(calibrateButton, INPUT_PULLUP);
calibrateButtonDebouncer.attach(calibrateButton, INPUT_PULLUP);
calibrateButtonDebouncer.interval(debounceDelay);
pinMode(buzzerPin, OUTPUT);
// Initialisation du joystick et du clavier
Gamepad.begin();
Keyboard.begin();
// Calibration initiale du joystick
calibrateJoystick();
}
void loop() {
// Lire les boutons
readButtons();
// Gérer le clic du joystick
handleJoystickClick();
// Gérer le bouton de calibration
handleCalibrateButton();
// Gérer le mouvement du joystick
handleJoystickMovement();
// Petit délai pour éviter une boucle trop rapide
delay(10);
}
// Fonction pour lire l'état des boutons avec débounce
void readButtons() {
for (int i = 0; i < 12; i++) {
debouncer[i].update(); // Mettre à jour le débounceur
bool reading = debouncer[i].read();
if (reading != lastButtonStates[i]) {
buttonStates[i] = reading;
if (buttonStates[i] == LOW) {
Gamepad.press(i + 1); // Appuyer sur le bouton
} else {
Gamepad.release(i + 1); // Relâcher le bouton
}
}
lastButtonStates[i] = reading;
}
}
// Fonction pour gérer le clic du joystick
void handleJoystickClick() {
joystickClickDebouncer.update(); // Mettre à jour le débounceur
bool reading = joystickClickDebouncer.read();
if (reading != lastJoystickClickState) {
lastJoystickClickState = reading;
if (reading == LOW) {
unsigned long pressStart = millis();
while (joystickClickDebouncer.read() == LOW) {
// Attendre que le bouton soit relâché
}
unsigned long pressDuration = millis() - pressStart;
if (pressDuration < longPressDuration) {
Keyboard.press(KEY_RETURN); // Appui court : touche Entrée
delay(50);
Keyboard.release(KEY_RETURN);
} else {
Keyboard.press(KEY_ESC); // Appui long : touche Échap
delay(50);
Keyboard.release(KEY_ESC);
}
}
}
}
// Fonction pour gérer le bouton de calibration
void handleCalibrateButton() {
calibrateButtonDebouncer.update(); // Mettre à jour le débounceur
bool reading = calibrateButtonDebouncer.read();
if (reading != lastCalibrateButtonState) {
lastCalibrateButtonState = reading;
if (reading == LOW) {
calibrateJoystick();
}
}
}
// Fonction pour gérer le mouvement du joystick
void handleJoystickMovement() {
int xValue = analogRead(joystickX);
int yValue = analogRead(joystickY);
// Mapper les valeurs analogiques vers une plage de -32767 à 32767
int xAxis = map(xValue, xMin, xMax, -32767, 32767);
int yAxis = map(yValue, yMin, yMax, -32767, 32767);
// Limiter les valeurs pour éviter les dépassements
xAxis = constrain(xAxis, -32767, 32767);
yAxis = constrain(yAxis, -32767, 32767);
// Mettre à jour les axes du joystick
Gamepad.xAxis(xAxis);
Gamepad.yAxis(yAxis);
// Détecter les directions du joystick
bool arrowStates[4] = {
yAxis < -joystickThreshold, // UP
yAxis > joystickThreshold, // DOWN
xAxis < -joystickThreshold, // LEFT
xAxis > joystickThreshold // RIGHT
};
// Touches fléchées correspondantes
const uint8_t arrowKeys[4] = {KEY_UP_ARROW, KEY_DOWN_ARROW, KEY_LEFT_ARROW, KEY_RIGHT_ARROW};
// Gérer les touches fléchées
for (int i = 0; i < 4; i++) {
if (arrowStates[i] != lastArrowStates[i]) {
if (arrowStates[i]) {
Keyboard.press(arrowKeys[i]); // Appuyer sur la touche fléchée
} else {
Keyboard.release(arrowKeys[i]); // Relâcher la touche fléchée
}
lastArrowStates[i] = arrowStates[i];
}
}
}
// Fonction pour calibrer le joystick
void calibrateJoystick() {
xMin = 1023; xMax = 0; yMin = 1023; yMax = 0;
for (int i = 0; i < 100; i++) {
int xValue = analogRead(joystickX);
int yValue = analogRead(joystickY);
xMin = min(xMin, xValue);
xMax = max(xMax, xValue);
yMin = min(yMin, yValue);
yMax = max(yMax, yValue);
delay(10);
}
// Vérifier que les valeurs sont valides
if (xMin == 1023 || xMax == 0 || yMin == 1023 || yMax == 0) {
buzzWarning(); // Joystick non détecté ou non bougé (4 bips : long, court, long, court)
} else if (xMin >= xMax || yMin >= yMax) {
buzzError(); // Erreur de calibration (3 bips courts)
} else {
buzzSuccess(); // Calibration réussie (1 bip très long)
}
}
il passe aussi l'etape de compilation
Je teste ça demain