Project

General

Profile

LDR Jeenode Oddity

Added by stroffduino over 6 years ago

Moving this post from the old forums…

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

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;
}
}


Replies (9)

RE: LDR Jeenode Oddity - Added by padvinder95 over 6 years ago

You’re sending 225 - anaRead()/4. Should this be 255 - anaRead()/4?

RE: LDR Jeenode Oddity - Added by jcw over 6 years ago

Wow - eagle eyes! I couldn’t figure out what was wrong from reading the code…

Note that this forum supports (pre) and (code) HTML tags, i.e. (code class=“C”) inside (pre) will colorise things (do use angle brackets):

  if (sendTimer.poll(3000)) {
    valToSend.light = 225 - ldr.anaRead()/4;
    Serial.println(valToSend.light);
    ...
  }

RE: LDR Jeenode Oddity - Added by stroffduino over 6 years ago

Thank you for your replies. I am still a little confused. I am following the example located
http://jeelabs.net/projects/cafe/wiki/POF\_03\_~~\_Wireless\_light\_sensor.
<pre>
// convert analog 0..1023 readings to a 0..255 light level
byte value = 255~~ ldr.anaRead() / 4;

I assume that the purpose of this line is to convert into a number that can be stored as a byte. Should I be doing something differently here?

Thank you, and sorry for the messy code.

RE: LDR Jeenode Oddity - Added by jcw over 6 years ago

Correct. The ADC reading is 0..1023, when divided by 4 it becomes 0..255. The last step then reverses the result, i.e. 0 > 255, and 255> 0. So that a high value means “much light”, and 0 means “no light”.

RE: LDR Jeenode Oddity - Added by stroffduino over 6 years ago

I’m sorry, are you saying that the line is or is not the reason for the problem? If the line is correct, is there another reason that the readings error in low light? Should I be depending on the internal pull up? Thanks again.

RE: LDR Jeenode Oddity - Added by martynj over 6 years ago

The line should read

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

The erroneous 225 causes the sign to change for low reading values, making the apparent value ‘jump’.

RE: LDR Jeenode Oddity - Added by stroffduino over 6 years ago

Wow. I’m really embarrassed that I didn’t catch that earlier. Thank you all for your time.

RE: LDR Jeenode Oddity - Added by gadjet over 6 years ago

Don’t be too embarassed I had to read the line 4 or 5 times before I spotted the 255 was actually 225 ;-)

RE: LDR Jeenode Oddity - Added by padvinder95 over 6 years ago

Certainly no need to be embarrassed about making the typo and not spotting it—about not reading my first reply carefully enough, maybe a bit.

I must have mentioned this on the weblog or forums once already, but I once spent almost 30 minutes debugging a rather long SQL query, taking out parts, substituting back ticks and single quotes; in the end I—by now almost desperate—showed it to a friend who said, after looking at it for about 2 seconds: “You’ve written UDPATE instead of UPDATE.”

martynj: byte is an alias for unsigned char, so technically not really a sign change, but definitely an under-flow that causes values above 4*225 to wrap around the zero towards the 255-225 range, indeed causing a jump in the value.

    (1-9/9)