#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
int dt = 1;
float tension_touche;
const byte ROWS = 2;
const byte COLS = 2;
int Vmin = 1;
uint16_t dacValue = round(Vmin * (4095 / 5));
float dV = 1.0 / 12.0;
char hexaKeys[ROWS][COLS] = {
{'A', 'B'},
{'C', 'D'},
};
char currentKey = NO_KEY; // Initialisation de currentKey en dehors de la fonction loop
byte colPins[COLS] = {6, 7};
byte rowPins[ROWS] = {2, 3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
byte gatePin = 13;
byte pinhold = 12;
int etat_gate = 0;
int etat_pin_hold = 0;
bool isKeyPressed = false;
bool wasKeyPressed = false;
void setup()
{
Serial.begin(9600);
pinMode(gatePin, OUTPUT);
digitalWrite(gatePin, LOW);
pinMode(pinhold, INPUT_PULLUP);
dac.begin(0x60);
}
void loop()
{
char key = customKeypad.getKey();
if (key != NO_KEY)
{
if (!wasKeyPressed)
{
// La première touche est enfoncée, configurer gatePin sur LOW pendant 1 ms
digitalWrite(gatePin, LOW);
delay(1);
}
// Si une touche est enfoncée, configure la broche gatePin sur HIGH
digitalWrite(gatePin, HIGH);
isKeyPressed = true;
// Traitez la dernière touche enfoncée ici
switch (key)
{
case 'A':
tension_touche = Vmin;
break;
case 'B':
tension_touche = Vmin + 1 * dV;
break;
case 'C':
tension_touche = Vmin + 2 * dV;
break;
case 'D':
tension_touche = Vmin + 3 * dV;
break;
}
// Mettez à jour dacValue
dacValue = round(tension_touche * (4095 / 5));
dac.setVoltage(dacValue, false);
Serial.println(dacValue);
// Marquer la touche actuelle comme la dernière touche enfoncée
currentKey = key;
wasKeyPressed = true;
}
else
{
dac.setVoltage(dacValue, false);
Serial.println(dacValue);
// Si aucune touche n'est enfoncée
if (customKeypad.getState() == IDLE && isKeyPressed)
{
// Détecter le relâchement de la touche
wasKeyPressed = false;
etat_pin_hold = digitalRead(pinhold);
etat_gate = digitalRead(gatePin);
// Ajouter la condition supplémentaire ici
if (etat_pin_hold == HIGH && etat_gate == HIGH)
{
// Ne rien faire, laisser gatePin HIGH
}
else
{
Serial.println("relache");
digitalWrite(gatePin, LOW);
isKeyPressed = false;
}
}
}
}