Skip to content

Instantly share code, notes, and snippets.

@danjperron
Created November 19, 2018 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danjperron/b4b3bafd718382fd3a281793d77c938b to your computer and use it in GitHub Desktop.
Save danjperron/b4b3bafd718382fd3a281793d77c938b to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
/*
* remote control utilisant un nrf24L01
*
* Bouton 1 -> Non momentané N.C. => Stop ALL N.O.=> Tamis ON
* Bouton 2 -> Stop feeder primaire
* Bouton 3 -> Stop feeder cone
* Bouton 4 -> Start feeder primaire
* Bouton 5 -> Start feeder cone
*
* Relais 1 -> Tamis
*
* Remote Relais2 -> feeder cone
* Remote Relais3 -> feeder primarie
*
* -------------------
* Utilisation de fonction de debounce pour empêcher
* d'envoyer des fausses info lorsque la switch toggle
*
* Utilisation d'un poid binaire pour faciliter l'envoi de l'info
*
* bouton1 = 1
* bouton2 = 2
* bouton3 = 4
* bouton4 = 8
* bouton5 = 16
*
* currentBoutons => valeurs des bouttons actuelles
* LastBoutons => valeurs passées des bouttons
* debounceBoutons => temps capturé de millis() depuis un changement de boutton
*
* si pas de changement sur les bouttons depuis plus de DEBOUNCE_MIN then currentB = LastB
*/
#define DEBOUNCE_MIN 50
#define BOUTON1 2
#define BOUTON2 3
#define BOUTON3 4
#define BOUTON4 6
#define BOUTON5 7
#define BOUTON1_BIT 1
#define BOUTON2_BIT 2
#define BOUTON3_BIT 4
#define BOUTON4_BIT 8
#define BOUTON5_BIT 16
unsigned long tempMilli; // variable temporaire pour lecture millis()
unsigned char tempBoutons; // variable temporaire pour lecture boutons
unsigned char currentBoutons= 1; //par défaux bouton 1 pressé (tout à OFF).
unsigned char lastBoutons=currentBoutons;
unsigned long debounceBoutons = millis(); // debounce current time en milli sec;
unsigned long currentLapse= debounceBoutons; // timer pour envoyer le data toute les 0.1 sec.
// set radio pin 8 CE , pin 10 CS
RF24 radio(8, 10);
/* nrf24 pipe
*
*
* pipe [0] => current tranmitter
* pipe[1...] => addresse des remotes
*/
const uint64_t master=0xF0F0F0F0E1LL;
// definition des relais
// RELAY2 ET 3 sont sous remote
#define RELAY1 5
#define RELAY2 0xF0F0F0F0D2LL
#define RELAY3 0xF0F0F0F0D3LL
//definition des noms de relay versus index
#define TAMIS 0
#define CONE_FEEDER 1
#define PRIMARY_FEEDER 2
#define RELAY_MAX 3
bool relaisVirtuel[RELAY_MAX] = { false,false,false};
uint64_t relaisID[RELAY_MAX] = { RELAY1, RELAY2, RELAY3};
/* LireBoutons
* retourne valeur de chaque boutton dans une valeur binaire
*/
unsigned char lireBoutons(void)
{
unsigned char bValue=0;
if (digitalRead(BOUTON1) == LOW)
bValue = BOUTON1_BIT;
if (digitalRead(BOUTON2) == LOW)
bValue |= BOUTON2_BIT;
if (digitalRead(BOUTON3) == LOW)
bValue |= BOUTON3_BIT;
if (digitalRead(BOUTON4) == LOW)
bValue |= BOUTON4_BIT;
if (digitalRead(BOUTON5) == LOW)
bValue |= BOUTON5_BIT;
return bValue;
}
// decode les boutons
void decodeAction(unsigned char Boutons)
{
// si bouton 1 activé éteint tout
if (currentBoutons & BOUTON1_BIT)
{
relaisVirtuel[TAMIS]=false;
relaisVirtuel[CONE_FEEDER]=false;
relaisVirtuel[PRIMARY_FEEDER]=false;
}
else
{
//Sinon tamis est fonctionnel
relaisVirtuel[TAMIS]=true;
// si bouton 2 activer stop PRIMARY feeder
if(currentBoutons & BOUTON2_BIT)
relaisVirtuel[PRIMARY_FEEDER]= false;
// si bouton 3 activer stop cone feeder
if(currentBoutons & BOUTON3_BIT)
relaisVirtuel[CONE_FEEDER]= false;
// si bouton 4 activer star primary feeder
if(currentBoutons & BOUTON4_BIT)
relaisVirtuel[PRIMARY_FEEDER]= true;
// si bouton 5 activer star primary feeder
if(currentBoutons & BOUTON5_BIT)
relaisVirtuel[CONE_FEEDER]= true;
}
}
unsigned char datapacket[1];
// envoyer relais virtuel sur les vrais relais
bool setRelais(unsigned char number)
{
bool rflag;
rflag=false;
if(number < RELAY_MAX)
{
uint64_t t_ID = relaisID[number];
if(t_ID <100)
{
// ok vrai relais
digitalWrite(t_ID,relaisVirtuel[number]);
rflag=true;
}
else
{
datapacket[0]=relaisVirtuel[number];
radio.openWritingPipe(t_ID);
printf("radio =>%lX =>",t_ID);
radio.stopListening();
rflag=radio.write(datapacket,1);
radio.startListening();
}
}
return rflag;
}
// fonction pour faire tout les relais
void syncToutRelais(void)
{
bool flag;
unsigned char loop;
for(loop=0;loop<RELAY_MAX;loop++)
{
flag=setRelais(loop);
printf("setRelais(%d,%d) return %d\n",loop,relaisVirtuel[loop],flag);
}
}
void setup()
{
pinMode(BOUTON3, INPUT);
pinMode(BOUTON2, INPUT);
pinMode(BOUTON4, INPUT);
pinMode(BOUTON1, INPUT);
pinMode(BOUTON5, INPUT);
pinMode(RELAY1, OUTPUT);
digitalWrite(BOUTON1, HIGH);
digitalWrite(BOUTON2, HIGH);
digitalWrite(BOUTON3, HIGH);
digitalWrite(BOUTON4, HIGH);
digitalWrite(BOUTON5, HIGH);
printf_begin();
Serial.begin(57600);
Serial.println("NRF24L01 Transmitter");
radio.begin();
radio.setChannel(76); // par 76 par defaux
radio.setRetries(15,15);
radio.setAutoAck(1);
radio.enableDynamicPayloads();
radio.enableAckPayload();
radio.openReadingPipe(1, master);
radio.openWritingPipe(0xF0F0F0F0D2LL);
radio.startListening();
radio.printDetails();
}
void loop() {
unsigned long cTimer = millis();
// maintenant vérifions les boutons
unsigned char _tBoutons = lireBoutons();
// est-ce la même valeur
if(_tBoutons != lastBoutons)
{
// ok les boutons ne sont pas stable attendons
debounceBoutons = cTimer;
lastBoutons = _tBoutons;
}
else
{
// est-ce que les bouttons on changé depuis le debounce
if(_tBoutons != currentBoutons)
{
// est-ce que le débounce est fait
if((debounceBoutons - cTimer) > DEBOUNCE_MIN)
{
// ok debounce est fait donc
currentBoutons = _tBoutons;
currentLapse = cTimer;
// Decode Boutons Action
decodeAction(currentBoutons);
syncToutRelais();
currentLapse = cTimer;
}
}
}
// Vérifier si cela fait plus de 1 seconde
// que nous avons pas envoyer . Si oui envoyons l'info
if( (cTimer - currentLapse) > 1000)
{
// 1 seconde écoulée envoyons.
syncToutRelais();
printf(".");
currentLapse = cTimer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment