-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
101 lines (81 loc) · 1.81 KB
/
main.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
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char txNum = '0';
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 20; // send once per second
uint8_t buttonState = 0;
uint8_t lastButtonState = 0;
uint8_t buttonPin = 7;
bool retry = 0;
struct dataStruct
{
uint8_t buttonState;
char data[10] = "Message 0";
} myData;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.setRetries(3, 5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop()
{
currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (currentMillis - prevMillis >= txIntervalMillis)
{
if (buttonState != lastButtonState)
{
lastButtonState = buttonState;
send();
}
prevMillis = millis();
}
}
//====================
void send()
{
myData.buttonState = buttonState;
bool rslt = 0;
while (!rslt)
{
rslt = radio.write(&myData, sizeof(myData));
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(myData.data);
if (rslt)
{
Serial.println(" Acknowledge received");
updateMessage();
}
else
{
Serial.println(" Tx failed.. retrying.");
delay(100);
}
}
}
//================
void updateMessage()
{
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9')
{
txNum = '0';
}
myData.data[8] = txNum;
}