wireless_temp_sensor_receiver.ino 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "queue.h"
  2. boolean disableRx = true;
  3. const int rxPinA = 7; // RX, Connected to pin 7
  4. const int enablePin = 4; // Enable, Connected to RTX pin 6: 0=Power down, 1=Active
  5. const int txEnablePin = 5; // rx/TX, Connected to RTX pin 5: 0=Rx, 1=Tx
  6. const int txPin = 6; // TX, Connected to RTX pin 4
  7. //********************************************************************
  8. // SETUP
  9. //********************************************************************
  10. void setup() {
  11. disableRx = true;
  12. pinMode(rxPinA, INPUT);
  13. pinMode(enablePin, OUTPUT);
  14. pinMode(txEnablePin, OUTPUT);
  15. pinMode(txPin, OUTPUT);
  16. digitalWrite(enablePin, LOW);
  17. digitalWrite(txEnablePin, LOW);
  18. digitalWrite(txPin, LOW);
  19. // ------------------ INIT THE TRANCEIVER -------------------------
  20. // From powerdown mode (pin 4-5-6 low),
  21. // 1. Drive high pin 6 (ENABLE)
  22. // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us
  23. // 3. Drive down 20us pin 6 (ENABLE).
  24. digitalWrite(enablePin, HIGH); delayMicroseconds( 20 );
  25. digitalWrite(txEnablePin, HIGH); delayMicroseconds( 200 ); digitalWrite(txEnablePin, LOW);
  26. delayMicroseconds( 40 );
  27. digitalWrite(enablePin, LOW); delayMicroseconds( 20 ); digitalWrite(enablePin, HIGH);
  28. delayMicroseconds( 200 );
  29. // ------------------ INIT THE TRANCEIVER -------------------------
  30. Serial.begin(9600); // USB Serial
  31. rcvInitQueue();
  32. cli(); // Clear interrupt
  33. setup_timer_interrupt();
  34. sei(); // Enable interrupt
  35. disableRx = false;
  36. }
  37. //********************************************************************
  38. // INTERRUPT FUNCTIONS
  39. //********************************************************************
  40. void setup_timer_interrupt()
  41. {
  42. //set timer1 interrupt at 10Hz
  43. TCCR1A = 0;// set entire TCCR1A register to 0
  44. TCCR1B = 0;// same for TCCR1B
  45. TCNT1 = 0;//initialize counter value to 0
  46. // set compare match register for 1hz increments
  47. // OCR1A = 40. This results is 16MHz / 8 / 40 = 20 uS between interrupts
  48. OCR1A = 40;
  49. // turn on CTC mode
  50. TCCR1B |= (1 << WGM12);
  51. // Set CS12:CS10 to 010 = /8 prescaler
  52. TCCR1B |= (1 << CS11);
  53. // enable timer compare interrupt
  54. TIMSK1 |= (1 << OCIE1A);
  55. }
  56. // 10uS timer interrupt function
  57. ISR(TIMER1_COMPA_vect,ISR_NOBLOCK) {
  58. static unsigned char currValue = 0;
  59. static unsigned short samples = 0;
  60. static unsigned short newSamples = 0;
  61. //return; // FOR DEBUG, Disables nexa receive
  62. // Sample the pin value
  63. unsigned char value = digitalRead(rxPinA);
  64. if( value == currValue ) {
  65. samples++;
  66. samples+=newSamples;
  67. newSamples=0;
  68. }
  69. else {
  70. newSamples++;
  71. }
  72. if( newSamples == 3 ) {
  73. uint16_t queSamples = (uint16_t)((samples*20 > 60000)? 60000 : samples*20);
  74. rcvEnQueue(queSamples); // Max 46000
  75. samples = newSamples;
  76. newSamples = 0;
  77. currValue = value;
  78. }
  79. }
  80. void stopInterrupts() {
  81. EIMSK &= 0xFC; // Clear bit 1:0 in Enable Interrupt register
  82. }
  83. //********************************************************************
  84. // LOOP
  85. //
  86. // Test-code: <NT1B5C1D83>
  87. //********************************************************************
  88. #define SER_RX_MODE_WAIT 0
  89. #define SER_RX_MODE_RCV 1
  90. #define SER_RX_MODE_END 2
  91. extern unsigned long x_data; // Receive data from Nexa
  92. extern unsigned long temp1_x_data; // Receive data from temp1
  93. void loop() {
  94. static int rxMode = SER_RX_MODE_WAIT;
  95. static int rxNum = 0;
  96. static unsigned long last_rx_time = 0;
  97. static unsigned char txDataReady = 0;
  98. static unsigned long previous_nexa_x_data = 0;
  99. static unsigned long nexa_old_time=0;
  100. static unsigned long previous_temp1_x_data = 0;
  101. static unsigned long temp1_old_time=0;
  102. //static boolean firstRun = true;
  103. static uint16_t width;
  104. static unsigned long txCode = 0;
  105. static unsigned long previous_now = 0; // Get the current time
  106. unsigned long now = millis(); // Get the current time
  107. // Check millis() for wrapping (after 49 days)
  108. if( previous_now > now ) {
  109. last_rx_time = 0; // Reset last rx time
  110. }
  111. // Display version information 10 sec after startup
  112. /* if( firstRun == true && now > 30000 ) {
  113. Serial.println("433MHz Repeater module. (Arduino nano) Ver: 170211 14:46"); // **** VERSION ****
  114. firstRun = false;
  115. } */
  116. if( rcvDeQueue(&width) ) {
  117. // ********** NEXA ***********
  118. if( nextPulseNexa(width) ) {
  119. last_rx_time = now; // Set last rx time to now
  120. if( x_data != previous_nexa_x_data || now > (nexa_old_time+1000) ) {
  121. unsigned long remote_id = (x_data & 0xFFFFFFC0) >>6;
  122. int group = (x_data & 0x00000020) >>5;
  123. int onOff = (x_data & 0x00000010) >>4;
  124. int channel = (x_data & 0x0000000C) >>2;
  125. int button = (x_data & 0x00000003);
  126. //Serial.println(remote_id);
  127. if( ( remote_id == 11067169UL || remote_id == 20071093UL ) && txDataReady == 0 ) {
  128. remote_id++; // Increase the remote id with one ( = the real id of the nexa remote control )
  129. txDataReady = 1;
  130. txCode = ((remote_id << 6) & 0xFFFFFFC0 );
  131. txCode |= ((group << 5) & 0x00000020 );
  132. txCode |= ((onOff << 4) & 0x00000010 );
  133. txCode |= ((channel << 2) & 0x0000000C );
  134. txCode |= ((button ) & 0x00000003 );
  135. }
  136. previous_nexa_x_data = x_data;
  137. }
  138. nexa_old_time = now;
  139. resetDecoder();
  140. }
  141. }
  142. if( txDataReady == 1 && (now > (last_rx_time+200)) ) { // Transmit if it has passed more than 100mS since last rx
  143. txDataReady = 0;
  144. sendNexaCode(txCode);
  145. }
  146. previous_now = now; // Store to check for wrapping of millis()
  147. }