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
}