Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
wiki:tutoriels:arduino:detecter_un_signal_electrique [2021/10/19 16:14] damien.muti [Programme Arduino 2 : Détection analogique] |
wiki:tutoriels:arduino:detecter_un_signal_electrique [2024/05/13 17:49] damien.muti [Simulation LTSpice] |
||
---|---|---|---|
Ligne 16: | Ligne 16: | ||
Les logiciels de simulation électronique permettent de schématiser le montage associé. Ici nous avons utilisé le logiciel gratuit [[https:// | Les logiciels de simulation électronique permettent de schématiser le montage associé. Ici nous avons utilisé le logiciel gratuit [[https:// | ||
+ | |||
+ | __**Le schéma N°1**__ correspond à une prise de signal avant le transformateur du haut parleur. Il implique une tension d' | ||
{{ : | {{ : | ||
+ | |||
+ | __**Le schéma N° 2**__ correspond à une prise de signal après le transformateur du haut parleur. Il implique une tension d'une dizaine de volt au maximum. Le transformateur permettant de diviser par 10 la tension porteuse du signal. Le signal utilisé est donc inoffensif. Le schéma du montage est le suivant : | ||
+ | {{ : | ||
Le matériel utilisé ici est le suivant (fournisseur [[https:// | Le matériel utilisé ici est le suivant (fournisseur [[https:// | ||
Ligne 29: | Ligne 34: | ||
===== Principe de fonctionnement ===== | ===== Principe de fonctionnement ===== | ||
+ | |||
+ | |||
+ | ==== Simulation LTSpice (schéma N°2) ==== | ||
+ | === Signal à différents points de mesures === | ||
+ | {{ : | ||
+ | |||
+ | Un agrandissement de la fenêtre à un instant où la tension est bien égale à 5V : | ||
+ | {{ : | ||
==== Signal envoyé par l’amplificateur sonore ==== | ==== Signal envoyé par l’amplificateur sonore ==== | ||
Ligne 42: | Ligne 55: | ||
On remarque que le signal a une durée totale d' | On remarque que le signal a une durée totale d' | ||
+ | |||
+ | |||
==== Masse flottante ==== | ==== Masse flottante ==== | ||
Ligne 119: | Ligne 134: | ||
==== Programme Arduino 1 : Détection digitale ==== | ==== Programme Arduino 1 : Détection digitale ==== | ||
+ | |||
+ | Le programme est téléchargeable sur ce lien: [[https:// | ||
+ | < | ||
+ | #include " | ||
+ | |||
+ | #ifdef __AVR__ | ||
+ | #include < | ||
+ | SoftwareSerial SSerial(5, 6); // RX, TX | ||
+ | #define COMSerial SSerial | ||
+ | #define ShowSerial Serial | ||
+ | |||
+ | WT2003S< | ||
+ | #endif | ||
+ | |||
+ | #ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE | ||
+ | #define COMSerial Serial1 | ||
+ | #define ShowSerial SerialUSB | ||
+ | |||
+ | WT2003S< | ||
+ | #endif | ||
+ | |||
+ | #ifdef ARDUINO_ARCH_STM32F4 | ||
+ | #define COMSerial Serial | ||
+ | #define ShowSerial SerialUSB | ||
+ | |||
+ | WT2003S< | ||
+ | #endif | ||
+ | |||
+ | |||
+ | uint8_t vol = 10; | ||
+ | uint32_t spi_flash_songs = 0; | ||
+ | uint32_t sd_songs = 0; | ||
+ | STROAGE workdisk = SD; | ||
+ | struct Play_history { | ||
+ | uint8_t disk; | ||
+ | uint16_t index; | ||
+ | char name[8]; | ||
+ | }* SPISong, *SDSong; | ||
+ | |||
+ | //////////////////////////////////////////////// | ||
+ | #define BOUTON 2 | ||
+ | #define ENTREE_DETECTEUR_SIGNAL A0 | ||
+ | #define SEUIL_DETECTION 300 // déclenchement du son si l' | ||
+ | uint8_t status; | ||
+ | int entreeDetecteur =0; | ||
+ | // Defining some status codes from the WT2003S | ||
+ | #define STATUS_PLAY 0x01 | ||
+ | #define STATUS_STOP 0x02 | ||
+ | #define STATUS_PAUSE 0x03 | ||
+ | ////////////////////////////////////////// | ||
+ | |||
+ | /////////////////////////////// | ||
+ | void readSongName(struct Play_history* ph, uint32_t num, STROAGE disk) { | ||
+ | Mp3Player.volume(0); | ||
+ | delay(100); | ||
+ | switch (disk) { | ||
+ | case SPIFLASH: | ||
+ | Mp3Player.playSPIFlashSong(0x0001); | ||
+ | break; | ||
+ | case SD: | ||
+ | Mp3Player.playSDRootSong(0x0001); | ||
+ | break; | ||
+ | case UDISK: | ||
+ | Mp3Player.playUDiskRootSong(0x0001); | ||
+ | break; | ||
+ | } | ||
+ | ShowSerial.println(" | ||
+ | for (int i = 0; i < num ; i++) { | ||
+ | delay(300); | ||
+ | ph[i].disk = disk; | ||
+ | ph[i].index = Mp3Player.getTracks(); | ||
+ | Mp3Player.getSongName(ph[i].name); | ||
+ | Mp3Player.next(); | ||
+ | } | ||
+ | ShowSerial.println(" | ||
+ | Mp3Player.pause_or_play(); | ||
+ | Mp3Player.volume(14); | ||
+ | delay(100); | ||
+ | } | ||
+ | |||
+ | void getAllSong() { | ||
+ | uint8_t diskstatus = Mp3Player.getDiskStatus(); | ||
+ | ShowSerial.println(diskstatus); | ||
+ | spi_flash_songs = Mp3Player.getSPIFlashMp3FileNumber(); | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(spi_flash_songs); | ||
+ | if (spi_flash_songs > 0) { | ||
+ | SPISong = (struct Play_history*)malloc((spi_flash_songs + 1) * sizeof(struct Play_history)); | ||
+ | readSongName(SPISong, | ||
+ | } | ||
+ | if (diskstatus && 0x02) { // have SD | ||
+ | sd_songs = Mp3Player.getSDMp3FileNumber(); | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(sd_songs); | ||
+ | if (sd_songs > 0) { | ||
+ | SDSong = (struct Play_history*)malloc((sd_songs + 1) * sizeof(struct Play_history)); | ||
+ | ShowSerial.println(" | ||
+ | readSongName(SDSong, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void printSongs() { | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.print("< | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(); | ||
+ | ShowSerial.println(" | ||
+ | for (int i = 0 ; i < spi_flash_songs; | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.print(SPISong[i].index); | ||
+ | ShowSerial.print("< | ||
+ | ShowSerial.print(SPISong[i].name); | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(); | ||
+ | } | ||
+ | ShowSerial.println(" | ||
+ | for (int i = 0 ; i < sd_songs; i++) { | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.print(SDSong[i].index); | ||
+ | ShowSerial.print("< | ||
+ | ShowSerial.print(SDSong[i].name); | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void printMenu(void) { | ||
+ | ShowSerial.println(" | ||
+ | ShowSerial.println(" | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println("' | ||
+ | ShowSerial.println(" | ||
+ | ShowSerial.println(); | ||
+ | ShowSerial.println(" | ||
+ | ShowSerial.println(); | ||
+ | } | ||
+ | |||
+ | void audio(int cmd) { | ||
+ | ShowSerial.print(" | ||
+ | if (workdisk == SD) { | ||
+ | Mp3Player.playSDRootSong(cmd); | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(SDSong[cmd - ' | ||
+ | } | ||
+ | if (workdisk == SPIFLASH) { | ||
+ | Mp3Player.playSPIFlashSong(cmd - ' | ||
+ | ShowSerial.print(cmd + ": "); | ||
+ | ShowSerial.print(SPISong[cmd - ' | ||
+ | } | ||
+ | ShowSerial.println(); | ||
+ | } | ||
+ | |||
+ | void afficheSerialSon(int i) { | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.print(SPISong[i].index); | ||
+ | ShowSerial.print("< | ||
+ | ShowSerial.print(SPISong[i].name); | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(); | ||
+ | } | ||
+ | /////////////////////////////////////////////////////////////////////// | ||
+ | void setup() { | ||
+ | while (!ShowSerial); | ||
+ | ShowSerial.begin(9600); | ||
+ | COMSerial.begin(9600); | ||
+ | ShowSerial.println(" | ||
+ | Mp3Player.init(COMSerial); | ||
+ | |||
+ | |||
+ | ShowSerial.println(" | ||
+ | getAllSong(); | ||
+ | printMenu(); | ||
+ | printSongs(); | ||
+ | |||
+ | /// volume au max | ||
+ | Mp3Player.volume(32); | ||
+ | // pin du bouton | ||
+ | pinMode(BOUTON, | ||
+ | } | ||
+ | ///////////////////////////////////////////// | ||
+ | void loop() { | ||
+ | // tester le statut du lecteur : (1) est en train de jouer, (2): stop, (3): en pause | ||
+ | status = Mp3Player.getStatus(); | ||
+ | entreeDetecteur = analogRead(ENTREE_DETECTEUR_SIGNAL); | ||
+ | if ( entreeDetecteur > SEUIL_DETECTION && (status == STATUS_STOP || status == STATUS_PAUSE)) { // si la tension en A0 est supéreure au seuil ET que le son ne joue pas 0x02:stop, 0x03: pause | ||
+ | // debug | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(entreeDetecteur); | ||
+ | |||
+ | digitalWrite(13, | ||
+ | // sélection aléatoire de l' | ||
+ | byte indexMusic = floor(random(0, | ||
+ | // affiche les caractéristiques de la musique | ||
+ | // | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(indexMusic); | ||
+ | // joue la musique | ||
+ | Mp3Player.playMode(SINGLE_CYCLE);// | ||
+ | audio(indexMusic); | ||
+ | } | ||
+ | else if (entreeDetecteur < SEUIL_DETECTION && status == STATUS_PLAY) { // OU si la tension en A0 est inférieure au seuil ET que le son joue : on l' | ||
+ | // si le bouton est relâché et que le son joue : on l' | ||
+ | digitalWrite(13, | ||
+ | // | ||
+ | Mp3Player.pause_or_play(); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
==== Programme Arduino 2 : Détection analogique ==== | ==== Programme Arduino 2 : Détection analogique ==== | ||
- | Le programme est téléchargeable sur ce lien: Sonnerie_Aleatoire_WT2003S_9_Lecture_Analog.ino | + | Le programme est téléchargeable sur ce lien: {{ : |
+ | |||
< | < | ||
- | #include " | ||
+ | ///////// MOdule MP3 /////////////////////////// | ||
+ | #include " | ||
#ifdef __AVR__ | #ifdef __AVR__ | ||
#include < | #include < | ||
- | SoftwareSerial SSerial(5, 6); // RX, TX | + | SoftwareSerial SSerial(5, 6); // 5-RX, 6-TX |
#define COMSerial SSerial | #define COMSerial SSerial | ||
#define ShowSerial Serial | #define ShowSerial Serial | ||
Ligne 167: | Ligne 405: | ||
uint8_t status; | uint8_t status; | ||
boolean etat_entree_signal_ou_bouton = false; | boolean etat_entree_signal_ou_bouton = false; | ||
+ | boolean etat_bouton = false; | ||
// entrée analogique | // entrée analogique | ||
- | #define ENTREE_DETECTEUR_SIGNAL A1 | + | #define ENTREE_DETECTEUR_SIGNAL A1 ////////// Système de mesure du signal venant d |
#define SEUIL_DETECTION 100 // déclenchement du son si l' | #define SEUIL_DETECTION 100 // déclenchement du son si l' | ||
int entreeDetecteur = 0; // variable de mémorisation du signal d' | int entreeDetecteur = 0; // variable de mémorisation du signal d' | ||
Ligne 282: | Ligne 521: | ||
void audio(int cmd) { | void audio(int cmd) { | ||
- | ShowSerial.print("play:"); | + | ShowSerial.println("PLAY"); |
if (workdisk == SD) { | if (workdisk == SD) { | ||
Mp3Player.playSDRootSong(cmd); | Mp3Player.playSDRootSong(cmd); | ||
ShowSerial.print(" | ShowSerial.print(" | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.print(cmd); | ||
+ | ShowSerial.print(" | ||
ShowSerial.println(SDSong[cmd - ' | ShowSerial.println(SDSong[cmd - ' | ||
} | } | ||
Ligne 333: | Ligne 575: | ||
entreeDetecteur = analogRead(ENTREE_DETECTEUR_SIGNAL); | entreeDetecteur = analogRead(ENTREE_DETECTEUR_SIGNAL); | ||
- | + | etat_bouton | |
- | etat_entree_signal_ou_bouton | + | etat_entree_signal_ou_bouton = (etat_bouton |
// debug | // debug | ||
- | | + | // |
- | // ShowSerial.println(etat_entree_signal_ou_bouton); | + | |
if (etat_entree_signal_ou_bouton == true && (status == STATUS_STOP || status == STATUS_PAUSE) ) { | if (etat_entree_signal_ou_bouton == true && (status == STATUS_STOP || status == STATUS_PAUSE) ) { | ||
Ligne 350: | Ligne 592: | ||
ShowSerial.print(" | ShowSerial.print(" | ||
ShowSerial.println(entreeDetecteur); | ShowSerial.println(entreeDetecteur); | ||
+ | ShowSerial.print(" | ||
+ | ShowSerial.println(etat_bouton); | ||
// si bouton appuyé ET que le son ne joue pas 0x02:stop, 0x03: pause | // si bouton appuyé ET que le son ne joue pas 0x02:stop, 0x03: pause |