Erreurs inconnues

Bonjour, je réalise un projet qui utilise arduino uno & shield et beaucoup de capteurs (la plupart d'entre eux connectés en i2c)
Et je tente de créer un programme qui regroupe dans une carte sd toutes les données recueillies par les capteurs.

Mais le vérificateur arduino me dit qu'il y a quelques erreurs, mais quand je les corrige, d'autres "étranges" erreurs apparaîssent!
Voici mon programme, pouvez-vous me dire ce qu'il ne va pas, s'il vous plaît?

#include<SD.h>
#include<SPI.h>
#include<Wire.h>

int address_sensor1= 72; //binary equivalent is 1001000
int address_sensor2= 73; //binary equivalent is 1001001
int address_sensor3= 74; //binary equivalent is 1001100
int address_sensor4= 75; //binary equivalent is 1001101

File Data;
void setup() {
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Wire.begin();
Serial.begin(9600);

if (!SD.begin(4)) {
Serial.println("Initialisation impossible !");
return;
}
Serial.println("Initialisation OK.");

// Check to see if the file exists:
if (!SD.exists("Data.txt")) {
Serial.println("Data.txt existe.");
}
else {
Serial.println("Data.txt n'existe pas.");
Serial.println("Création de Data.txt...");
Data = SD.open("Data.txt", FILE_WRITE);
Data.close();
}
Data = SD.open("Data.txt", FILE_WRITE);
if (Data) {
Serial.print("Ecriture sur Data.txt...");
Data.println("Test 1, 2, 3.");
// close the file:
Data.close();
Serial.println("OK.");
}
else {
// if the file didn't open, print an error:
Serial.println("Erreur d'ouverture du Data.txt");
}

{
void loop()

{ Wire.requestFrom(4, 6); // request 6 bytes from slave device #4

while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
Wire.beginTransmission(address_sensor1); //Send a request to begin communication with the device at the specified address

Wire.write(0); //Sends a bit asking for register 0, the data register of the TC74 sensor

Wire.endTransmission(); //this ends transmission of data from the arduino to the temperature sensor

//this now reads the temperature from the TC74 sensor
Wire.requestFrom(address_sensor1, 1);//this requests 1 byte from the specified address

while(Wire.available() == 0);
int celsius1= Wire.read();
Serial.print(celsius1)}
}

// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:
dataString += String(celsius1)
delay(2000);

}

delay(500);
}
}

Merci !²

bonjour,
1- code entre balise code </>
2 fais un formatage de ton code via l'ide
tu verras les erreurs

void setup non fermé par un }

 {
    void loop()
        Serial.print(celsius1)

} qui ferme avant la fin du code pour le loop

    dataString += String(celsius1)

code corrigé, mais la prochaine fois mets les erreurs, j'ai pas vérifié si ca fonctionne ou non, mais ca compile

#include<SD.h>
#include<SPI.h>
#include<Wire.h>



int address_sensor1 = 72; //binary equivalent is 1001000
int address_sensor2 = 73; //binary equivalent is 1001001
int address_sensor3 = 74; //binary equivalent is 1001100
int address_sensor4 = 75; //binary equivalent is 1001101
int celsius1;
File Data;
void setup() {
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Wire.begin();
  Serial.begin(9600);



  if (!SD.begin(4)) {
    Serial.println("Initialisation impossible !");
    return;
  }
  Serial.println("Initialisation OK.");


  // Check to see if the file exists:
  if (!SD.exists("Data.txt")) {
    Serial.println("Data.txt existe.");
  }
  else {
    Serial.println("Data.txt n'existe pas.");
    Serial.println("Création de Data.txt...");
    Data = SD.open("Data.txt", FILE_WRITE);
    Data.close();
  }
  Data = SD.open("Data.txt", FILE_WRITE);
  if (Data) {
    Serial.print("Ecriture sur Data.txt...");
    Data.println("Test 1, 2, 3.");
    // close the file:
    Data.close();
    Serial.println("OK.");
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("Erreur d'ouverture du Data.txt");
  }

} 
    void loop()


    {
      Wire.requestFrom(4, 6);    // request 6 bytes from slave device #4

      while (Wire.available()) { // slave may send less than requested
        char c = Wire.read(); // receive a byte as character
        Serial.print(c);         // print the character
        Wire.beginTransmission(address_sensor1); //Send a request to begin communication with the device at the specified address

        Wire.write(0); //Sends a bit asking for register 0, the data register of the TC74 sensor

        Wire.endTransmission(); //this ends transmission of data from the arduino to the temperature sensor

        //this now reads the temperature from the TC74 sensor
        Wire.requestFrom(address_sensor1, 1);//this requests 1 byte from the specified address

        while (Wire.available() == 0);
         celsius1 = Wire.read();
        Serial.print(celsius1);
      }
   

    // make a string for assembling the data to log:
    String dataString = "";

    // read three sensors and append to the string:
    dataString += String(celsius1);
                  delay(2000);





  delay(500);

}

