Gant interactif avec ruban de led et buzzer ou hp et bouton ou ldr pour declencher

Bonjour a la communauté.
J'ai un projet à realiser pour des enfants de maternelle,ça doit donc etre simple et pratique.
j ai une balle en scratch ainsi que 2 gants avec scratch et une cible avec scratch aussi.Le but est de lancer la balle et lorsqu elle touche le gant ou la cible ,une animation avec des led ou bandeau de led avec un son (provenant soit d un buzzer soit d un petit HP)se lance
Pour les sons j ai pensé a des applaudissements ou "super" ou "dans le mille" ou un son de jackpot.
Pour declencher l animation j ai pensé mettre un mini bouton poussoir au niveau de la paume du gant et au centre de la cible ou bien une ldr

le code pour la ldr j ai fais ça:

const int ledrPin = 11;
const int ledPin = 13;
const int buzzerPin = 12;
const int ldrPin = A0;

void setup () {

  Serial.begin(9600);
  pinMode(ledrPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ldrPin, INPUT);
}

void loop() {

  int ldrStatus = analogRead(ldrPin);  //read the state of the LDR value

  if (ldrStatus >= 700) {

     noTone(buzzerPin);
    digitalWrite(ledPin, LOW);
    digitalWrite(ledrPin, LOW);
     
  }
  else {

   tone(buzzerPin, 200);
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledrPin, HIGH);
    delay(250);

    noTone(buzzerPin);
    digitalWrite(ledPin, LOW);
    digitalWrite(ledrPin, LOW);
    delay(250);

    
  }
}

avec le bouton poussoir j ai du mal a trouver un truc qui marche!!(j ai mis une resistance de 10ko en serie pour eviter les rebonds mais c est pas convainquant!!
Si quelqu un a une idee avec le bouton poussoir!!
Pour alimenter le tout j ai pensé a des petites batteries lipo qu on trouve sur les petits drones.
Merci pour votre possible aide et longue vie a tous!

Post mis dans la mauvaise section, on parle anglais dans les forums généraux, je viens de déplacer le post dans la section francophone.

Merci de prendre en compte les recommandations listées dans "Les bonnes pratiques du Forum Francophone".

Je vois 3 points principaux dans ton projet :

  1. Détecter que la balle touche le gant ou la cible
  2. Jouer un son complexe (pas un bip)
  3. Faire une animation lumineuse

Pour le troisième, un bandeau de leds sera le plus efficace et probablement le plus simple à mettre en œuvre. Les leds WS2812 sont faciles à connecter et des bibliothèques existent pour les piloter : on peut faire des animations un peu complexes avec très peu de lignes de code.

Pour le second, il te faut un module DFPlayer, qui peut jouer des sons sauvés sur une carte SD.

Le premier point est plus difficile. Le bouton poussoir me semble peu adapté, il demande un effort trop important pour l'enfoncer. La LDR : à tester, je ne sais pas.

Je suppose que la cible est immobile. Est-ce que le gant sera dans la main d'un enfant ou bien posé sur un support et immobile ? Si c'est le dernier cas, alors un capteur de choc pourra détecter que la balle touche le gant ou la cible. Un accéléromètre pourra convenir.
Sinon, si le gant n'est pas immobile, il faudra trouver un autre type de capteur.

Merci pour ta reponse Lesept.
Pour la cible elle sera fixe ,par contre les gants seront portés par les enfants donc mobiles,ils devront essayer d'attraper la balle ,un peu comme au baseball.
je pensais coller une plaque en plastique dur sur le bouton et qui prenne toute la paume (fixée sous le scratch)

Avec un ESP32 il est possible de mettre certaines IO en mode tactile. Elle va détecter une variation de capacité locale et l'interpréter comme un toucher.
Mais je ne sais pas si le contact de la balle suffirait à faire varier la capacité. Il faudrait le tester.

pour le contact sur la paume du gant je pense utiliser plutot un interrupteur a lamelle ,plus sensible.
Je voulais ,par contre,integrer un autre son mais avec le buzzer uniquement et j ai trouvé ce code qui joue la musique de pacman.

/*
  Pacman Intro Theme
  Connect a piezo buzzer or speaker to pin 11 or select a new pin.
  More songs available at https://github.com/robsoncouto/arduino-songs

                                              Robson Couto, 2019
*/
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0


// change this to make the song slower or faster
int tempo = 105;

// change this to whichever pin you want to use
int buzzer = 11;

// notes of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
int melody[] = {

  // Pacman
  // Score available at https://musescore.com/user/85429/scores/107109
  NOTE_B4, 16, NOTE_B5, 16, NOTE_FS5, 16, NOTE_DS5, 16, //1
  NOTE_B5, 32, NOTE_FS5, -16, NOTE_DS5, 8, NOTE_C5, 16,
  NOTE_C6, 16, NOTE_G6, 16, NOTE_E6, 16, NOTE_C6, 32, NOTE_G6, -16, NOTE_E6, 8,

  NOTE_B4, 16,  NOTE_B5, 16,  NOTE_FS5, 16,   NOTE_DS5, 16,  NOTE_B5, 32,  //2
  NOTE_FS5, -16, NOTE_DS5, 8,  NOTE_DS5, 32, NOTE_E5, 32,  NOTE_F5, 32,
  NOTE_F5, 32,  NOTE_FS5, 32,  NOTE_G5, 32,  NOTE_G5, 32, NOTE_GS5, 32,  NOTE_A5, 16, NOTE_B5, 8
};

// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;

// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

void setup() {
  // iterate over the notes of the melody.
  // Remember, the array is twice the number of notes (notes + durations)
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    // calculates the duration of each note
    divider = melody[thisNote + 1];
    if (divider > 0) {
      // regular note, just proceed
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      // dotted notes are represented with negative durations!!
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; // increases the duration in half for dotted notes
    }

    // we only play the note for 90% of the duration, leaving 10% as a pause
    tone(buzzer, melody[thisNote], noteDuration * 0.9);

    // Wait for the specief duration before playing the next note.
    delay(noteDuration);

    // stop the waveform generation before the next note.
    noTone(buzzer);
  }
}

