Permalink
Browse files

Add simple LDR light level example

  • Loading branch information...
1 parent 20a6ffd commit 5951cc01d991a66a2d4d091b13dc7c6b553d7ef7 @nathanchantrell committed Aug 2, 2012
Showing with 90 additions and 0 deletions.
  1. +5 −0 README
  2. +85 −0 TinyTX_LDR/TinyTX_LDR.ino
View
@@ -27,6 +27,11 @@ TinyTX_LDR_Meter
With LDR to read LED pulses on electricity meter.
+TinyTX_LDR
+----------------
+With LDR to read light levels.
+
+
TinyTX_RX_Simple
----------------
A simple receive example, outputing received data on the serial monitor.
@@ -0,0 +1,85 @@
+//----------------------------------------------------------------------------------------------------------------------
+// TinyTX_LDR - An ATtiny84 and RFM12B Wireless Light Sensor Node
+// By Nathan Chantrell. For hardware design see http://nathan.chantrell.net/tinytx
+//
+// Using an LDR and 10K resistor
+// LDR between A0 (ATtiny pin 13) and ground and 10K between A0 and D9 (ATtiny pin 12)
+//
+// Licenced under the Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) licence:
+// http://creativecommons.org/licenses/by-sa/3.0/
+//
+// Requires Arduino IDE with arduino-tiny core: http://code.google.com/p/arduino-tiny/
+//----------------------------------------------------------------------------------------------------------------------
+
+#include <JeeLib.h> // https://github.com/jcw/jeelib
+
+ISR(WDT_vect) { Sleepy::watchdogEvent(); } // interrupt handler for JeeLabs Sleepy power saving
+
+#define myNodeID 1 // RF12 node ID in the range 1-30
+#define network 210 // RF12 Network group
+#define freq RF12_433MHZ // Frequency of RFM12B module
+
+//########################################################################################################################
+//Data Structure to be sent
+//########################################################################################################################
+
+ typedef struct {
+ int light; // Light reading
+ int supplyV; // Supply voltage
+ } Payload;
+
+ Payload tinytx;
+
+//########################################################################################################################
+
+void setup() {
+
+ rf12_initialize(myNodeID,freq,network); // Initialize RFM12 with settings defined above
+ rf12_sleep(0); // Put the RFM12 to sleep
+
+ pinMode(9, OUTPUT); // set D9 to output
+
+}
+
+void loop() {
+
+ digitalWrite(9, HIGH); // set D9 high
+ delay(10);
+ tinytx.light = 1024-analogRead(0); // read the input pin (A0)
+ digitalWrite(9, LOW); // set D9 low
+
+ tinytx.supplyV = readVcc(); // Get supply voltage
+
+ rfwrite(); // Send data via RF
+
+ Sleepy::loseSomeTime(60000); //JeeLabs power save function: enter low power mode for 60 seconds (valid range 16-65000 ms)
+
+}
+
+//--------------------------------------------------------------------------------------------------
+// Send payload data via RF
+//--------------------------------------------------------------------------------------------------
+ static void rfwrite(){
+ rf12_sleep(-1); //wake up RF module
+ while (!rf12_canSend())
+ rf12_recvDone();
+ rf12_sendStart(0, &tinytx, sizeof tinytx);
+ rf12_sendWait(2); //wait for RF to finish sending while in standby mode
+ rf12_sleep(0); //put RF module to sleep
+}
+
+//--------------------------------------------------------------------------------------------------
+// Read current supply voltage
+//--------------------------------------------------------------------------------------------------
+ long readVcc() {
+ long result;
+ // Read 1.1V reference against Vcc
+ ADMUX = _BV(MUX5) | _BV(MUX0);
+ delay(2); // Wait for Vref to settle
+ ADCSRA |= _BV(ADSC); // Convert
+ while (bit_is_set(ADCSRA,ADSC));
+ result = ADCL;
+ result |= ADCH<<8;
+ result = 1126400L / result; // Back-calculate Vcc in mV
+ return result;
+}

0 comments on commit 5951cc0

Please sign in to comment.