Partage
  • Partager sur Facebook
  • Partager sur Twitter

Plusieurs programmes dans un seul : Arduino

19 février 2018 à 19:45:12

Bonsoir,

j'aimerais mettre dans un seul programme Arduino des programmes correspondants à plusieurs capteurs,

comment peut-on faire svp ?

  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 8:43:07

Bonjour,

il est bien sûr possible de faire un programme pour gérer plusieurs capteurs en même temps sur un Arduino. Il faut par contre s'assurer que les capteurs sont bien connectés sur des broches distinctes sinon ça va poser des problèmes.

Il faudra mettre les différentes initialisations dans la partie setup() et les traitements les uns après les autres dans la partie loop()

  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 12:23:32

Merci, que veux-tu dire par des broches distinctes ? car certains utilisent la même.

Peux-tu me faire un plan pour illustrer ce que tu dis concernant la 2e partie de ton post ?

car j'ai essayé de mettre les codes à la suite suivant les void setup et void loop, mais cela ne fonctionnait pas.

  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 12:53:46

Ce serait plus Tinista a écrit:

Merci, que veux-tu dire par des broches distinctes ? car certains utilisent la même.

Les capteurs ne doivent PAS être sur les mêmes broches, sauf éventuellement dans le cas d'un bus mais ils doivent alors avoir des adresses distinctes.

Tinista a écrit:

Peux-tu me faire un plan pour illustrer ce que tu dis concernant la 2e partie de ton post ?

Un plan ??, mais ce sont des instructions.

Tinista a écrit:

car j'ai essayé de mettre les codes à la suite suivant les void setup et void loop, mais cela ne fonctionnait pas.

Si les capteurs sont sur les mêmes broches, c'est normal, CQFD.

Ce serait plus rapide de poster un exemple concret qui pose problème. Schéma avec références des pièces, programme, photos éventuelles.

-
Edité par clgbzh 20 février 2018 à 12:56:40

  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 13:29:28

Liste des capteurs :

- MQ2

- BMP180

- DHT11

J'ai un capteur sur 3.3 V et deux autres sur 5V.

Mais c'est quand je compile que cela ne fonctionne pas, "erreur status 1". 

  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 16:59:25

Tinista a écrit:

Mais c'est quand je compile que cela ne fonctionne pas, "erreur status 1". 


Il faudrait poster le programme pour avoir de l'aide.
  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 17:28:35

oui, j'attendais d'avoir fini les cours pour l'envoyer (codes séparés)

#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])));
}

 j'avais essayé de les regrouper comme ça

#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);

nt 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);
}


#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; 
 
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);
}



Serial.begin (9600);
pinMode (DHpin, OUTPUT);



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() {

Programme1()}
  void Programme1(){
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(5000);
  }

Programme2()}
void Programme2(){
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 (10000);


}

Programme3()}
void Programme3(){
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(15000);
}

}


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])));
}


j'avais modifié le temps de delay pour que les valeurs n'arrivent pas en même temps


-
Edité par Tinista 20 février 2018 à 17:31:50

  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 17:45:41

Il ne peut y avoir qu'une seule et unique fonction setup() ainsi qu'une seule fonction loop()

on évite aussi des doublons comme les lignes 78 et 88

Il faut faire très attention aux accolades, et pour s'en sortir il est indispensable de faire des indentations (décalages) pour que ça soit lisible. A chaque accolade ouvrante { il faut identifier l'accolade fermante }, 

entre les lignes 14 et 41 c'est totalement illisible, tellement c'est fouillis. 

d'autre part, par exemple en ligne 125

Programme2()}

n'a aucun sens

  • Partager sur Facebook
  • Partager sur Twitter
20 février 2018 à 17:49:51

d'accord, c'était un test pour regrouper les codes. J'avais fait des recherches où l'on mettait des void programmes, mais cela me semblait bizarre.

Je note pour les doublons et les indentations.

EDIT : J'ai testé à nouveau mais avec 2 capteurs, je n'ai que les valeurs du 1er qui s'affichent.