void loop() {
  // no need to repeat the melody.
}

Est ce possible que je l integre dans mon code donné plus haut
J'ai essayé un truc mais ça me dit,erreur de compilation pour arduino mega,

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0


// change this to make the song slower or faster
int tempo = 105;
// notes of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
int melody[] = {

  // Pacman
  // Score available at https://musescore.com/user/85429/scores/107109
  NOTE_B4, 16, NOTE_B5, 16, NOTE_FS5, 16, NOTE_DS5, 16, //1
  NOTE_B5, 32, NOTE_FS5, -16, NOTE_DS5, 8, NOTE_C5, 16,
  NOTE_C6, 16, NOTE_G6, 16, NOTE_E6, 16, NOTE_C6, 32, NOTE_G6, -16, NOTE_E6, 8,

  NOTE_B4, 16,  NOTE_B5, 16,  NOTE_FS5, 16,   NOTE_DS5, 16,  NOTE_B5, 32,  //2
  NOTE_FS5, -16, NOTE_DS5, 8,  NOTE_DS5, 32, NOTE_E5, 32,  NOTE_F5, 32,
  NOTE_F5, 32,  NOTE_FS5, 32,  NOTE_G5, 32,  NOTE_G5, 32, NOTE_GS5, 32,  NOTE_A5, 16, NOTE_B5, 8
};

// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int notes = sizeof(melody) / sizeof(melody[0]) / 2;

// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

const int ledrPin = 11;
const int ledPin = 13;
const int buzzerPin = 12;
const int ldrPin = A0;

void setup () 

{  Serial.begin(9600);
  pinMode(ledrPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ldrPin, INPUT);
 int ldrStatus = analogRead(ldrPin);
   // iterate over the notes of the melody.
  // Remember, the array is twice the number of notes (notes + durations)
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) 
{

    // calculates the duration of each note
    divider = melody[thisNote + 1];
    if (divider > 0) 
    {
      // regular note, just proceed
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) 
    {
      // dotted notes are represented with negative durations!!
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; // increases the duration in half for dotted notes
    }

  if (ldrStatus <= 400) 
  {

     noTone(buzzerPin);
    digitalWrite(ledPin, LOW);
    digitalWrite(ledrPin, LOW);
     
  }
  else 
  {

    tone(buzzerPin, melody[thisNote], noteDuration * 0.9);

    // Wait for the specief duration before playing the next note.
    delay(noteDuration);

    // stop the waveform generation before the next note.
    noTone(buzzerPin);
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledrPin, HIGH);
    delay(600);

    noTone(buzzerPin);
    digitalWrite(ledPin, LOW);
    digitalWrite(ledrPin, LOW);
    delay(600);
      }

}
}
MERCI encore pour votre possible aide

Pour intégrer le code qui joue la musique de Pacman dans ton code, tu peux faire comme ceci :

Copie tout le code "Pacman" dans ton code à toi, avant le setup. Enlève la loop du Pacman (efface la carrément) et renomme le setup du Pacman en void Pacman().

