-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevator_remote.ino
170 lines (161 loc) · 5.04 KB
/
elevator_remote.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
// elevator remote control
#include <SPI.h>
#include "RF24.h"
const int ACTIVITY_LED = D0;
//const int TXLED = D4; // actually also CE on radio
const int ELEV_IS_UP_LED = D1;
const int ELEV_IS_DOWN_LED = D2;
RF24 radio(D4, D8);
// these must be the same in the relay code
enum {TXADR=0, RXADR};
uint8_t addresses[][6] = { "Node0","Node1" };
unsigned long lastTimeButtonSent;
// the bits in the control byte, also the index to the button arrays
#define UP 0
#define DOWN 1
// button arrays
int btnPins[] = { 10, D3 }; // SD3/GPIO10 and SD2/GPI09
// these are bit arrays
byte btnValues = 0;
byte btnPrevious = 0;
byte btnChanged = 0;
// where the elevator is, bit 0 is up, bit 1 is down
byte elevatorPosition = 0;
// update the buttons, debounce, set changed entry and new entry
// return true if anything changed
bool btnCheck() {
bool retval = false;
for (int i = 0; i < (sizeof(btnPins) / sizeof(*btnPins)); ++i) {
bool newval = !digitalRead(btnPins[i]);
// debounce if changed
if (newval != bitRead(btnPrevious, i)) {
delay(50);
// read it again
newval = !digitalRead(btnPins[i]);
// if really changed, flag it
if (newval != bitRead(btnPrevious, i)) {
bitWrite(btnPrevious, i, newval);
bitWrite(btnValues, i, newval);
bitSet(btnChanged, i);
retval = true;
}
}
}
return retval;
}
// get the first changed index button, -1 for none
int btnChangedIndex() {
for (int i = 0; i < (sizeof(btnPins) / sizeof(*btnPins)); ++i) {
if (bitRead(btnChanged, i)) {
// reset the flag
bitClear(btnChanged, i);
return i;
}
}
return -1; // didn't find any
}
// send data
bool xmitData(byte* data, int bytes) {
radio.stopListening();
radio.openWritingPipe(addresses[TXADR]);
bool success = false;
success = radio.write(data, bytes);
radio.startListening();
return success;
}
// blink the light
void blinkLED(int times, int timeBetween, int timeAfter) {
while (times--) {
digitalWrite(ACTIVITY_LED, LOW);
delay(timeBetween);
digitalWrite(ACTIVITY_LED, HIGH);
if (times && timeAfter)
delay(timeAfter);
}
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("init...");
delay(10);
pinMode(ACTIVITY_LED, OUTPUT); // Initialize the LED_BUILTIN pin as an output
pinMode(ELEV_IS_DOWN_LED, OUTPUT);
digitalWrite(ELEV_IS_DOWN_LED, LOW);
pinMode(ELEV_IS_UP_LED, OUTPUT);
digitalWrite(ELEV_IS_UP_LED, LOW);
blinkLED(1, 500, 0);
for (int i = 0; i < (sizeof(btnPins) / sizeof(*btnPins)); ++i) {
pinMode(btnPins[i], INPUT_PULLUP);
Serial.print("button: ");
Serial.println(btnPins[i]);
}
Serial.println("running");
radio.begin();
// radio.setPALevel(RF24_PA_MAX);
radio.setRetries(15, 5);
radio.setChannel(100);
radio.setDataRate(RF24_1MBPS);
// radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, addresses[RXADR]);
radio.startListening();
lastTimeButtonSent = millis();
// digitalWrite(TXLED, HIGH);
digitalWrite(ELEV_IS_DOWN_LED, HIGH);
delay(500);
digitalWrite(ELEV_IS_DOWN_LED, LOW);
digitalWrite(ELEV_IS_UP_LED, HIGH);
delay(500);
digitalWrite(ELEV_IS_UP_LED, LOW);
delay(250);
#if 0
analogWriteRange(100);
for (int i = 0; i < 100; ++i) {
analogWrite(ELEV_IS_DOWN_LED, i);
delay(50);
}
#endif // 0
}
byte lastData = 0;
void loop() {
if (btnCheck()) {
// something changed, get the index
int btn = btnChangedIndex();
Serial.print(btn ? "DOWN " : "UP ");
Serial.println(bitRead(btnValues, btn) ? "PRESSED" : "RELEASED");
// force an xmit
lastTimeButtonSent = 0;
}
// set current data
byte data = btnValues;
// can't set both bits!
if (data >= 3)
data = 0;
// see if time to send and different from last time
if (millis() > lastTimeButtonSent + 2000) {
lastTimeButtonSent = millis();
// keep sending data if not 0 and last one wasn't zero
// I.E. no reason to keep sending 0's
// Serial.println("lastData: "+String(lastData));
if (data || data != lastData) {
Serial.println("sending: " + String(data));
if (xmitData(&data, sizeof(data))) {
blinkLED(1, 25, 0);
}
else {
Serial.println("tx failed");
blinkLED(5, 50, 25);
}
}
lastData = data;
}
// get and set the elevator status
// get from radio
while (radio.available()) {
Serial.println("got elevator position");
radio.read(&elevatorPosition, sizeof elevatorPosition);
digitalWrite(ELEV_IS_DOWN_LED, bitRead(elevatorPosition, DOWN) ? HIGH : LOW);
digitalWrite(ELEV_IS_UP_LED, bitRead(elevatorPosition, UP) ? HIGH : LOW);
}
// Serial.println("position:" + String(elevatorPosition));
delay(10);
}