#include #include "nRF24L01.h" #include "RF24.h" #define SWSIG() ((PINC&0x08)==0) #define SWONL() (SWcntr==200) #define SWON() (SWcntr==4) #define SENDER() ((PINC&0x10)==0) RF24 radio(8,10); // SPI ... ce:8, cs:10 const uint64_t pipes[2][2]={ 0x3A3A3A3AD2LL, 0xF0F0F0F0D2LL, 0x3A3A3A3AC3LL, 0xF0F0F0F0C3LL }; const uint8_t pp[]={0x39, 0x3a, 0x3c}; uint8_t LEDseg[] PROGMEM={ 0b11000000, 0b11111001, 0b10100100, 0b10110000, 0b10011001, // 0,1,2,3,4 0b10010010, 0b10000010, 0b11011000, 0b10000000, 0b10010000 // 5,6,7,8,9 }; bool ok=false; uint8_t S=0, digit[3], pd=0, SWcntr=255, blink=0, GRP; uint16_t T=0; uint32_t t; unsigned long m5, m100, m500; void setup(){ PORTD=0xff; DDRD=0xff; // pgfedcba PORTC=0x38; DDRC=0x07; // xxrrrooo GRP=((PINC&0x20)==0?0:1); delay(200); m5=m100=m500=millis(); radio.begin(); radio.setRetries(15,15); radio.setPayloadSize(sizeof(uint32_t)); if(SENDER()){ radio.openWritingPipe(pipes[GRP][0]); radio.openReadingPipe(1,pipes[GRP][1]); }else{ radio.openWritingPipe(pipes[GRP][1]); radio.openReadingPipe(1,pipes[GRP][0]); } radio.startListening(); } void tx(void){ radio.stopListening(); t=(T<<8)|S; ok=radio.write(&t, sizeof(uint32_t)); radio.startListening(); } void rx(void){ for(bool done=false;!done;){ done=radio.read(&t, sizeof(uint32_t)); if(done) ok=true; delay(20); } S=t&0xff; T=(t>>8); } void loop(){ if(millis()>=m500+500){ m500+=500; blink++; } if(millis()>=m5+5){ // 5ms–ˆ‚Ι if(S==0 && SWON()){ S=1; tx(); } // SWƒIƒ“‚Θ‚η else if(S==1){ S=2; m100=millis(); tone(9,440,200);}// ƒsƒb! else if(S==2){ // ƒ‰ƒ“ if(SWONL()){ S=5; tx(); } // ’·‰Ÿ‚΅(ƒŠƒZƒbƒg) else if(SWON()){S=3; tx(); } // ’Z‰Ÿ‚΅(ƒ‰ƒ“) }else if(S==3){ S=4; tone(9,440,200);} // ƒsƒb! else if(S==4||S==6){S=0; tx(); } else if(S==5){ S=6; T=0; tone(9,880,200);} // ƒ|ƒb if(!SWSIG()) SWcntr=0; else if(SWcntr<255) SWcntr++; PORTC=0xf8; PORTD=digit[pd]; PORTC=pp[pd]; // LED•\Ž¦ if(++pd==3) pd=0; // D0¨D1¨D2¨... m5+=5; digit[2]=(T/100==0?0xff:pgm_read_byte(&LEDseg[T/100])); // 10•b digit[1]=pgm_read_byte(&LEDseg[(T%100)/10])&((ok||blink&1)?0x7f:0xff); // 1•b(P) digit[0]=pgm_read_byte(&LEDseg[T%10]); // 0.1•b }else if(S==2 && millis()>=m100+100){ // ƒ‰ƒ“Žž‚Ν100ms–ˆ‚Ι if(T==998) S=0; // 999‚Ε’βŽ~ m100+=100; if(T<999) T++; // T‚πƒJƒEƒ“ƒgƒAƒbƒv }else if(radio.available()) rx(); }