-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarControl_nRF24_Tx.ino
70 lines (56 loc) · 1.48 KB
/
CarControl_nRF24_Tx.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
#include "SPI.h"
#include "NRFLite.h"
// Joystick
#define PIN_VX A2
#define PIN_VY A1
#define SW_PIN A0
#define ADC_MAX 1023
// nRF24L01
const static uint8_t RADIO_ID = 1; // Our radio's id.
const static uint8_t DESTINATION_RADIO_ID = 0; // Id of the radio we will transmit to.
const static uint8_t PIN_RADIO_CE = 9;
const static uint8_t PIN_RADIO_CSN = 10;
struct RadioPacket // Any packet up to 32 bytes can be sent.
{
int16_t vx;
int16_t vy;
};
NRFLite _radio;
RadioPacket _radioData;
int offsetX = 0;
int offsetY = 0;
int nbSamples = 100;
void calib() {
for (int i=0; i<nbSamples; i++) {
offsetX += analogRead(PIN_VX) - ADC_MAX/2;
offsetY += analogRead(PIN_VY) - ADC_MAX/2;
}
offsetX = offsetX / nbSamples;
offsetY = offsetY / nbSamples;
}
void setup()
{
Serial.begin(115200);
while (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN))
{
Serial.println("Cannot communicate with radio");
delay (1000); // Wait here forever.
}
Serial.println("Connected...");
// Joystick
pinMode(PIN_VX, INPUT);
pinMode(PIN_VY, INPUT);
// calib
calib();
}
void loop()
{
int16_t vx = analogRead(PIN_VX) - offsetX - ADC_MAX/2;
int16_t vy = analogRead(PIN_VY) - offsetY - ADC_MAX/2;
Serial.println("vx: " + String(vx));
Serial.println("vy: " + String(vy));
_radioData.vx = vx;
_radioData.vy = vy;
if (_radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData))) Serial.println("Message sent");
delay(1000);
}