#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);

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);
Serial.println(F("BMP280 test"));
 
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}

pinMode (DHpin, OUTPUT);

}
 
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(1000);


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 (2000);

Serial.println();
delay(3000);

}



-
Edité par Tinista 20 février 2018 à 21:59:50

  • Partager sur Facebook
  • Partager sur Twitter
28 mai 2020 à 17:34:03

Bonsoir,

j'aimerais mettre dans un seul programme Arduino des programmes correspondants à plusieurs capteurs,

comment peut-on faire svp ?

/ déclarer la biblio LiquidCrystal
#include <LiquidCrystal.h>
//Déclarer une variable lcd avec les ports utlisés
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
// initialiser l'écran LCD
lcd.begin(16,2);
//Imprimer un message sur l'écran
lcd.setCursor(0,0);
lcd.print("LDR= ");
lcd.setCursor(1,1);
lcd.print("Med Yak");
}
void loop() {
int valeur = analogRead(A0);
lcd.setCursor(6,0);
lcd.print(valeur);
}
// déclarer la bibliothéque LiquidCrystal
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
lcd.begin(16,2);
lcd.setCursor(1,0);
lcd.print("Temp= ");
lcd.setCursor(10,0);
lcd.print("C");
lcd.setCursor(1,1);
lcd.print("Med Yak");
}
void loop() {
int Valeur=analogRead(A0);
int Vout=Valeur*5.0/1023*1000;
int temp=Vout/10;
lcd.setCursor(7,0);
lcd.print(temp);
}
/ Inclure la biblio LiquidCrystal
#include <LiquidCrystal.h>
//Définir les pins utilisés pour l'écran LCD
LiquidCrystal LCD(9, 8, 5, 4, 3, 2);
double AcsOffset=2.5; // la valeur du tension de sortie du capteur lorsque le courant =0
double Sensibl=0.066; // La valeur de la sensibilité du capteur
double courant=0;
double tension=0;
void setup() {
// Initialiser l'écran Lcd
LCD.begin(16,2);
}
void loop() {
// Définir la valeur lue par le capteur sur le pin A0
double ValeurLue=analogRead(A0);
// Convertir cette valeur à une tension comprise entre 0 et 5V
tension=(ValeurLue*5.0/1023);
// Définir la formule de calcule du courant
courant=(tension-AcsOffset)/Sensibl;
// Déplacer le curseur sue l'écran LCD
LCD.setCursor(0,0);
// Imprimer la valeur du courant sur l'écran LCD
LCD.print("Courant=");
LCD.print(courant);
LCD.print(" Amp");
delay(500);
}
int p=1;
int pwmpin=9;
void setup()
{
setPwmFrequency(pwmpin, 1);
analogWrite(pwmpin,p);
//Serial.begin(9600);//SERIAL COMMUNICATION NOT //NEEDED
//Serial.println("HAII");
//delay(5000);delay(5000);
}
void loop()
{
int a=analogRead(A2);
delay(1);
//Serial.println(a);
//Serial.print("P=");
//Serial.println(p);
if(a>220)
{
p=p-1;
analogWrite(pwmpin,p);
if(p<0)p=0;
}
if (a<205 )//&& a>150
{
p=p+1;
if(p>220)p=220;
analogWrite(pwmpin,p);
}
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}

-
Edité par AhmedBenSalah14 28 mai 2020 à 17:37:28

  • Partager sur Facebook
  • Partager sur Twitter
28 mai 2020 à 20:43:36

Bonjour,

ce topic date de 2018.. Il est d'usage a ne pas le déterrer, et d'en créer un nouveau. Tu as déjà de quoi faire en lisant les messages du dessus.. a commencer par le 2ème message au-dessus du tien:-°

ps: faudra penser à rendre le code lisible... le texte blanc n'est pas idéale pour donner envie au lecteuro_O

  • Partager sur Facebook
  • Partager sur Twitter
28 mai 2020 à 22:29:37