Ca transforme le setup en une fonction qui joue la musique. Cette fonction tu l'appelles simplement en insérant la ligne suivante là où tu veux jouer la musique :
Pacman();

Cette fonction est bloquante, c'est à dire que l'Arduino ne fait rien d'autre pendant qu'il joue la musique. Donc, il ne s'occupe pas des capteurs notamment.

merci pour votre aide
En tatonnant sur plusieurs codes recupérés sur instructables et youtube je suis arrivé a un truc qui ,je pense, ira bien pour mon projet.
J'ai donc 4 led avec res 220ohm (sur pin 3.6.9.11) ,mais je pense en mettre 5 (une pour chaque doigt) et un buzzer (sur pin 7)
et la ldr avec res de 10ko(sur A0)pour declencher quand elle est cachée.
Ca me joue pacman avecc les led qui clignotent en suivant la melodie.
Je vais voir pour d'autres melodies si je peux les faire jouer aleatoirement ,ça serait cool.

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0

#define led0 B0001
#define led1 B0010
#define led2 B0100
#define led3 B1000

void setup () {
const int buzzerPin = 7;
const int ldrPin = A0;
int ledPins[] = { 
  3,6,9,11 };       // an array of pin numbers to which LEDs are attached
const int pinCount = 4;           // the number of pins (i.e. the length of the array)
}
// notes in the melody:
int melody[] = {
  NOTE_B4, NOTE_B5, NOTE_FS5, NOTE_DS5, //1
  NOTE_B5, NOTE_FS5, NOTE_DS5, NOTE_C5,
  NOTE_C6, NOTE_G6, NOTE_E6, NOTE_C6, NOTE_G6, NOTE_E6,

  NOTE_B4, NOTE_B5, NOTE_FS5, NOTE_DS5, NOTE_B5,//2
  NOTE_FS5, NOTE_DS5, NOTE_DS5, NOTE_E5, NOTE_F5, 32,
  NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_B5,
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {

  16, 16, 16, 16,
  32, 16, 8, 16,
  16, 16, 16, 32, 32, 8,
  16, 16, 16, 16, 32, 
  32, 8, 32, 32, 32,
  32, 32, 32, 32, 32, 16, 8    
   };

byte LEDs[]={
   B0010,   B0100,   B1000,   B0001,
   B0010,   B1000,   B0100,   B0010,
   B1000,   B0100,   B0010,   B0001,   B1111,   B0001,
   B1000,   B0010,   B1111,   B0010,   B0001,
   B0100,   B1000,   B0001,   B1111,   B0001,
   B0010,   B0001,   B1000,   B0100,   B0010,   B1000,   B1111};
   
   
void Pacman() {
 int buzzerPin = 7;
 int ledPins[] = { 
  3,6,9,11 };
 int pinCount = 4;
 int ldrPin = A0;
  for (int i=0;i<pinCount;i++)
  {
    pinMode(ledPins[i],OUTPUT);
    digitalWrite(ledPins[i], LOW);
  }
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 31; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 2500/noteDurations[thisNote];
    // I found that by lengthening the noteDuration,
    // I was able to "slow" the song down.
    tone(7, melody[thisNote],noteDuration);
  
    LEDon(thisNote);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    delay(noteDuration);
    LEDoff(thisNote);

    int pauseBetweenNotes = noteDuration * 0.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(7);
  }
}

void loop() {
int ldrPin = A0;
int buzzerPin = 7;
  int ldrStatus = analogRead(ldrPin);  //read the state of the LDR value

  if (ldrStatus <= 400) {

     noTone(buzzerPin);
         
  }
  else {
   
   Pacman();
    
        
      }
}

int currentLEDpin=3;
void LEDon(int thisNote)
{ int ledPins[] = { 
  3,6,9,11 };
  if ((LEDs[thisNote]&led0)!=0) digitalWrite(ledPins[0],HIGH);
  if ((LEDs[thisNote]&led1)!=0) digitalWrite(ledPins[1],HIGH);
  if ((LEDs[thisNote]&led2)!=0) digitalWrite(ledPins[2],HIGH);
  if ((LEDs[thisNote]&led3)!=0) digitalWrite(ledPins[3],HIGH);
}

void LEDoff(int thisNote)
{ int ledPins[] = { 
  3,6,9,11 };
  if ((LEDs[thisNote]&led0)!=0) digitalWrite(ledPins[0],LOW);
  if ((LEDs[thisNote]&led1)!=0) digitalWrite(ledPins[1],LOW);
  if ((LEDs[thisNote]&led2)!=0) digitalWrite(ledPins[2],LOW);
  if ((LEDs[thisNote]&led3)!=0) digitalWrite(ledPins[3],LOW);
}