Désolé je ne sais pas comment afficher les erreurs avec le code, je te remercie de ton aide !
:slight_smile:

ma04nu:
Désolé je ne sais pas comment afficher les erreurs avec le code, je te remercie de ton aide !
:slight_smile:

lorsque tu lance la compilation l'IDE affiche des erreurs, suffit de les copier et coller dans un post du forum

Bonjour,

infobarquee:
lorsque tu lance la compilation l'IDE affiche des erreurs, suffit de les copier et coller dans un post du forum

A l'ancienne ctrl+C et crtl+V

:slight_smile:
la jeunesse connait elle les raccourcis pour le copier/coller?

Je n'avais pas pensé a a les coller en dessous désolé ^^, sinon oui je pense que la plupart d'entre nous( les jeunes mdr) connaissent les ctrl+x , ctrl+c etc..... :slight_smile:

Désolé de vous déranger (encore) mais quelqu'un aurait un peu de temps a perdre pour m'aider avec mon code, je n'arrive pas a résoudre une erreur:
#include<SD.h>
#include<SPI.h>
#include<Wire.h>

int address_sensor1 = 72;
int address_sensor2 = 73;
int address_sensor3 = 74;
int address_sensor4 = 75;
int celsius1;
int Data2;

File Data;
void setup() {
while (!Serial) {
; // Attends le port de serie pour se connecter.
}

Wire.begin();
Serial.begin(9600);

if (!SD.begin(4)) {
Serial.println("Initialisation impossible !");
return;
}
Serial.println("Initialisation OK.");

// Vérifie si le fichier existe:
if (!SD.exists("Data.txt")) {
Serial.println("Data.txt existe.");
}
else {
Serial.println("Data.txt n'existe pas.");
Serial.println("Création de Data.txt...");
Data = SD.open("Data.txt", FILE_WRITE);
Data.close();
}
Data = SD.open("Data.txt", FILE_WRITE);
if (Data) {
Serial.print("Ecriture sur Data.txt...");
Data.println("Test 1, 2, 3.");
// close the file:
Data.close();
Serial.println("OK.");
}
else {
// if the file didn't open, print an error:
Serial.println("Erreur d'ouverture du Data.txt");
}

}
void loop()

{
Wire.requestFrom(4, 6); // demande 6 bytes de l'esclave n*4

while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
Wire.beginTransmission(address_sensor1); //Send a request to begin communication with the device at the specified address

Wire.write(0); //Sends a bit asking for register 0, the data register of the TC74 sensor

Wire.endTransmission(); //this ends transmission of data from the arduino to the temperature sensor

//this now reads the temperature from the TC74 sensor
Wire.requestFrom(address_sensor1, 1);//this requests 1 byte from the specified address

while (Wire.available() == 0);
celsius1 = Wire.read();
Serial.print(celsius1);
}

// make a string for assembling the data to log:
String dataString = "";

// read three sensors and append to the string:
dataString += String(celsius1);
delay(2000);
Data2 = (String);
Data2 = SD.open("Data.txt", FILE_WRITE);
Data.println(Data);
Data.close();

delay(500);

}

LES ERREURS :

F:\PROJET\CODE SOURCE\En_cours__\En_cours__.ino: In function 'void loop()':

En_cours__:86: error: expected primary-expression before ')' token

Data2 = (String);

^

