Info pour Arduino mega et module 8 realis

Bonjour
Meilleurs voeux pour 2023 a tous
Je voudrais savoir si on peut mettre deux void setup() et deux void loop() car dans mo programme j’utilise 1void setup() et 1 void loop() pour mon module 8 relais et pour le même programme j’utilise 1void setup() et 1 void loop pour mon code.
Merci de votre réponse.
Cordialement

Non une seule fonction setup() et une seule fonction loop()

Sous réserve qu’il n’y ai pas de conflit sur les entrées/sorties, il est facile de concatener le code des deux setup() pour avoir un setup() unique.

Pour le loop(), ça dépend. Il faudrait me montrer les deux codes en fait et je vous arrange ça :slight_smile:

@+

BONJOUR
Je voudrais savoir si vous avez bien reçu le fichier.
Merci de votre réponse.
Cordialement
Bardiaux Daniel

Je n’ai rien reçu. Vous l’avez envoyé par MP ?

// J'utilise un module de 8 relais Arduino, une carte Arduino Méga 2560 et un I2C 4 fils
//8 Channel Reley Module code Guide
 
 
 int Relay1=6;//Defined Pin 6 as the variable
 int Relay2=7;//Defined Pin 7 as the variable
 int Relay3=8;//Defined Pin 8 as the variable
 int Relay4=9;//Defined Pin 9 as the variable
 int Relay5=10;//Defined Pin 10 as the variable

 void setup()

 {
  pinMode(Relay1,OUTPUT);// Set Pin6 as output
  pinMode(Relay2,OUTPUT);// set Pin7 as output
  pinMode(Relay3,OUTPUT);// set Pin8 as output
  pinMode(Relay4,OUTPUT);// set Pin9 as output
  pinMode(Relay5,OUTPUT);// set Pin10 as output

 }
 void loop()
 {
      //Channel1
  digitalWrite(Relay1,HIGH);//Turn off relay
  delay(1000);
  digitalWrite(Relay1,LOW);//Turn on relay
  delay(1000);

      //Channel2
  digitalWrite(Relay2,HIGH);//Turn off relay
  delay(2000);
  digitalWrite(Relay2,LOW);//Turn on relay
  delay(2000);

      //Channel3
  digitalWrite(Relay3,HIGH);//Turn off relay
  delay(3000);
  digitalWrite(Relay3,LOW);//Turn on relay
  delay(3000);

    //Channel4
  digitalWrite(Relay4,HIGH);//Turn off relay
  delay(4000);
  digitalWrite(Relay4,LOW);//Turn on relay
  delay(4000);

    //Channel5
  digitalWrite(Relay5,HIGH);//Turn off relay
  delay(5000);
  digitalWrite(Relay5,LOW);//Turn on relay
  delay(5000);
 }
 // here's the SCETCH for you again:

#include <SPI.h>
#include <LiquidCrystal_I2C.h>// Include previously added LiquidCrystal_I2C library LiquidCrystal_I2C lcd(0x3F, 20, 4); 
#include <Wire.h> // Include Wire library 
#include <MFRC522.h>
#include <MFRC522Extended.h>

 void setup() 
 {
 pinMode(1,OUTPUT); // high 
pinMode(2,OUTPUT);//down 
pinMode(3,OUTPUT); //right 
pinMode(4,OUTPUT); //Left 
pinMode(5,OUTPUT); // Magnet
 }

 void loop() 
{ 
digitalWrite(1, HIGH); //high 
digitalWrite(2, HIGH); //down 
digitalWrite(3, HIGH); //right 
digitalWrite(4, HIGH); //Left 
digitalWrite(5,HIGH); // Magnet 
lcd.init(); //In the setup the LCD is started
 lcd.backlight(); //backlight 
lcd.setCursor(0, 0);//The position of the first character is set here. In this case (0,0) means the first character in the first line. 
lcd.print(" Maerklin 7051"); 
lcd.setCursor(0, 1); 
lcd.print(" with ARDUINO"); 
delay(2000); 
delay(4000);//...wait 4 seconds 
digitalWrite(1, LOW); //high 
delay(4000); //...and wait a second. 
digitalWrite(1, HIGH); 
//................................... 
delay(2000); 
//................................................ 
digitalWrite(3, LOW); //right 
delay(3500);//...wait a second 
digitalWrite(3, HIGH); //And turn off again
 //.......................................... 
delay(2000); 
//................................................ 
digitalWrite(2, LOW); //down
 delay(2900); //...and wait a second. 
digitalWrite(2, HIGH);
 //........................................ 
digitalWrite(5, LOW); //Magnet //
delay(4000);//...wait a second //
digitalWrite(5, HIGH); //And turn off again 
delay(2000); 
//.......................................... 
digitalWrite(1, LOW); //high 
delay(4000); //...and wait a second. 
digitalWrite(1, HIGH); 
//........................................ 
digitalWrite(4, LOW); //Left 
delay(7500);//...wait a second 
digitalWrite(4, HIGH); //And turn off again
 //................................................ 
delay(2000); //.......................................... 
digitalWrite(2, LOW); //down 
delay(2900); //...and wait a second. 
digitalWrite(2, HIGH); 
//.......................................... 
digitalWrite(5, LOW); //Magnetoff 
delay(2000);//...wait a second 
digitalWrite(5, HIGH); //And turn off again
 //................................................ 
digitalWrite(1, LOW); //high
 delay(4000); //...and wait a second. 
digitalWrite(1, HIGH); 
//................................................ 
digitalWrite(3, LOW); //right 
delay(3500);//...wait a second 
digitalWrite(3, HIGH); //And turn off again 
//.................................................
 digitalWrite(2, LOW); //down 
delay(4200); //...and wait a second. 
digitalWrite(2, HIGH); 
//................................................ 
lcd.clear(); 
lcd.setCursor(1, 0); 
lcd.print(" Repeat "); l
cd.setCursor(0, 1); 
lcd.print(" in 15 SEC. "); 
delay(15000);
 }

Comme je pensais, aucun problème pour fusionner les deux init() mais cela ne va pas marcher avec les loop() car ils utilisent des Delay() qui bloquent l’exécution du loop() jusqu’à l’échéance du delay …

En fait il faut installer un mécanisme de co-routine qui va permettre de lancer l’équivalent de deux loop() indépendante, chacune se rend la main dans le cadre des Delay(). Je ne sais pas si je suis claire.

Sur Arduino, la librairie à utiliser s’appelle AceRoutine.

En gros loop() { … } va se transformer en COROUTINE_LOOP() { … } et vos delay() en COROUTINE_DELAY().

Un exemple : AceRoutine/HelloCoroutine.ino at develop · bxparks/AceRoutine · GitHub

Pour en savoir plus sur cette librairie : GitHub - bxparks/AceRoutine: A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.

Votre code ressemblerait à ça - ATTENTION pas compilé pas testé, je vous laisse grenouiller un peu hein :slight_smile:


#include <Arduino.h>
#include <AceRoutine.h>
using namespace ace_routine;

int Relay1=6;//Defined Pin 6 as the variable
int Relay2=7;//Defined Pin 7 as the variable
int Relay3=8;//Defined Pin 8 as the variable
int Relay4=9;//Defined Pin 9 as the variable
int Relay5=10;//Defined Pin 10 as the variable

#include <SPI.h>
#include <LiquidCrystal_I2C.h>// Include previously added LiquidCrystal_I2C library LiquidCrystal_I2C lcd(0x3F, 20, 4);
#include <Wire.h> // Include Wire library
#include <MFRC522.h>
#include <MFRC522Extended.h>

COROUTINE(myRelay) {
  COROUTINE_LOOP() {

  //Channel1
  digitalWrite(Relay1,HIGH);//Turn off relay
  COROUTINE_DELAY(1000);
  digitalWrite(Relay1,LOW);//Turn on relay
  COROUTINE_DELAY(1000);

  //Channel2
  digitalWrite(Relay2,HIGH);//Turn off relay
  COROUTINE_DELAY(2000);
  digitalWrite(Relay2,LOW);//Turn on relay
  COROUTINE_DELAY(2000);

  //Channel3
  digitalWrite(Relay3,HIGH);//Turn off relay
  COROUTINE_DELAY(3000);
  digitalWrite(Relay3,LOW);//Turn on relay
  COROUTINE_DELAY(3000);

  //Channel4
  digitalWrite(Relay4,HIGH);//Turn off relay
  COROUTINE_DELAY(4000);
  digitalWrite(Relay4,LOW);//Turn on relay
  COROUTINE_DELAY(4000);

  //Channel5
  digitalWrite(Relay5,HIGH);//Turn off relay
  COROUTINE_DELAY(5000);
  digitalWrite(Relay5,LOW);//Turn on relay
  COROUTINE_DELAY(5000);
  }
}

COROUTINE(myLcd) {
  COROUTINE_LOOP() {

  digitalWrite(1, HIGH); //high
  digitalWrite(2, HIGH); //down
  digitalWrite(3, HIGH); //right
  digitalWrite(4, HIGH); //Left
  digitalWrite(5,HIGH); // Magnet
  
  lcd.init(); //In the setup the LCD is started
  lcd.backlight(); //backlight
  lcd.setCursor(0, 0);//The position of the first character is set here. In this case (0,0) means the first character in the first line.
  lcd.print(" Maerklin 7051");
  lcd.setCursor(0, 1);
  lcd.print(" with ARDUINO");
  COROUTINE_DELAY(2000);
  COROUTINE_DELAY(4000);//…wait 4 seconds
  digitalWrite(1, LOW); //high
  COROUTINE_DELAY(4000); //…and wait a second.
  digitalWrite(1, HIGH);
  //…
  COROUTINE_DELAY(2000);
  //…
  digitalWrite(3, LOW); //right
  COROUTINE_DELAY(3500);//…wait a second
  digitalWrite(3, HIGH); //And turn off again
  //…
  COROUTINE_DELAY(2000);
  //…
  digitalWrite(2, LOW); //down
  COROUTINE_DELAY(2900); //…and wait a second.
  digitalWrite(2, HIGH);
  //…
  digitalWrite(5, LOW); //Magnet //
  COROUTINE_DELAY(4000);//…wait a second //
  digitalWrite(5, HIGH); //And turn off again
  COROUTINE_DELAY(2000);
  //…
  digitalWrite(1, LOW); //high
  COROUTINE_DELAY(4000); //…and wait a second.
  digitalWrite(1, HIGH);
  //…
  digitalWrite(4, LOW); //Left
  COROUTINE_DELAY(7500);//…wait a second
  digitalWrite(4, HIGH); //And turn off again
  //…
  COROUTINE_DELAY(2000); //…
  digitalWrite(2, LOW); //down
  COROUTINE_DELAY(2900); //…and wait a second.
  digitalWrite(2, HIGH);
  //…
  digitalWrite(5, LOW); //Magnetoff
  COROUTINE_DELAY(2000);//…wait a second
  digitalWrite(5, HIGH); //And turn off again
  //…
  digitalWrite(1, LOW); //high
  COROUTINE_DELAY(4000); //…and wait a second.
  digitalWrite(1, HIGH);
  //…
  digitalWrite(3, LOW); //right
  COROUTINE_DELAY(3500);//…wait a second
  digitalWrite(3, HIGH); //And turn off again
  //…
  digitalWrite(2, LOW); //down
  COROUTINE_DELAY(4200); //…and wait a second.
  digitalWrite(2, HIGH);
  //…
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print(" Repeat “);
  lcd.setCursor(0, 1);
  lcd.print(” in 15 SEC. ");
  COROUTINE_DELAY(15000);

  }
}

void setup() {
   // setup relay
  pinMode(Relay1,OUTPUT);// Set Pin6 as output
  pinMode(Relay2,OUTPUT);// set Pin7 as output
  pinMode(Relay3,OUTPUT);// set Pin8 as output
  pinMode(Relay4,OUTPUT);// set Pin9 as output
  pinMode(Relay5,OUTPUT);// set Pin10 as output

  // setup lcd
  pinMode(1,OUTPUT); // high
  pinMode(2,OUTPUT);//down
  pinMode(3,OUTPUT); //right
  pinMode(4,OUTPUT); //Left
  pinMode(5,OUTPUT); // Magnet

}

// Manually execute the coroutines.
void loop() {
    myRelay.runCoroutine();
    myLcd.runCoroutine();
}

Bonjour Julaye

Dans le code que vous m’avez envoyé, il y a une ligne qui ne marche pas:
c’est lcd.init();//In the setup the LCD is started .
Pourriez vous m’éclairer sur ce sujet.
Merci.

Bard

C’est quoi le code / message d’erreur renvoyé par l’éditeur quand vous lancez la compilation ?