clean serial output
[Arduino] / outdoor_temperature_sensor / outdoor_temperature_sensor.ino
index 841e079..5a18f26 100644 (file)
@@ -6,7 +6,8 @@
 //   |_________|  |______|  |___|  |
 //
 //   |  Sync      |    1    |  0   |
-//   |  8320us    | 4500us  | 2530us
+//   |  9780us    | 4420us  | 2410us
+//      9810        4410      2490
 
 // Defines
 #define DataBits0 4                                       // Number of data0 bits to expect
 #define F_CARRY_BIT 3                                     // Bit used to carry over bit shift from one long to the other
 #define F_STATE 7                                         // 0=Sync mode, 1=Data mode
 
+#define DEBUG 0
 
+// uno: pin 2 = int.0 or pin 3 = int.1 see
+// http://arduino.cc/en/Reference/attachInterrupt
+#define ISR_PIN 2
+#define ISR_INT 0
 
 // Constants
-const unsigned long sync_MIN = 4300;                      // Minimum Sync time in micro seconds
-const unsigned long sync_MAX = 4700;
+const unsigned long sync_MIN = 9600;                      // Minimum Sync time in micro seconds
+const unsigned long sync_MAX = 9900;
 
-const unsigned long bit1_MIN = 2300;
-const unsigned long bit1_MAX = 2700;
+const unsigned long bit1_MIN = 4200;
+const unsigned long bit1_MAX = 4600;
 
-const unsigned long bit0_MIN = 1330;
-const unsigned long bit0_MAX = 1730;
+const unsigned long bit0_MIN = 2200;
+const unsigned long bit0_MAX = 2600;
 
 const unsigned long glitch_Length = 300;                  // Anything below this value is a glitch and will be ignored.
 
@@ -40,9 +46,9 @@ unsigned long build_Buffer[] = {0,0};                     // Placeholder last da
 volatile unsigned long read_Buffer[] = {0,0};             // Placeholder last full data packet read.
 volatile byte isrFlags = 0;                               // Various flag bits
 
-void PinChangeISR0(){                                     // Pin 2 (Interrupt 0) service routine
+void PinChangeISR(){                                     // Pin 2 (Interrupt 0) service routine
   unsigned long Time = micros();                          // Get current time
-  if (digitalRead(2) == LOW) {
+  if (digitalRead(ISR_PIN) == LOW) {
 // Falling edge
     if (Time > (rise_Time + glitch_Length)) {
 // Not a glitch
@@ -125,67 +131,66 @@ digitalWrite(13,LOW); // Used for debugging
 
 
 void setup() {
-pinMode(13,OUTPUT); // Used for debugging
+  pinMode(13,OUTPUT); // Used for debugging
   Serial.begin(9600);
-  pinMode(2,INPUT);
-  Serial.println(F("ISR Pin 2 Configured For Input."));
-  attachInterrupt(0,PinChangeISR0,CHANGE);
-  Serial.println(F("Pin 2 ISR Function Attached. Here we go."));
+  pinMode(ISR_PIN,INPUT);
+  attachInterrupt(ISR_INT,PinChangeISR,CHANGE);
+  Serial.print("# Pin ");
+  Serial.print(ISR_PIN);
+  Serial.print(" int ");
+  Serial.print(ISR_INT);
+  Serial.println(" ISR attached");
 }
 
 void loop() {
-  unsigned long myData0 = 0;
-  unsigned long myData1 = 0;
-  if (bitRead(isrFlags,F_GOOD_DATA) == 1) 
-{
+  if (bitRead(isrFlags,F_GOOD_DATA) == 1) {
+    serial_dump();
+    delay(100);
+  }
+}
+
+void serial_dump(void) {
+    unsigned long myData0 = 0;
+    unsigned long myData1 = 0;
+
     // We have at least 2 consecutive matching reads
-    myData0 = read_Buffer[0]; // Read the data spread over 2x 32 variables
-    myData1 = read_Buffer[1];
+    myData0 = read_Buffer[0]; // first 4 bits
+    myData1 = read_Buffer[1]; // rest of 32 bits
     bitClear(isrFlags,F_HAVE_DATA); // Flag we have read the data
+
+if (DEBUG) {
+    Serial.print("# 0=");
     dec2binLong(myData0,DataBits0);
+    Serial.print(" 1=");
     dec2binLong(myData1,DataBits1);
+    Serial.println();
+}
+
+    Serial.print("temp=");
+    int temp = ( myData1 >> 8 ) & 0xFFF;
+    if ( temp & 0x800 ) temp = temp | 0xF000;
+    Serial.print(temp / 10.0, 1);
+
+    Serial.print(" humidity=");
+    Serial.print( myData1 & 0xFF );
+
+    byte b1 = ( myData1 >> 20 ); // second byte from received packet
+
+    Serial.print(" button=");
+    Serial.print( b1 & 0x04);
+
+    Serial.print(" battery=");
+    Serial.print( b1 & 0x08);
+
+    Serial.print(" channel=");
+    Serial.print( ( b1 & 0x03 ) + 1 );
+
+    Serial.print(" rid=");
+    Serial.println( myData1 >> 24 );
+
+    // remote but so we don't see this packet again
+    bitClear(isrFlags, F_GOOD_DATA);
 
-    Serial.print(" - Battery=");
-    byte H = (myData1 >> 26) & 0x3;   // Get Battery
-    Serial.print(H);
-    
-    Serial.print(" Channel=");
-    H = ((myData1 >> 24) & 0x3) + 1;        // Get Channel
-    Serial.print(H);
-    
-    Serial.print(" Temperature=");
-    byte ML = (myData1 >> 12) & 0xF0; // Get MMMM
-//     Serial.print(" (M=");
-//     Serial.print(ML);
-    H = (myData1 >> 12) & 0xF;        // Get LLLL
-//     Serial.print(" L=");
-//     Serial.print(H);
-    ML = ML | H;                      // OR MMMM & LLLL nibbles together
-    H = (myData1 >> 20) & 0xF;        // Get HHHH
-//     Serial.print(" H=");
-//     Serial.print(H);
-//     Serial.print(" T= ");
-    byte HH = 0;
-    if((myData1 >> 23) & 0x1 == 1) //23 bit
-         HH = 0xF;
-    int Temperature = (H << 8)|(HH << 12) | ML;  // Combine HHHH MMMMLLLL
-//     Serial.print( Temperature);
-//     Serial.print(") ");
-    // Temperature = Temperature*3; //F // Remove Constant offset
-    Serial.print(Temperature/10.0,1);   
-    Serial.print("C Humidity=");
-    H = (myData1 >> 0) & 0xF0;        // Get HHHH
-//     Serial.print(" (H=");
-//     Serial.print(H);
-    ML = (myData1 >> 0) & 0xF;       // Get LLLL
-//     Serial.print(" L=");
-//     Serial.print(ML);
-//     Serial.print(") ");
-    ML = ML | H;                      // OR HHHH & LLLL nibbles together
-    Serial.print(ML);
-    Serial.println("%");
-  }
-  delay(100);
 }
 
 void dec2binLong(unsigned long myNum, byte NumberOfBits) {
@@ -197,6 +202,7 @@ void dec2binLong(unsigned long myNum, byte NumberOfBits) {
       else 
       Serial.print("0");
       myNum = myNum << 1;
+      if ( i % 4 == 3 ) Serial.print(" ");
     }
   }
 }