Bonjour à tous !
Voici un petit bout de code qui permet de détecter le contact entre des broches numériques et de récupérer la combinaison sous la forme d'un octet.
L'idée m'a été donnée par "fdufnews" (cf [Résolu] Detecter le contact de broches numériques entre elles - Français - Arduino Forum)
- J'ai 5 digital pin le pin D2 écrit dans le bit 1, le pin D3 dans le bit 2...
voici donc le tableau des différentes combinaisons : machord machord - Google Sheets
Et le code:
int touchPins[] = {2,3,4,5,6};
#define numtouchPins (sizeof(touchPins)/sizeof(int *)) //array size
byte PinState;
void oneLOW(int PinID)
{
for(int i=0; i < numtouchPins; i++)//All pins INPUT with Pull Up
{
pinMode(touchPins[i],INPUT);
digitalWrite(touchPins[i],HIGH);
}
pinMode(touchPins[PinID],OUTPUT);//put PinID LOW
digitalWrite(touchPins[PinID],LOW);
for(int i=0; i < numtouchPins; i++)//read all pins
{
if(!digitalRead(touchPins[i]) && i!=PinID)//if the pin is LOW and we dont declar before (it touch another pin)
{
bitSet(PinState, i);//write state on PinState
}
}
}
void setup()
{
for( int i=0; i < numtouchPins; i++)
{
pinMode(touchPins[i],INPUT);
digitalWrite(touchPins[i],HIGH);
}
Serial.begin(9600);
}
void loop()
{
for(int i=0; i < numtouchPins; i++)//scan pins
{
oneLOW(i);
}
Serial.println(PinState);
PinState=0;
}
L'idée par la suite est de l’implémenter dans Mackey Mackey afin d'étendre les possibilités, je vais lancer un post dans le forum mackey mackey.
Je suis ouvert a toutes critiques bien sur.