Le wagon :
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <avr/wdt.h>
/*
* Arduino Wireless Communication Tutorial
* Example 1 - Transmitter Code
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
//--------------------------------------------------------------------------------------------------
// v0 | 1st version with send of default value in increment of 1000 to check "portée"
// v2 | 2nd version + bugs correction
// v3 | Add TX power selection and Baud Rate
// | Cannaux WiFi utilisable en France: canal 1 à 13 (F=2.412 à 2.472GHz, par pas de 5MHz)
//--------------------------------------------------------------------------------------------------
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
// PA
#define SENSOR_IN 2 // capteur roue
#define RADIO_TX 4 // LED Radio Tx
#define CFG_PW0 14
#define CFG_PW1 15
#define CFG_BR 16
#define CFG_CH 17
int timeCnt, time0, timet;
int speed;
int speedTab[8];
long speedAvg;
unsigned char tabPt;
float diam = 11.6; // diametre roue (mm): mesurée pa LJ 11.6mm
float Pi = 3.1416;
float scale = 87;
//float K = (diam * Pi * scale * 3600 * 1000)/(1000 * 1000); // to convert speed in mm/s (HO) to km/h reel
float K = (diam * Pi * scale * 3.6); // to convert speed in mm/s (HO) to km/h reel
float fspeed;
int NbPulse;
unsigned char sensorLevel, newSensorLevel;
void setup() {
//wdt_disable();
pinMode (CFG_PW0,INPUT_PULLUP); // Config
pinMode (CFG_PW1,INPUT_PULLUP); // Config
pinMode (CFG_BR,INPUT_PULLUP); // Config
pinMode (CFG_CH,INPUT_PULLUP); // Config
char TxPower = 0;
if(digitalRead(CFG_PW0) == 0) TxPower += 1;
if(digitalRead(CFG_PW1) == 0) TxPower += 2;
radio.begin(); // Begin operation of the chip. Call this in setup(), before calling any other methods.
radio.openWritingPipe(address); // Open a pipe for writing via byte array
//radio.setPALevel(RF24_PA_MAX); // Set Power Amplifier (PA) level to one of four levels: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
radio.setPALevel(TxPower); // Set Power Amplifier (PA) level to one of four levels: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
if(digitalRead(CFG_BR) == 0) radio.setDataRate(RF24_250KBPS); // (by default: 1MBds) Fast enough.. Better range
if(digitalRead(CFG_CH) == 0) radio.setChannel(108); // (by default: 2.476GHz), F = 2508MHz, = channel Above most Wifi Channels
radio.stopListening();
pinMode (SENSOR_IN,INPUT_PULLUP); // Capteur roue
sensorLevel = digitalRead(SENSOR_IN);
pinMode (RADIO_TX,OUTPUT); // Measure Tx Time
digitalWrite(RADIO_TX, 0);
time0 = millis();
speed = 0;
for(tabPt = 0; tabPt < 8; tabPt++)
{
speedTab[tabPt] = 0;
}
tabPt = 0;
NbPulse = 0;
//wdt_reset();
//wdt_enable(WDTO_8S);
}
void loop() {
newSensorLevel = digitalRead(SENSOR_IN);
timet = millis();
timeCnt = timet - time0;
if((sensorLevel == 0) && (newSensorLevel == 1))
{
sensorLevel = 1;
if(NbPulse == 0)
{
time0 = timet;
timeCnt = 0;
}
NbPulse++;
if(timeCnt > 1000)
{
NbPulse--; // v1.1
fspeed = (K * NbPulse) / timeCnt;
speed = (int)fspeed;
// averaging:
speedTab[tabPt++] = speed;
if(tabPt > 7) tabPt = 0;
speedAvg = 0;
for(unsigned char idx = 0; idx < 8; idx++)
{
speedAvg += speedTab[idx];
}
speedAvg = ((speedAvg << 16) & 0xFFFF0000) + speed; // divide by 8 equal >> 3
digitalWrite(RADIO_TX, 1);
radio.write(&speedAvg, sizeof(speedAvg)); // Write for single NOACK writes. Optionally disables acknowledgements/autoretries for a single write.
digitalWrite(RADIO_TX, 0);
speed = 0;
NbPulse = 0;
}
}
else
{
sensorLevel = newSensorLevel;
if(timeCnt > 2000)
{
// averaging:
speedTab[tabPt++] = 0;
if(tabPt > 7) tabPt = 0;
speedAvg = 0;
for(unsigned char idx = 0; idx < 8; idx++)
{
speedAvg += speedTab[idx];
}
speedAvg = ((speedAvg << 16) & 0xFFFF0000) + speed; // divide by 8 equal >> 3
digitalWrite(RADIO_TX, 1);
radio.write(&speedAvg, sizeof(speedAvg)); // Write for single NOACK writes. Optionally disables acknowledgements/autoretries for a single write.
digitalWrite(RADIO_TX, 0);
time0 = timet;
if((speed > 0) && (speed < 1000)); // if calculated speed: do nothing
else // no speed
{
speed += 1000;
if (speed >= 10000) speed = 0;
}
}
}
delay(5); // PA v1.1
}
L'afficheur :
/*
* Arduino Wireless Communication Tutorial
* Example 1 - Receiver Code
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
#define CFG_PW0 14
#define CFG_PW1 15
#define CFG_BR 16
#define CFG_CH 17
int speed;
long speedAvg;
long SpeedAvg_Table[4];
float speedMoy;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
pinMode (CFG_PW0,INPUT_PULLUP); // Config
pinMode (CFG_PW1,INPUT_PULLUP); // Config
pinMode (CFG_BR,INPUT_PULLUP); // Config
pinMode (CFG_CH,INPUT_PULLUP); // Config
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
//char TxPower = 0;
//if(digitalRead(CFG_PW0) == 0) TxPower += 1;
//if(digitalRead(CFG_PW1) == 0) TxPower += 2;
radio.setPALevel(RF24_PA_MIN);
if(digitalRead(CFG_BR) == 0) radio.setDataRate(RF24_250KBPS); // (by default: 1MBds) Fast enough.. Better range
if(digitalRead(CFG_CH) == 0) radio.setChannel(108); // (by default: 2.476GHz), F = 2508MHz, = channel Above most Wifi Channels
radio.startListening();
lcd.init(); // initialize the lcd
lcd.backlight();
//lcd.print("Hello, world!");
lcd.print(" RX_RF24 vers.02a ");
lcd.setCursor(0,1);
lcd.print(" 03 / 09 / 2018 ");
char TxPower = 0;
if(digitalRead(CFG_PW0) == 0) TxPower += 1;
if(digitalRead(CFG_PW1) == 0) TxPower += 2;
lcd.setCursor(0,2);
lcd.print("Tx Pw:");
lcd.print(TxPower, DEC);
if(TxPower == 0) lcd.print(" MIN PA ");
else if(TxPower == 1) lcd.print(" LOW PA ");
else if(TxPower == 2) lcd.print(" HIGH PA ");
else if(TxPower == 3) lcd.print(" MAX PA ");
else lcd.print(" ERROR ");
lcd.setCursor(0,3);
if(digitalRead(CFG_BR) == 0) lcd.print("BR=250kBp ");
else lcd.print("BR=1MBp ");
if(digitalRead(CFG_CH) == 0) lcd.print("F=2.508GHz ");
else lcd.print("F=2.476GHz ");
delay(2000);
speed = 0;
}
void loop() {
if (radio.available())
{
radio.read(&speedAvg, sizeof(speedAvg));
Serial.print(speedAvg & 0x0000FFFF, DEC);
Serial.print(" Avg: ");
speedMoy = speedAvg >> 16;
speedMoy = speedMoy / 8;
Serial.println(speedMoy);
for (int i = 0; i < 3; i++)
{
SpeedAvg_Table[i] = SpeedAvg_Table[i+1];
}
SpeedAvg_Table[3] = speedAvg;
lcd.clear();
lcd.home();
for (int i = 0; i < 4; i++)
{
lcd.setCursor(0,i);
lcd.print(SpeedAvg_Table[i] & 0x0000FFFF, DEC);
lcd.setCursor(6,i);
lcd.print(" Avg: ");
speedMoy = SpeedAvg_Table[i] >> 16;
speedMoy = speedMoy / 8;
lcd.print(speedMoy);
}
}
}