You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.8 KiB
85 lines
1.8 KiB
#include <RF24.h>
|
|
#include <nRF24L01.h>
|
|
|
|
//tally LEDs
|
|
const int tallyLED[] = {2,4};
|
|
const int tallyLEDlength = 2;
|
|
boolean tallyLEDstat[2];
|
|
|
|
# define errorLED 6
|
|
|
|
//init NRF
|
|
RF24 radio (7, 8);
|
|
const uint16_t address = 0xADB5;
|
|
uint16_t datareceive;
|
|
|
|
//define min interval to show error
|
|
unsigned long currentTime = millis();
|
|
unsigned long previousTime = 0;
|
|
const long errorTime = 3000;
|
|
|
|
void setup(){
|
|
for (int i=0; i < tallyLEDlength; i++){
|
|
pinMode(tallyLED[i], OUTPUT);
|
|
digitalWrite(tallyLED[i], LOW);
|
|
}
|
|
pinMode(errorLED, OUTPUT);
|
|
digitalWrite(errorLED, HIGH);
|
|
|
|
pinMode(3, OUTPUT);
|
|
digitalWrite(3, LOW);
|
|
|
|
pinMode(5, OUTPUT);
|
|
digitalWrite(5, LOW);
|
|
|
|
//start Serial
|
|
Serial.begin(9600);
|
|
Serial.println("start serial");
|
|
|
|
//start NRF
|
|
Serial.println("start NRF");
|
|
radio.begin();
|
|
radio.openReadingPipe(0, address);
|
|
radio.setPALevel(RF24_PA_HIGH);
|
|
radio.setDataRate(RF24_1MBPS);
|
|
radio.setAutoAck(false);
|
|
radio.disableCRC();
|
|
radio.startListening();
|
|
|
|
delay(20);
|
|
}
|
|
|
|
void loop(){
|
|
while (!updateLoop());
|
|
radio.read(&datareceive, sizeof(datareceive));
|
|
Serial.print("data: ");
|
|
Serial.println(datareceive, BIN);
|
|
if (getChecksum((datareceive >> 8) & 0xFF) != (datareceive & 0xFF)){
|
|
Serial.println("corrupted data");
|
|
digitalWrite(errorLED, HIGH);
|
|
}else digitalWrite(errorLED, LOW);
|
|
for (int i=0; i < tallyLEDlength; i++){
|
|
digitalWrite(tallyLED[i], (datareceive >> 15 - i) & 1);
|
|
}
|
|
}
|
|
|
|
boolean updateLoop(){
|
|
currentTime = millis();
|
|
if (radio.available()){
|
|
Serial.println("new package");
|
|
previousTime = currentTime;
|
|
return true;
|
|
}
|
|
else if (currentTime - previousTime >= errorTime){
|
|
Serial.println("timeout");
|
|
digitalWrite(errorLED, HIGH);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
byte getChecksum(byte data){
|
|
byte checksum = 0;
|
|
for (int i=8; i<8; i++) checksum += (data >> i) & 1;
|
|
return checksum;
|
|
}
|