#include "queue.h" boolean disableRx = true; const int rxPinA = 7; // RX, Connected to pin 7 const int enablePin = 4; // Enable, Connected to RTX pin 6: 0=Power down, 1=Active const int txEnablePin = 5; // rx/TX, Connected to RTX pin 5: 0=Rx, 1=Tx const int txPin = 6; // TX, Connected to RTX pin 4 //******************************************************************** // SETUP //******************************************************************** void setup() { disableRx = true; pinMode(rxPinA, INPUT); pinMode(enablePin, OUTPUT); pinMode(txEnablePin, OUTPUT); pinMode(txPin, OUTPUT); digitalWrite(enablePin, LOW); digitalWrite(txEnablePin, LOW); digitalWrite(txPin, LOW); // ------------------ INIT THE TRANCEIVER ------------------------- // From powerdown mode (pin 4-5-6 low), // 1. Drive high pin 6 (ENABLE) // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us // 3. Drive down 20us pin 6 (ENABLE). digitalWrite(enablePin, HIGH); delayMicroseconds( 20 ); digitalWrite(txEnablePin, HIGH); delayMicroseconds( 200 ); digitalWrite(txEnablePin, LOW); delayMicroseconds( 40 ); digitalWrite(enablePin, LOW); delayMicroseconds( 20 ); digitalWrite(enablePin, HIGH); delayMicroseconds( 200 ); // ------------------ INIT THE TRANCEIVER ------------------------- Serial.begin(9600); // USB Serial initLuxSensor(); // Init the lux sensor on i2c rcvInitQueue(); cli(); // Clear interrupt setup_timer_interrupt(); sei(); // Enable interrupt disableRx = false; } //******************************************************************** // INTERRUPT FUNCTIONS //******************************************************************** void rxInterrupt() { static unsigned char currValue = 0; static unsigned short samples = 0; static unsigned short newSamples = 0; //return; // FOR DEBUG, Disables nexa receive // Sample the pin value unsigned char value = digitalRead(rxPinA); if( value == currValue ) { samples++; samples+=newSamples; newSamples=0; } else { newSamples++; } if( newSamples == 3 ) { rcvEnQueue(samples * 20); samples = newSamples; newSamples = 0; currValue = value; } } void stopInterrupts() { EIMSK &= 0xFC; // Clear bit 1:0 in Enable Interrupt register } //******************************************************************** // LOOP // // Test-code: //******************************************************************** #define SER_RX_MODE_WAIT 0 #define SER_RX_MODE_RCV 1 #define SER_RX_MODE_END 2 extern unsigned long x_data; // Receive data from Nexa extern unsigned long temp1_x_data; // Receive data from temp1 void loopB() { uint32_t lux = readLuxSensor(); Serial.println(lux); delay(1000); } void loop() { static int rxMode = SER_RX_MODE_WAIT; static int rxNum = 0; //static unsigned char rxChars[20]; static unsigned long last_rx_time = 0; static unsigned char txDataReady = 0; static unsigned long previous_nexa_x_data = 0; static unsigned long nexa_old_time=0; static unsigned long previous_temp1_x_data = 0; static unsigned long temp1_old_time=0; //static char hex[100]; static boolean firstRun = true; static unsigned int width; static unsigned long txCode = 0; static unsigned long previous_lux_time = 20000; // Start 1st measure after 20 sec static unsigned long previous_now = 0; // Get the current time unsigned long now = millis(); // Get the current time // Check millis() for wrapping (after 49 days) if( previous_now > now ) { last_rx_time = 0; // Reset last rx time previous_lux_time = 0; } // Display version information 10 sec after startup if( firstRun == true && now > 10000 ) { Serial.println("433MHz Repeater module. (Arduino nano) Ver: 170211 14:46"); // **** VERSION **** firstRun = false; } if( now > previous_lux_time ) { // Yes ! It's time to measure lux value int32_t lux = readLuxSensor(); if( lux > 0xFFFF ) lux=0xFFFF; if( lux < 0 ) lux=0; //Serial.println(lux); // Set next measure to 3 minutes +/- 20 sec previous_lux_time = now + 170000 + (random(20)*1000); txDataReady = 1; txCode = 0x6AD50000; txCode |= (lux & 0x0000FFFF); blinkTheLED(); } if( rcvDeQueue(&width) ) { // ********** NEXA *********** if( nextPulseNexa(width) ) { last_rx_time = now; // Set last rx time to now if( x_data != previous_nexa_x_data || now > (nexa_old_time+1000) ) { unsigned long remote_id = (x_data & 0xFFFFFFC0) >>6; int group = (x_data & 0x00000020) >>5; int onOff = (x_data & 0x00000010) >>4; int channel = (x_data & 0x0000000C) >>2; int button = (x_data & 0x00000003); Serial.println("Nexa"); if( remote_id == 11067169UL && txDataReady == 0 ) { remote_id++; txDataReady = 1; txCode = ((remote_id << 6) & 0xFFFFFFC0 ); txCode |= ((group << 5) & 0x00000020 ); txCode |= ((onOff << 4) & 0x00000010 ); txCode |= ((channel << 2) & 0x0000000C ); txCode |= ((button ) & 0x00000003 ); //Serial.println(remote_id); //Serial.println(onOff); } previous_nexa_x_data = x_data; } nexa_old_time = now; resetDecoder(); blinkTheLED(); } } if( txDataReady == 1 && (now > (last_rx_time+200)) ) { // Transmit if it has passed more than 100mS since last rx txDataReady = 0; sendNexaCode(txCode); } previous_now = now; // Store to check for wrapping of millis() }