exit status 1
expected primary-expression before ')' token

MERCI !!!

Bonjour,
Nous les ancien (mdr), on lit les messages épinglés du forum (codes balises)et on met le code entre balises :wink:

hello ne serait ce pas :

Data2 = (dataString);
que tu devrais mettre

edit: +1 avec icare. mets ton code entre les balises prévues à cet effet

Désolé, comme ça c'est mieux ?
Mon Code

#include<SD.h>
#include<SPI.h>
#include<Wire.h>



int address_sensor1 = 72; 
int address_sensor2 = 73; 
int address_sensor3 = 74; 
int address_sensor4 = 75; 
int celsius1;
int Data2;

File Data;
void setup() {
 while (!Serial) {
   ; // Attends le port de serie pour se connecter.
 }

 Wire.begin();
 Serial.begin(9600);



 if (!SD.begin(4)) {
   Serial.println("Initialisation impossible !");
   return;
 }
 Serial.println("Initialisation OK.");


 // Vérifie si le fichier existe:
 if (!SD.exists("Data.txt")) {
   Serial.println("Data.txt existe.");
 }
 else {
   Serial.println("Data.txt n'existe pas.");
   Serial.println("Création de Data.txt...");
   Data = SD.open("Data.txt", FILE_WRITE);
   Data.close();
 }
 Data = SD.open("Data.txt", FILE_WRITE);
 if (Data) {
   Serial.print("Ecriture sur Data.txt...");
   Data.println("Test 1, 2, 3.");
   // close the file:
   Data.close();
   Serial.println("OK.");
 }
 else {
   // if the file didn't open, print an error:
   Serial.println("Erreur d'ouverture du Data.txt");
 }

} 
   void loop()


   {
     Wire.requestFrom(4, 6);    // demande 6 bytes de l'esclave n*4

     while (Wire.available()) { // slave may send less than requested
       char c = Wire.read(); // receive a byte as character
       Serial.print(c);         // print the character
       Wire.beginTransmission(address_sensor1); //Send a request to begin communication with the device at the specified address

       Wire.write(0); //Sends a bit asking for register 0, the data register of the TC74 sensor

       Wire.endTransmission(); //this ends transmission of data from the arduino to the temperature sensor

       //this now reads the temperature from the TC74 sensor
       Wire.requestFrom(address_sensor1, 1);//this requests 1 byte from the specified address

       while (Wire.available() == 0);
        celsius1 = Wire.read();
       Serial.print(celsius1);
     }
  

   // make a string for assembling the data to log:
   String dataString = "";

   // read three sensors and append to the string:
   dataString += String(celsius1);
                 delay(2000);
   Data2 = (String);
   Data2 = SD.open("Data.txt", FILE_WRITE);
   Data.println(Data);
   Data.close();




 delay(500);

}

Les erreurs :

F:\PROJET\CODE SOURCE\En_cours__\En_cours__.ino: In function 'void loop()':

En_cours__:86: error: expected primary-expression before ')' token

    Data2 = (String);

                   ^

exit status 1
expected primary-expression before ')' token

Merci

et la solution que je t'ai proposée plus haut?

tu compiles ton code tel qu'il est
tu as le code d'erreur
et dans le code, la ligne concernée est en surlignée

Merci de ta réponse dfgh mais quand je remplace le Data2 = (String) avec ton morceau de code, le compilateur affiche :

F:\PROJET\CODE SOURCE\En_cours__\En_cours__.ino: In function 'void loop()':

En_cours__:86: error: cannot convert 'String' to 'int' in assignment

     Data2 = (dataString);

           ^

exit status 1
cannot convert 'String' to 'int' in assignment

Thanks people !

Je vais peut être sire une bêtise mais à mon avis, ton Data2 ne sert à rien...
Premièrement: tu l'initialise en tant que int (ligne 13) et tu fais un Data2 = SD.open("Data.txt", FILE_WRITE);
or cette fonction retourne un File, pas un int.
En plus, ce Data2, tu ne l'utilises nul part car tu écrit tes données dans Data (ligne 89)
Et pour ton Data2 = (String);
Je ne pense pas que ce soit du code valide, tu peux retirer cette ligne aussi.
Bref, pour résumer:
supprime la ligne:
Data2 = (String);

