Besoin d aide pour mon code SVP

Bonjour, je fait face à un petit probleme pour mon code.

J'utilise un réveil capable de détecter les phases du sommeil grace à un bandeau pourvu d' electrode ( réveil ZEO )
Les informations du réveil sont transmisent à un module arduino.
Le module les décrypte et quand la phase recherchée est détectée, il envoie une série de flash lumineux.

int incomingByte = 0;   
byte header[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
int NOMBRE = 0; // COMPTEUR
int SIZE = 0;
int *DATA;
int val;
int REMnombre = 0;
unsigned long digit = 0;


int dixsecondes = 10000;         //delay 10 secondes
int uneheure = 3600000;          //delay 3600 secondes
int troisheures = 10800000;      //delay 10800 secondes


////////////////////////////////////////////////

void setup() {
  
   
  
  Serial.begin(38400);  // ouverture du port serial du ZEO, regle le debit donnees à 38400 bps
  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  pinMode(10, OUTPUT);  // LED acquisition de données provenant du ZEO
  pinMode(11, OUTPUT);  // LED messages visuels REM

 Serial.println(" Void setup ok ");delay(4000); 




// Test LED 1 (messages visuels)

Serial.println(" Test LED 1 ");

 analogWrite(ledPin, 0);delay(500); 
 analogWrite(ledPin, 63);delay(100);
 analogWrite(ledPin, 0);delay(500); 
 analogWrite(ledPin, 127);delay(100);
 analogWrite(ledPin, 0);delay(500); 
 analogWrite(ledPin, 190);delay(100);
 analogWrite(ledPin, 0);delay(500); 
 analogWrite(ledPin, 255);delay(100);
 analogWrite(ledPin, 0);

 
 
// Test LED 2 (messages visuels)


Serial.println(" Test LED 2 ");


  digitalWrite(11, LOW);    // set the LED off
  delay(500);  
  digitalWrite(11, HIGH);   // set the LED on
  delay(100);    // wait for a second
  
  digitalWrite(11, LOW);    // set the LED off
  delay(500);              // wait for a second
  digitalWrite(11, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(11, LOW);    // set the LED off
  delay(500);  
  digitalWrite(11, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(11, LOW);    // set the LED off
  delay(500);              // wait for a second
  digitalWrite(11, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(11, LOW);    // set the LED off
 

 
// Test LED 3 acquisition de données provenant du ZEO


Serial.println(" Test LED 3 ");

  
  digitalWrite(10, LOW);    // set the LED off
  delay(500);  
  digitalWrite(10, HIGH);   // set the LED on
  delay(100);    // wait for a second
  
  digitalWrite(10, LOW);    // set the LED off
  delay(500);              // wait for a second
  digitalWrite(10, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(10, LOW);    // set the LED off
  delay(500);  
  digitalWrite(10, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(10, LOW);    // set the LED off
  delay(500);              // wait for a second
  digitalWrite(10, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(10, LOW);    // set the LED off  
  
  
  
// Test LED 1 et 2 


delay(dixsecondes);Serial.println("10s avant Test LED 1 et 2");
Serial.println(" Test LED 1 et 2 ");

  
  digitalWrite(11, LOW); analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW); analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off  
  
  
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop() {
        // attend les informations
        if (Serial.available() > 0) {
                // lecture des donnees :
                incomingByte = Serial.read();
                if ( incomingByte == 65 ) { holeHeader(); }  // nouvelles informations arrivent
               }
              
}


////////////////////////////////////////////////

void holeHeader() {
  NOMBRE = 0;
  while ( NOMBRE < 10 ) {
        if (Serial.available() > 0) {
                header[NOMBRE] = Serial.read();
                NOMBRE++;}
              }
  SIZE = (( header[3] * 256 ) + header[2]);
  if ( SIZE > 20 ) { SIZE = 1; } // dont read long raw waveform packages
  if ( header[3] + header[5] == 255 & header[2] + header[4] == 255 ) { holeDaten(SIZE); }
  else { Serial.println(" CAPTEURS = AUCUNES INFORMATIONS DETECTEES ");

  digitalWrite(10, LOW);    // set the LED off
  delay(500);  
  digitalWrite(10, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(10, LOW);    // set the LED off
  delay(500);              // wait for a second
  digitalWrite(10, HIGH);   // set the LED on
  delay(100);              // wait for a second
  
  digitalWrite(10, LOW);    // set the LED off 


}
  }
 

////////////////////////////////////////////////
  
void holeDaten(int SIZE) {
DATA = (int*)calloc(1, SIZE * sizeof(int));
NOMBRE = 0;
while ( NOMBRE < SIZE ) {
  if (Serial.available() > 0) {
    DATA[NOMBRE] = Serial.read();
    NOMBRE++;
  }
}
if ( DATA[0] == 157 ) { Serial.print("Phase de sommeil ");
                       if (DATA[1] == 0) { Serial.println(": INCONNUE"); }
                       if (DATA[1] == 1) { Serial.println(": EVEIL"); REMnombre = 0 ; }
                       if (DATA[1] == 2) { Serial.println(": REM //phase cible// "); REMnombre++; }
                       if (DATA[1] == 3) { Serial.println(": LEGER"); REMnombre = 0; }
                       if (DATA[1] == 4) { Serial.println(": PROFOND"); REMnombre = 0; }
                       Serial.print("numero = "); Serial.println(REMnombre);
                      }
if ( REMnombre > 10 ) { LEDflash(); REMnombre = 0; } // si la phase cible est superieure à 10 lectures (5 min) , lancer la fonction LEDflash (8 flshs )
free(DATA);
}


////////////////////////////////////////////////

void LEDflash() {

  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW); analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second
  
  digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
  delay(100);              // wait for a second
  digitalWrite(11, LOW);analogWrite(ledPin, 0);    // set the LED off
  delay(500);              // wait for a second  
  
}

jusque la tout fonctionne.

Le module arduino recherche la phase de sommeil souhaitée( ici : REM //phase cible// )

if ( DATA[0] == 157 ) { Serial.print("Phase de sommeil ");
** if (DATA[1] == 0) { Serial.println(": INCONNUE"); }**
** if (DATA[1] == 1) { Serial.println(": EVEIL"); REMnombre = 0 ; }**
** if (DATA[1] == 2) { Serial.println(": REM //phase cible// "); REMnombre++; }**
** if (DATA[1] == 3) { Serial.println(": LEGER"); REMnombre = 0; }**
** if (DATA[1] == 4) { Serial.println(": PROFOND"); REMnombre = 0; }**
** Serial.print("numero = "); Serial.println(REMnombre);**
** }**
if ( REMnombre > 10 ) { LEDflash(); REMnombre = 0; } // si la phase cible est superieure à 10 lectures (5 min)
free(DATA);

Une fois qu il détecte les 5 mn d affilées de REM //phase cible//, il lance une série de 8 flashs lumineux

if ( REMnombre > 10 ) { LEDflash(); REMnombre = 0; } // si la phase cible est superieure à 10 lectures (5 min) , lancer la fonction LEDflash (8 flshs )
free(DATA);

Je voudrais ajouter quelque chose...
Je voudrais que les flashs soient envoyé aprés avoir trouvé une phase de sommeil léger suivie d'une phase de REM (phase cible dans le code ci dessus)

C'est quoi la phase cible ? cauchemars ? ]:smiley:

Sinon par exemple plutôt que de faire

int dixsecondes = 10000;         //delay 10 secondes
int uneheure = 3600000;          //delay 3600 secondes
int troisheures = 10800000;      //delay 10800 secondes

j'aurais fais

#define 10s 10000
#define 1h 3600000
#define 3h 10800000

Pour ta fonction LEDflash c'est un peux répétitif donc (idem pour les testes)

  void LEDflash() {

  for(int i = 0;i <=7;i++ ){
      digitalWrite(11, HIGH); analogWrite(ledPin, 255);   // set the LED on
      delay(100);              // wait for a second
      digitalWrite(11, LOW); analogWrite(ledPin, 0);    // set the LED off
      delay(500);              // wait for a second
  }
}

manque "ledPin" je suppose que ça déclaration à disparue dans le copier coller ?

Pour ce que tu veux faire, entre 2 REMnombre++ il peux y avoir une autre phase ? ou c'est obligatoirement 10 REM daffilée sans autre phase intermédiaire ?

Si il n'y a pas d'intermédiaire

.
.
.
if (DATA[1] == 3) { Serial.println(": LEGER"); leger = true; REMnombre = 0; }
if (DATA[1] == 4) { Serial.println(": PROFOND"); leger = false;REMnombre = 0; }
.
.
.

if ( leger && REMnombre > 10 )
{ 
    LEDflash();
    leger false; 
    REMnombre = 0; 
}

faut dormir pour débuger ou avoir quelqu'un qui dors à côté ? :sleeping: :grin: