#include #include #include "nRF24L01.h" #include "RF24.h" //definition tunnel communication #define tunnel1 "PIPE1" #define tunnel2 "PIPE2" #define tunnel3 "PIPE3" #define tunnel4 "PIPE4" #define tunnel5 "PIPE5" #define tunnel6 "PIPE6" DFRobot_MCP23017 mcp(Wire, /*addr =*/0x27);//constructor, change the Level of A2, A1, A0 via DIP switch to revise the I2C address within 0x20~0x27. #define Led0 mcp.eGPB0 #define Led1 mcp.eGPB1 #define Bp0 mcp.eGPA0 #define Bp1 mcp.eGPA1 int MemLed0 =0; int MemBp0 =0; int tempo0 =50; int MemLed1 =0; int MemBp1 =0; int tempo1 =50; int compteur = 0; RF24 radio(4, 5); const byte adresses[][6] = {tunnel1, tunnel2, tunnel3, tunnel4, tunnel5, tunnel6}; const int taille = 32; char message[taille + 1]; char messageACK[taille +1]; void DebugVar(int nb, int valBP0) { Serial.print("temps = ");Serial.println(nb); Serial.print("Bp0 =");Serial.println(digitalRead(mcp.eGPA0)); Serial.print("ValeurBp0 =");Serial.println(digitalRead(valBP0)); Serial.print("MemBp0 =");Serial.println(MemBp0); Serial.print("MemLed0 =");Serial.println(MemLed0); delay(250); } int LectBp0() { // Bouton O -- Led0 int valeurBp0 = mcp.digitalRead(mcp.eGPA0); //Temps 0 if(valeurBp0== 0 && MemBp0 == 0 && MemLed0 == 0){ mcp.digitalWrite(mcp.eGPB0, LOW); MemBp0 = 0; MemLed0 =0; //delay(tempo0);DebugVar(0,valeurBp0); } //Temps 1 if (valeurBp0 == 1 && MemBp0 ==0 && MemLed0 == 0) { mcp.digitalWrite(mcp.eGPB0, HIGH);MemBp0 =1;MemLed0 = 1; //delay(tempo0);DebugVar(1,valeurBp0); } //Temps 2 if (valeurBp0 == 0 && MemBp0 ==1 && MemLed0 == 1) { mcp.digitalWrite(mcp.eGPB0, HIGH);MemBp0 =0;MemLed0 = 1; //delay(tempo0);DebugVar(2,valeurBp0); } //Temps 3 if (valeurBp0 == 1 && MemBp0 ==0 && MemLed0 == 1) { mcp.digitalWrite(mcp.eGPB0, LOW);MemBp0 =1;MemLed0 = 0; //delay(tempo0);DebugVar(3,valeurBp0); } //Temps 4 if (valeurBp0 == 0 && MemBp0 ==1 && MemLed0 == 0) { mcp.digitalWrite(mcp.eGPB0, LOW);MemBp0 =0;MemLed0 = 0; //delay(tempo0);DebugVar(4,valeurBp0); } delay(20); return(!valeurBp0); } int LectBp1(){ // Bouton 1 -- Led1 uint8_t valeurBp1 = mcp.digitalRead(mcp.eGPA1); //Temps 0 if(valeurBp1== 0 && MemBp1 == 0 && MemLed1 == 0){ mcp.digitalWrite(mcp.eGPB1, LOW); MemBp1 = 0; MemLed1 =0; //delay(tempo0);DebugVar(0,valeurBp0); } //Temps 1 if (valeurBp1 == 1 && MemBp1 ==0 && MemLed1 == 0) { mcp.digitalWrite(mcp.eGPB1, HIGH);MemBp1 =1;MemLed1 = 1; //delay(tempo0);DebugVar(1,valeurBp0); } //Temps 2 if (valeurBp1 == 0 && MemBp1 ==1 && MemLed1 == 1) { mcp.digitalWrite(mcp.eGPB1, HIGH);MemBp1 =0;MemLed1 = 1; //delay(tempo0);DebugVar(2,valeurBp0); } //Temps 3 if (valeurBp1 == 1 && MemBp1 ==0 && MemLed1 == 1) { mcp.digitalWrite(mcp.eGPB1, LOW);MemBp1 =1;MemLed1 = 0; //delay(tempo0);DebugVar(3,valeurBp0); } //Temps 4 if (valeurBp1 == 0 && MemBp1 ==1 && MemLed1 == 0) { mcp.digitalWrite(mcp.eGPB1, LOW);MemBp1 =0;MemLed1 = 0; //delay(tempo0);DebugVar(4,valeurBp0); } return(valeurBp1); } void setup() { Serial.begin(115200); Serial.println("Emetteur de donnees"); radio.begin(); radio.setChannel(125); radio.setPALevel(RF24_PA_MIN); radio.setDataRate(RF24_250KBPS); radio.openWritingPipe(adresses[0]); //pour recevoir radio.openReadingPipe(1, adresses[1]);//ouverture tunnel2 en LECTURE (reception Radio) radio.openReadingPipe(2, adresses[2]);//ouverture tunnel3 en LECTURE (reception Radio) radio.openReadingPipe(3, adresses[3]);//ouverture tunnel4 en LECTURE (reception Radio) radio.openReadingPipe(4, adresses[4]);//ouverture tunnel5 en LECTURE (reception Radio) radio.openReadingPipe(5, adresses[5]);//ouverture tunnel6 en LECTURE (reception Radio) while(mcp.begin() != 0){ Serial.println("Initialization of the chip failed, please confirm that the chip connection is correct!"); delay(1000); } mcp.pinMode(Bp0, INPUT); mcp.pinMode(Bp1, INPUT); mcp.pinMode(mcp.eGPB0,OUTPUT); mcp.pinMode(Led0,OUTPUT); mcp.digitalWrite(mcp.eGPB0, LOW); mcp.digitalWrite(Led1, LOW); } void loop() { //Lecture Boutons poussoir LectBp0(); //LectBp1(); //Serial.println(LectBp0()); compteur = LectBp0(); //Serial.println(compteur); //delay(200); // envoie message if (compteur == 1 ){ radio.stopListening(); itoa(compteur, message, 10); Serial.print("J'envoie maintenant "); // pour débogage Serial.println(message);//Serial.print(" ");Serial.println(LectBp0()); radio.write( message, taille ); // émission du message via nRF24L01 delay(200); compteur = 0; } radio.startListening(); if(radio.available()){ while ( radio.available() ) { radio.read( messageACK, taille ); Serial.print("MessageACK recu : "); Serial.println(messageACK); } delay(20); } delay(5); }