remplace la ligne:
Data2 = SD.open("Data.txt", FILE_WRITE);
par
Data = SD.open("Data.txt", FILE_WRITE);

ça devrait marcher

J'ai oublié:
remplace ton Data.println(Data) par Data.println(dataString)

Merci Kor6k, le code est bon, je teste sa avec ma maquette et je vous dit tout ^^
:slight_smile:

Bon après un petit test je commence a désespérer, mon programme sensé récupérer les données d'une chaîne de capteurs en I2C et les stocker dans un fichier sur une carte SD ne sert a rien.
Si quelqu'un a des suggestions je suis preneur !

Je réup mon code au cas ou :

 #include<SD.h>
#include<SPI.h>
#include<Wire.h>



int address_sensor1 = 72; 
int address_sensor2 = 73; 
int address_sensor3 = 74; 
int address_sensor4 = 75; 
int celsius1;


File Data;
void setup() {
  while (!Serial) {
    ; // Attends le port de serie pour se connecter.
  }

  Wire.begin();
  Serial.begin(9600);



  if (!SD.begin(4)) {
    Serial.println("Initialisation impossible !");
    return;
  }
  Serial.println("Initialisation OK.");


  // Vérifie si le fichier existe:
  if (!SD.exists("Data.txt")) {
    Serial.println("Data.txt existe.");
  }
  else {
    Serial.println("Data.txt n'existe pas.");
    Serial.println("Création de Data.txt...");
    Data = SD.open("Data.txt", FILE_WRITE);
    Data.close();
  }
  Data = SD.open("Data.txt", FILE_WRITE);
  if (Data) {
    Serial.print("Ecriture sur Data.txt...");
    Data.println("Test 1, 2, 3.");
    // close the file:
    Data.close();
    Serial.println("OK.");
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("Erreur d'ouverture du Data.txt");
  }

} 
    void loop()


    {
      Wire.requestFrom(4, 6);    // demande 6 bytes de l'esclave n*4

      while (Wire.available()) { // slave may send less than requested
        char c = Wire.read(); // receive a byte as character
        Serial.print(c);         // print the character
        Wire.beginTransmission(address_sensor1); //Send a request to begin communication with the device at the specified address

        Wire.write(0); //Sends a bit asking for register 0, the data register of the TC74 sensor

        Wire.endTransmission(); //this ends transmission of data from the arduino to the temperature sensor

        //this now reads the temperature from the TC74 sensor
        Wire.requestFrom(address_sensor1, 1);//this requests 1 byte from the specified address

        while (Wire.available() == 0);
         celsius1 = Wire.read();
        Serial.print(celsius1);
      }
   

    // make a string for assembling the data to log:
    String dataString = "";

    // read three sensors and append to the string:
    dataString += String(celsius1);
                  delay(2000);
 
    Data = SD.open("Data.txt", FILE_WRITE);
    Data.println(dataString);
    Serial.print(dataString);
    Serial.print(Data);
    Data.close();




  delay(500);

}

mais encore ?

s'il ne sert à rien alors pourquoi le faire ? :slight_smile:

tu ne lis qu'un seul capteur en boucle

quels sont tes capteurs?

Beh justement je ne sais pas comment orienter le code de façon a lire les données reçu du bus I2C
(Pin a4 et a5 sur la Shield)
(et quand je dit qu'il ne sert a rien, je veux dire que dans son état actuel il est loin d'etre utile)

Merci

alors lit il quelque chose?
le serial print celsius1 dit quoi ?

ce n'est pas ton code. est il adapté à tes capteurs?

quelle est la ref de tes capteurs

Le dataString et le Data affichent une série de 010101..... et lorsque je bouge les 3 potentiomètres (qui remplacent les capteurs de températures, d'humidité, et de poids dans ma maquette) rien ne change.

Le serial print celsius1 semble (?) ne rien afficher, mais je pense que c'est parce qu'il n'est pas prévu pour de l'I2C.

La référence de mes capteurs (définitifs) est-elle importante ? Car ils seront tous liés en I2C et transmettrons leurs données a la chaîne.

Merci