Skip to main content

You are here

LDR Jeenode Oddity

1 post / 0 new
stroffduino
LDR Jeenode Oddity

Hello,

I cobbled together some code from the pingpong sketch and the LDR example (http://jeelabs.net/projects/cafe/wiki/POF_03_-_Wireless_light_sensor). I'm getting strange readings when the room is dark at night. It's very possible that this is not specific to the Jeenode. I'm pretty new at this. Anyway, as you can see in the jpg, during the day, the values are seen as expected. At night, there is a large jump, then back to normal in the morning. I set the Jeenode up as shown in the example page except mine is connected to USB. Code to follow. Any ideas?

Thanks!

/// @dir pingPong
/// Demo of a sketch which sends and receives packets.
// 2010-05-17 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

// with thanks to Peter G for creating a test sketch and pointing out the issue
// see http://news.jeelabs.org/2010/05/20/a-subtle-rf12-detail/

#include <JeeLib.h>

Port ldr (3);
MilliTimer sendTimer;
MilliTimer aliveTimer;

typedef struct {
byte srcID; // source node ID
byte destID; // destination node ID
int curTemp; // current temperature: -500..+500 (tenths)
int tarTemp; // target temperature: -500..+500 (tenths)
byte light; // light sensor: 0..255
byte humi; // humidity: 0..100
byte moved; // motion detector: 0..1
byte lobat; // supply voltage dropped under 3.1V: 0..1
}
payload;

payload valReceived;
payload valToSend;
payload prevValToSend;
byte needToSend;
word runningAvg;
byte thisNodeID;
int seconds = 900; // send every 15 minutes

void setup () {
Serial.begin(57600);
thisNodeID = rf12_config();
valToSend.srcID = thisNodeID;
valToSend.destID = 1;

// enable pull-up on LDR analog input
ldr.mode2(INPUT);
ldr.digiWrite2(1);
// prime the running average
runningAvg = ldr.anaRead();
}

void loop () {
if (rf12_recvDone() && rf12_crc == 0) {
valReceived = *(payload*) rf12_data;
Serial.print("OK ");
for (byte i = 0; i < rf12_len; ++i)
Serial.print(rf12_data[i]);
Serial.println();

if (valReceived.srcID == byte(1) && valReceived.destID == thisNodeID && valToSend.tarTemp != valReceived.tarTemp) { //
valToSend.tarTemp = valReceived.tarTemp;
Serial.println("changing target temp");
}
}

if (sendTimer.poll(3000)) {

// keep track of a running 5-second average
//runningAvg = (4 * runningAvg + ldr.anaRead()) / 5;
// convert analog 0..1023 readings to a 0..255 light level
//valToSend.light = 255 - runningAvg / 4;

valToSend.light = 225 - ldr.anaRead()/4;

Serial.println(valToSend.light);
//--------------------check to see if values are equal before sending
if (prevValToSend.light == valToSend.light && prevValToSend.curTemp == valToSend.curTemp)
{
needToSend = 0;
}
else
{
needToSend = 1;
}
}

if (aliveTimer.poll(1000) && --seconds <= 0){
needToSend = 1;
seconds = 900;
}

if (needToSend && rf12_canSend()) {
needToSend = 0;
rf12_sendStart(0, &valToSend, sizeof valToSend);
prevValToSend = valToSend;
}
}

AttachmentSize
untitled.jpg35.93 KB
Premium Drupal Themes by Adaptivethemes