Plusieurs programmes dans un seul

Bonsoir,

Je travaille sur un projet avec plusieurs capteurs. Ils marchent bien séparément.
J'aimerais les regrouper dans un seul programme j'ai testé plusieurs choses mais cela ne marche pas.

Avez-vous des idées ?

Bonjour,

Tinita:
Avez-vous des idées ?

Oui.
Poste ton code :wink:

Oui, bien sûr, j'ai 3 capteurs. Les codes sont les uns après les autres pour que tu les distingues bien.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
 
 
// Here we are using IIC
 
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
 
void setup() {
Serial.begin(9600);
Serial.println(F("BMP280 test"));
 
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
}
 
void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
 
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
 
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); // this should be adjusted to your local forcase
Serial.println(" m");
 
Serial.println();
delay(2000);
}








int DHpin = 8;
byte dat [5];
byte read_data () {
byte data;
for (int i = 0; i < 8; i ++) {
if (digitalRead (DHpin) == LOW) {
while (digitalRead (DHpin) == LOW);
delayMicroseconds (30);
if (digitalRead (DHpin) == HIGH)
data |= (1 << (7-i));
while (digitalRead (DHpin) == HIGH);
}
}
return data;
}
void start_test () {
digitalWrite (DHpin, LOW);
delay (30);
digitalWrite (DHpin, HIGH);
delayMicroseconds (40);
pinMode (DHpin, INPUT);
while (digitalRead (DHpin) == HIGH);
delayMicroseconds (80);
if (digitalRead (DHpin) == LOW);
delayMicroseconds (80);
for (int i = 0; i < 4; i ++)
dat[i] = read_data ();
pinMode (DHpin, OUTPUT);
digitalWrite (DHpin, HIGH);
}
void setup () {
Serial.begin (9600);
pinMode (DHpin, OUTPUT);
}
void loop () {
start_test ();
Serial.print ("Current humdity =");
Serial.print (dat [0], DEC);
Serial.print ('.');
Serial.print (dat [1], DEC);
Serial.println ('%');
Serial.print ("Current temperature =");
Serial.print (dat [2], DEC);
Serial.print ('.');
Serial.print (dat [3], DEC);
Serial.println ('C');
delay (700);
}














#define         MQ_PIN                       (0)     //define which analog input channel you are going to use
#define         RL_VALUE                     (5)     //define the load resistance on the board, in kilo ohms
#define         RO_CLEAN_AIR_FACTOR          (9.83)  //RO_CLEAR_AIR_FACTOR=(Sensor resistance in clean air)/RO,
                                                     //which is derived from the chart in datasheet
 
/***********************Software Related Macros************************************/
#define         CALIBARAION_SAMPLE_TIMES     (50)    //define how many samples you are going to take in the calibration phase
#define         CALIBRATION_SAMPLE_INTERVAL  (500)   //define the time interal(in milisecond) between each samples in the
                                                     //cablibration phase
#define         READ_SAMPLE_INTERVAL         (50)    //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_TIMES            (5)     //define the time interal(in milisecond) between each samples in 
                                                     //normal operation
 
/**********************Application Related Macros**********************************/
#define         GAS_LPG                      (0)
#define         GAS_CO                       (1)
#define         GAS_SMOKE                    (2)
 
/*****************************Globals***********************************************/
float           LPGCurve[3]  =  {2.3,0.21,-0.47};   //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent"
                                                    //to the original curve. 
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59) 
float           COCurve[3]  =  {2.3,0.72,-0.34};    //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent" 
                                                    //to the original curve.
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.72), point2: (lg10000,  0.15) 
float           SmokeCurve[3] ={2.3,0.53,-0.44};    //two points are taken from the curve. 
                                                    //with these two points, a line is formed which is "approximately equivalent" 
                                                    //to the original curve.
                                                    //data format:{ x, y, slope}; point1: (lg200, 0.53), point2: (lg10000,  -0.22)                                                     
float           Ro           =  10;                 //Ro is initialized to 10 kilo ohms
 
void setup()
{
  Serial.begin(9600);                               //UART setup, baudrate = 9600bps
  Serial.print("Calibrating...\n");                
  Ro = MQCalibration(MQ_PIN);                       //Calibrating the sensor. Please make sure the sensor is in clean air 
                                                    //when you perform the calibration                    
  Serial.print("Calibration is done...\n"); 
  Serial.print("Ro=");
  Serial.print(Ro);
  Serial.print("kohm");
  Serial.print("\n");
}
 
void loop()
{
   Serial.print("LPG:"); 
   Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_LPG) );
   Serial.print( "ppm" );
   Serial.print("    ");   
   Serial.print("CO:"); 
   Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_CO) );
   Serial.print( "ppm" );
   Serial.print("    ");   
   Serial.print("SMOKE:"); 
   Serial.print(MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) );
   Serial.print( "ppm" );
   Serial.print("\n");
   delay(200);
}
 
