-
Notifications
You must be signed in to change notification settings - Fork 1
/
TX_nRF24_5ch_LED.ino
369 lines (307 loc) · 13.3 KB
/
TX_nRF24_5ch_LED.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//*********************************************************************************************************************
// RC transmitter for cars, boats, tanks and simple model airplanes
//******************************************************************
// Simple surface 5 channel RC transmitter from my repository https://github.com/stanekTM/TX_nRF24_5ch_LED
//
// The hardware includes nRF24L01+ transceiver and ATmega328P processor.
// Telemetry monitors receiver voltage using LED indication. The code is Arduino.
//
// This RC transmitter works with RC receiver from my repository https://github.com/stanekTM/RX_nRF24_Motor_Servo
//
// Thank you to "Phil_G" http://www.singlechannel.co.uk for the calibration and reverse routine I used in the code.
//*********************************************************************************************************************
#include <RF24.h> // https://github.com/nRF24/RF24
#include <SPI.h> // Arduino standard library
#include <EEPROM.h> // Arduino standard library
//setting a unique address (5 bytes number or character)
const byte address[] = "jirka";
//RF communication channel settings (0-125, 2.4Ghz + 76 = 2.476Ghz)
#define RADIO_CHANNEL 76
//TX battery voltage settings
#define TX_BATTERY_VOLTAGE 4.2
#define TX_MONITORED_VOLTAGE 3.3
//RX voltage monitoring settings
#define RX_BATTERY_VOLTAGE 4.2
#define RX_MONITORED_VOLTAGE 3.49
//setting the control range value
#define MIN_CONTROL_VAL 1000
#define MID_CONTROL_VAL 1500
#define MAX_CONTROL_VAL 2000
#define EPA_POSITIVE 500
#define EPA_NEGATIVE -500
//free pins
//pin 0
//pin 1
//pin 3
//pin 5
//pin 6
//pin 7
//pin 8
//pin A5
//pin A6
//pins for pots, joysticks
//pot1 A0
//pot2 A1
//pot3 A2
//pot4 A3
//pot5 A4
//LED battery and RF on/off
#define PIN_LED 2
//calibration button (I had to add a 10k resistor -> VCC even when the internal INPUT_PULLUP is activated)
#define PIN_BUTTON_CALIB 4
//input battery
#define PIN_BATTERY A7
//pins for nRF24L01
#define PIN_CE 9
#define PIN_CSN 10
//hardware SPI
//----- MOSI 11
//----- MISO 12
//----- SCK 13
//setting of CE and CSN pins
RF24 radio(PIN_CE, PIN_CSN);
//*********************************************************************************************************************
//this structure defines the sent data in bytes ***********************************************************************
//*********************************************************************************************************************
struct rc_packet_size
{
unsigned int ch1;
unsigned int ch2;
unsigned int ch3;
unsigned int ch4;
unsigned int ch5;
};
rc_packet_size rc_packet;
//*********************************************************************************************************************
//this struct defines data, which are embedded inside the ACK payload *************************************************
//*********************************************************************************************************************
struct telemetry_packet_size
{
byte rssi; //not used yet
float batt_A1;
float batt_A2; //not used yet
};
telemetry_packet_size telemetry_packet;
//*********************************************************************************************************************
//read pots, joysticks ************************************************************************************************
//*********************************************************************************************************************
int ch, raw_pots;
int pot_calib_min[] = {0, 0, 0, 0, 0};
int pot_calib_mid[] = {512, 512, 512, 512, 512};
int pot_calib_max[] = {1023, 1023, 1023, 1023, 1023};
int pots_value[] = {1500, 1500, 1500, 1500, 1500};
byte reverse[] = {0, 0, 0, 0, 0};
void read_pots()
{
for (ch = 0; ch < 5; ch++)
{
raw_pots = analogRead(ch);
if (raw_pots > pot_calib_mid[ch])
pots_value[ch] = map(raw_pots, pot_calib_mid[ch], pot_calib_min[ch], 0, EPA_POSITIVE);
else
pots_value[ch] = map(raw_pots, pot_calib_max[ch], pot_calib_mid[ch], EPA_NEGATIVE, 0);
}
// format the frame
for (ch = 0; ch < 5; ch++)
{
pots_value[ch] += MID_CONTROL_VAL;
pots_value[ch] = constrain(pots_value[ch], MIN_CONTROL_VAL, MAX_CONTROL_VAL);
if (reverse[ch] == 1) pots_value[ch] = 3000 - pots_value[ch];
}
rc_packet.ch1 = pots_value[0]; //A0
rc_packet.ch2 = pots_value[1]; //A1
rc_packet.ch3 = pots_value[2]; //A2
rc_packet.ch4 = pots_value[3]; //A3
rc_packet.ch5 = pots_value[4]; //A4
//Serial.println(rc_packet.ch1);
}
//*********************************************************************************************************************
//calibrate pots, joysticks *******************************************************************************************
//*********************************************************************************************************************
int calibrated = 1;
void calibrate_pots()
{
while (digitalRead(PIN_BUTTON_CALIB) == 0)
{
calibrated = 0;
for (int pot = 0; pot < 5; ++pot)
{
raw_pots = analogRead(pot);
if (raw_pots > pot_calib_min[pot]) pot_calib_min[pot] = raw_pots;
if (raw_pots < pot_calib_max[pot]) pot_calib_max[pot] = raw_pots;
pot_calib_mid[pot] = raw_pots; //save neutral pots, joysticks as button is released
}
} //calibrate button released
if (calibrated == 0)
{
for (ch = 0; ch < 5; ch++)
{
EEPROMWriteInt(ch * 6, pot_calib_max[ch]); //eeprom locations 0, 6, 12, 18 (decimal)
EEPROMWriteInt(ch * 6 + 2, pot_calib_mid[ch]); //eeprom locations 2, 8, 14, 20 (decimal)
EEPROMWriteInt(ch * 6 + 4, pot_calib_min[ch]); //eeprom locations 4, 10, 16, 22 (decimal)
}
calibrated = 1;
}
for (ch = 0; ch < 5; ch++)
{
pot_calib_max[ch] = EEPROMReadInt(ch * 6); //eeprom locations 0, 6, 12, 18 (decimal)
pot_calib_mid[ch] = EEPROMReadInt(ch * 6 + 2); //eeprom locations 2, 8, 14, 20 (decimal)
pot_calib_min[ch] = EEPROMReadInt(ch * 6 + 4); //eeprom locations 4, 10, 16, 22 (decimal)
reverse[ch] = EEPROM.read(ch + 30) & 1; //eeprom locations 30, 31, 32, 33 (decimal), ch * 6 = 30
}
//check for reversing, stick over on power-up
for (ch = 0; ch < 5; ch++)
{
pots_value[ch] = map(analogRead(ch), pot_calib_max[ch], pot_calib_min[ch], EPA_NEGATIVE, EPA_POSITIVE);
if (pots_value[ch] > EPA_POSITIVE - 50 || pots_value[ch] < EPA_NEGATIVE + 50)
{
reverse[ch] ^= B00000001;
EEPROM.write(30 + ch, reverse[ch]); //ch * 6 = 30
}
}
}
//*********************************************************************************************************************
//this function will write a 2 byte integer to the eeprom at the specified address and address + 1 ********************
//*********************************************************************************************************************
void EEPROMWriteInt(int p_address, int p_value)
{
byte lowByte = p_value % 256;
byte highByte = p_value / 256;
EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}
//*********************************************************************************************************************
//this function will read a 2 byte integer from the eeprom at the specified address and address + 1 *******************
//*********************************************************************************************************************
unsigned int EEPROMReadInt(int p_address)
{
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);
return lowByte + highByte * 256;
}
//*********************************************************************************************************************
//initial main settings ***********************************************************************************************
//*********************************************************************************************************************
const byte invert_address = ~address[5]; //invert bits for reading so that telemetry packets have a different address
void setup()
{
//Serial.begin(9600); //print value on a serial monitor
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_BATTERY, INPUT);
pinMode(PIN_BUTTON_CALIB, INPUT_PULLUP);
calibrate_pots();
//define the radio communication
radio.begin();
radio.setAutoAck(true);
radio.enableAckPayload();
radio.enableDynamicPayloads();
radio.setRetries(5, 5);
radio.setChannel(RADIO_CHANNEL);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MIN); //RF24_PA_MIN (-18dBm), RF24_PA_LOW (-12dBm), RF24_PA_HIGH (-6dbm), RF24_PA_MAX (0dBm)
radio.stopListening();
radio.openWritingPipe(address);
radio.openReadingPipe(1, invert_address);
}
//*********************************************************************************************************************
//program loop ********************************************************************************************************
//*********************************************************************************************************************
void loop()
{
read_pots();
receive_time();
send_and_receive_data();
TX_batt_check();
}
//*********************************************************************************************************************
//after losing RF data or turning off the RX, gain time and the LED flashing ******************************************
//*********************************************************************************************************************
unsigned long rx_time = 0;
void receive_time()
{
if (millis() - rx_time > 1000) //after 1 second
{
RF_off_check();
}
}
//*********************************************************************************************************************
//send and receive data ***********************************************************************************************
//*********************************************************************************************************************
void send_and_receive_data()
{
if (radio.write(&rc_packet, sizeof(rc_packet_size)))
{
if (radio.isAckPayloadAvailable())
{
radio.read(&telemetry_packet, sizeof(telemetry_packet_size));
RX_batt_check();
rx_time = millis();
}
}
/*
if (radio.write(&rc_packet, sizeof(rc_packet_size)))
{
if (radio.available())
{
radio.read(&telemetry_packet, sizeof(telemetry_packet_size));
RX_batt_check();
rx_time = millis();
}
}
*/
}
//*********************************************************************************************************************
//input measurement TX_BATTERY_VOLTAGE < TX_MONITORED_VOLTAGE = LED flash at a interval of 0.2s ***********************
//Battery OK = LED is lit *********************************************************************************************
//*********************************************************************************************************************
unsigned long led_time = 0;
bool tx_low_batt = 0, previous_state_batt, led_state;
void TX_batt_check()
{
tx_low_batt = analogRead(PIN_BATTERY) <= (1023 / TX_BATTERY_VOLTAGE) * TX_MONITORED_VOLTAGE;
digitalWrite(PIN_LED, led_state);
if (tx_low_batt)
{
previous_state_batt = 1;
if (millis() - led_time > 200)
{
led_time = millis();
led_state = !led_state;
}
}
tx_low_batt = previous_state_batt;
//Serial.println(tx_low_batt);
}
//*********************************************************************************************************************
//after receiving RF data, the monitored RX battery is activated ******************************************************
//RX battery voltage(telemetry_packet.batt_A1) < RX_MONITORED_VOLTAGE = LEDs TX, RX flash at a interval of 0.5s *******
//Battery OK = LEDs TX, RX is lit *************************************************************************************
//*********************************************************************************************************************
bool rx_low_batt = 0;
void RX_batt_check()
{
rx_low_batt = telemetry_packet.batt_A1 <= (255 / RX_BATTERY_VOLTAGE) * RX_MONITORED_VOLTAGE;
digitalWrite(PIN_LED, led_state);
if (rx_low_batt)
{
if (millis() - led_time > 500)
{
led_time = millis();
led_state = !led_state;
}
}
//Serial.println(telemetry_packet.batt_A1);
}
//*********************************************************************************************************************
//when TX is switched on and RX is switched off, or after the loss of RF data = LED TX flash at a interval of 0.1s ****
//Normal mode = LED TX is lit *****************************************************************************************
//*********************************************************************************************************************
void RF_off_check()
{
digitalWrite(PIN_LED, led_state);
if (millis() - led_time > 100)
{
led_time = millis();
led_state = !led_state;
}
}