Issue with FOR loop

The code fragment below is expected to produce some server.log output, but none appears. Is there something wrong with the FOR statement?

`
OnCount <- 0
OffCount <- 0
const threshold = 37000 // readings above 37000 (at night) mean the lamp is illuminated.

// Function to take and record a light reading, using Imp’s built-in light sensor

function measureBrightness() {
local brightness = hardware.lightlevel()
server.log("Brightness: " + brightness) // Remove after testing.
if (brightness >= threshold)
OnCount += 1
else
OffCount += 1
}

// Measure light level ten times at 0.5 second intervals

for (local a=1; a <= 10; a+=1)
imp.wakeup(0.5, measureBrightness)
`

You should be seeing output with that, however your code is not doing what the description says it wants; you’re queueing 10 wakeups to fire in 0.5s all of which will immediately call measureBrightness(), so there will be 10 readings logged immediately.

What you want is probably this:

`OnCount <- 0
OffCount <- 0
const threshold = 37000 // readings above 37000 (at night) mean the lamp is illuminated.
readings <- 0

// Function to take and record a light reading, using Imp’s built-in light sensor

function measureBrightness() {
local brightness = hardware.lightlevel()
server.log("Brightness: " + brightness) // Remove after testing.
if (brightness >= threshold)
OnCount += 1
else
OffCount += 1

// If we've not done this 10 times, do it again in 0.5s
if (++readings < 10) imp.wakeup(0.5, measureBrightness);

}

// Measure light level ten times at 0.5 second intervals
measureBrightness();`

(I confirmed that your code does print brightness just fine, and mine does do more what you want it to)

Thank you, Hugo. I’m surprised that you saw output from my code. I don’t.

With your code, I see one brightness reading, not the expected ten readings.

I wonder about your use of “++readings” I thought that was written “readings++”

I now understand the difference between ++ as a prefix and as a suffix.

Tomcox,
Do you have other code after this? Such as a while(1) or long imp.sleep()s? It sounds like timers are never firing for you which would indicate something is blocking them.

Here is the complete device code:

`
// Beacon Monitor program for Imp device
//
// (Beacon cycle is about 1.9 seconds on and 0.8 second off)
//
// Initialize

onCount <- 0
offCount <- 0
readings <- 0
const threshold = 37000 // readings above 37000 (at night) mean the lamp is illuminated.

// Function to take and record a light reading, using Imp’s built-in light sensor

function measureBrightness() {
local brightness = hardware.lightlevel()
server.log("Brightness: " + brightness) // Remove after testing.
if (brightness >= threshold)
onCount += 1
else
offCount += 1

// If we've not done this less than ten times, do it again in 0.5s
if (++readings < 10) imp.wakeup(0.5, measureBrightness);

}

// Measure light level ten times at 0.5 second intervals
measureBrightness();

// Publish the totals (remove after testing)
server.log("ON: " + onCount)
server.log("OFF: " + offCount)

// Send beacon status to agent, but ONLY if time is between midnight and 2 a.m. This is
// to avoid messages that would be triggered by cycling imp power.

local now = date()
local currentHour = now.hour
if ((currentHour > 5) && (currentHour < 7)) { // 5 UTC is midnight CDT. 7 UTC is 2 am CDT.
if ((onCount > 0) && (offCount > 0)) // If flashing, we should have a mixture of on and off readings.
agent.send(“beacon”, “flashing”)
else
agent.send(“beacon”, “NOT flashing”)
}

// Sleep until 1am CDT (= 6 UTC)

imp.onidle(function() { server.sleepuntil(6, 0) } )
`

and here is the resulting device log:

2015-08-13 18:44:49 UTC-5 [Status] Device disconnected
2015-08-13 18:44:49 UTC-5 [Status] Downloading new code; 1.22% program storage used
2015-08-13 18:44:49 UTC-5 [Device] Brightness: 63183
2015-08-13 18:44:49 UTC-5 [Device] ON: 1
2015-08-13 18:44:49 UTC-5 [Device] OFF: 0
2015-08-13 18:44:49 UTC-5 [Device] sleeping until 1439532000000

The code is doing exactly what you’ve asked it to do.

measureBrightness() is executed the first time and the next call of it is scheduled in 0.5s

In the meantime, it prints out your onCount and Offcount and sends your beacon. Your problem starts with imp.onidle(). Even though you have cued measureBrightness to execute 10 times, the CPU will be idle in between.
The upshot of this is that your imp will go to sleep immediately after setting up your idle handler and wakeup at 1am, execute another measureBrightness again before going to sleep until the next day.

To operate the way (I think) you intend, you should grab all the code below your call to measureBrightness and stick it in an else {} block after if (++readings < 10) imp.wakeup(0.5, measureBrightness);

Thank you, coverdriven. I believe you have made it work. Now I need to figure it out.