/****************** MQResistanceCalculation ****************************************

************************************************************************************/ 
float MQResistanceCalculation(int raw_adc)
{
  return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc));
}
 
/***************************** MQCalibration ****************************************

************************************************************************************/ 
float MQCalibration(int mq_pin)
{
  int i;
  float val=0;
 
  for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) {            //take multiple samples
    val += MQResistanceCalculation(analogRead(mq_pin));
    delay(CALIBRATION_SAMPLE_INTERVAL);
  }
  val = val/CALIBARAION_SAMPLE_TIMES;                   //calculate the average value
 
  val = val/RO_CLEAN_AIR_FACTOR;                        //divided by RO_CLEAN_AIR_FACTOR yields the Ro 
                                                        //according to the chart in the datasheet 
 
  return val; 
}
/*****************************  MQRead *********************************************

************************************************************************************/ 
float MQRead(int mq_pin)
{
  int i;
  float rs=0;
 
  for (i=0;i<READ_SAMPLE_TIMES;i++) {
    rs += MQResistanceCalculation(analogRead(mq_pin));
    delay(READ_SAMPLE_INTERVAL);
  }
 
  rs = rs/READ_SAMPLE_TIMES;
 
  return rs;  
}
 
/*****************************  MQGetGasPercentage **********************************

************************************************************************************/ 
int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
{
  if ( gas_id == GAS_LPG ) {
     return MQGetPercentage(rs_ro_ratio,LPGCurve);
  } else if ( gas_id == GAS_CO ) {
     return MQGetPercentage(rs_ro_ratio,COCurve);
  } else if ( gas_id == GAS_SMOKE ) {
     return MQGetPercentage(rs_ro_ratio,SmokeCurve);
  }    
 
  return 0;
}
 
/*****************************  MQGetPercentage **********************************

************************************************************************************/ 
int  MQGetPercentage(float rs_ro_ratio, float *pcurve)
{
  return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}

Le sujet est abordé presque tous les mois, un peu de recherche préalable pour voir les conseils qui ont été donnés est le minimum.
Ne pense tu pas que nous sommes lassés de répéter toujours la mêmechose ?

Pour commencer se contenter de dire sans autre explication que cela ne marche pas ne sert à rien, si tu poses une question on se doute bien que c'est parce que cela ne marche pas.

Expliquer ce que tu as fait et dire ce que tu constate et qui te fait dire que cela ne marche pas serait bien plus efficace.

Quelques vérifications élémentaires :
redéclaration de variable
utilisation conflictuelle des mêmes E/S
utilisation conflictuelle des mêmes timers
redéclaration d'un Serial.begin(xxxxx)
etc..........

Une fois cet état des lieux effectué commencer par assembler 2 programmes et une fois fait ajouter le 3eme.

J'ai fait des recherches préalables, j'ai essayé de faire plusieurs void pour des sous-programmes, mais je n'ai pas tout compris (je débute) et cela ne marchait pas.

hé bé, avec ça t'es pas prêt d'avoir de l'aide !
Relis attentivement le message de 68tjs pour commencer.

mais je n'ai pas tout compris (je débute) et cela ne marchait pas.

Cela ne marchais pas : a cela je répond c'est normal ça n'a pas de jambe.

Cela ne marche pas ne veux strictement rien dire tant que tu n'identifie pas ce qui marche pas.
Il y a des millions de causes possible et hors cet instrument :
boule_de_cristal.jpg
impossible de répondre.

je débute

Il faut bien débuter un jour, tout le monde est passé par là avec son lot de bêtises, il n'y a pas de sur-Humains ici.

Mais il y a débuter et débuter.
Dans le message de bienvenue, que pas une seconde je n'imagine que tu ai pu ignorer, nous conseillons de commencer par le tuto d'Eskimon et de faire des exemples simples, très très simples et d'augmenter la difficulté progressivement.
Qu'est-ce que tu as fait : tu as trouvé 3 programmes tout fait que tu veux assembler, ce n'est pas la bonne approche.
Commence par ré-écrire le premier programme, pas de copie de la ré-écriture et en tapant chaque ligne demande toi à quoi elle sert, tu aura fais un grand pas en avant dans la compréhension.

En complément du tuto d'Eskimon la consultation d'un tuto (basique pour débutant) de C serait bien utile. Cela t'aurais éviter de dire n'importe quoi à propos de "void".
"void" c'est un type ce n'est pas une fonction, void en anglais signifie "vide". Cela sert à indiquer au compilateur que la fonction ne renvoie rien.
Si devant le nom de la fonction il y a "int" à la place de "void" cela indique au compilateur que la fonction renvoie un entier de type integer.

On ne programme pas sans faire un travail d'apprentissage préalable.
Si tu part sur de bonnes bases la progression sera plus facile et plus rapide.

Merci, je vais décortiquer plus le code.