add preview and RGB
parent
e39aac0769
commit
24a6568d66
@ -0,0 +1,83 @@
|
||||
#include <RF24.h>
|
||||
#include <nRF24L01.h>
|
||||
|
||||
//tally RGB LED
|
||||
const int tallyLED[] = {3,5,6}; //RGB
|
||||
const int tallyLEDlength = 3;
|
||||
boolean tallyLEDstat[3];
|
||||
|
||||
# define RGBground 4
|
||||
# define channel 1
|
||||
|
||||
//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(RGBground, OUTPUT);
|
||||
digitalWrite(RGBground, 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 ((datareceive >> 15 - channel) & 1 == 1) setRGB(100,0,0);
|
||||
else if ((datareceive >> 7 - channel) & 1 == 1) setRGB(0,30,0);
|
||||
else setRGB(0,0,0);
|
||||
}
|
||||
|
||||
boolean updateLoop(){
|
||||
currentTime = millis();
|
||||
if (radio.available()){
|
||||
Serial.println("new package");
|
||||
previousTime = currentTime;
|
||||
return true;
|
||||
}
|
||||
else if (currentTime - previousTime >= errorTime){
|
||||
Serial.println("timeout");
|
||||
setRGB(0,0,50);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
byte getChecksum(byte data){
|
||||
byte checksum = 0;
|
||||
for (int i=8; i<8; i++) checksum += (data >> i) & 1;
|
||||
return checksum;
|
||||
}
|
||||
|
||||
void setRGB(int r, int g, int b){
|
||||
analogWrite(tallyLED[0], r);
|
||||
analogWrite(tallyLED[1], g);
|
||||
analogWrite(tallyLED[2], b);
|
||||
}
|
Loading…
Reference in New Issue