AhmedBenSalah14 a écrit:

Bonsoir,

j'aimerais mettre dans un seul programme Arduino des programmes correspondants à plusieurs capteurs,

comment peut-on faire svp ?

/ déclarer la biblio LiquidCrystal
#include <LiquidCrystal.h>
//Déclarer une variable lcd avec les ports utlisés
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
// initialiser l'écran LCD
lcd.begin(16,2);
//Imprimer un message sur l'écran
lcd.setCursor(0,0);
lcd.print("LDR= ");
lcd.setCursor(1,1);
lcd.print("Med Yak");
}
void loop() {
int valeur = analogRead(A0);
lcd.setCursor(6,0);
lcd.print(valeur);
}
// déclarer la bibliothéque LiquidCrystal
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup() {
lcd.begin(16,2);
lcd.setCursor(1,0);
lcd.print("Temp= ");
lcd.setCursor(10,0);
lcd.print("C");
lcd.setCursor(1,1);
lcd.print("Med Yak");
}
void loop() {
int Valeur=analogRead(A0);
int Vout=Valeur*5.0/1023*1000;
int temp=Vout/10;
lcd.setCursor(7,0);
lcd.print(temp);
}
/ Inclure la biblio LiquidCrystal
#include <LiquidCrystal.h>
//Définir les pins utilisés pour l'écran LCD
LiquidCrystal LCD(9, 8, 5, 4, 3, 2);
double AcsOffset=2.5; // la valeur du tension de sortie du capteur lorsque le courant =0
double Sensibl=0.066; // La valeur de la sensibilité du capteur
double courant=0;
double tension=0;
void setup() {
// Initialiser l'écran Lcd
LCD.begin(16,2);
}
void loop() {
// Définir la valeur lue par le capteur sur le pin A0
double ValeurLue=analogRead(A0);
// Convertir cette valeur à une tension comprise entre 0 et 5V
tension=(ValeurLue*5.0/1023);
// Définir la formule de calcule du courant
courant=(tension-AcsOffset)/Sensibl;
// Déplacer le curseur sue l'écran LCD
LCD.setCursor(0,0);
// Imprimer la valeur du courant sur l'écran LCD
LCD.print("Courant=");
LCD.print(courant);
LCD.print(" Amp");
delay(500);
}
int p=1;
int pwmpin=9;
void setup()
{
setPwmFrequency(pwmpin, 1);
analogWrite(pwmpin,p);
//Serial.begin(9600);//SERIAL COMMUNICATION NOT //NEEDED
//Serial.println("HAII");
//delay(5000);delay(5000);
}
void loop()
{
int a=analogRead(A2);
delay(1);
//Serial.println(a);
//Serial.print("P=");
//Serial.println(p);
if(a>220)
{
p=p-1;
analogWrite(pwmpin,p);
if(p<0)p=0;
}
if (a<205 )//&& a>150
{
p=p+1;
if(p>220)p=220;
analogWrite(pwmpin,p);
}
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
  • Partager sur Facebook
  • Partager sur Twitter
29 mai 2020 à 0:20:43

Bonjour,

Déterrage

Citation des règles générales du forum :

Avant de poster un message, vérifiez la date du sujet dans lequel vous comptiez intervenir.

Si le dernier message sur le sujet date de plus de deux mois, mieux vaut ne pas répondre.
En effet, le déterrage d'un sujet nuit au bon fonctionnement du forum, et l'informatique pouvant grandement changer en quelques mois il n'est donc que rarement pertinent de déterrer un vieux sujet.

Au lieu de déterrer un sujet il est préférable :

  • soit de contacter directement le membre voulu par messagerie privée en cliquant sur son pseudonyme pour accéder à sa page profil, puis sur le lien "Ecrire un message"
  • soit de créer un nouveau sujet décrivant votre propre contexte
  • ne pas répondre à un déterrage et le signaler à la modération

Je ferme ce sujet. En cas de désaccord, me contacter par MP.

  • Partager sur Facebook
  • Partager sur Twitter