MikeyDK
21
Yes, every time the switch in the sensor is closed, it pulls it down to gnd, and after it is opened again it goes back up to 3v3.
With an arduino I would have used an interrupt pin and either millis() or micro() and then just calculate the time between the two.
MikeyDK
22
Tried to make some new code, but still seems to return wrong numbers.
`
pulseLength <- 0;
pulseLow <- false;
function onBufferFull(buffer, length)
{
// process the buffer:
local bufferLength = length/2; // each sample is 2 bytes
local windSpeed;
for(local i = 0; i < bufferLength; i++)
{
pulseLength++;
// read the value
local dataPoint = buffer.readn('w');
if (dataPoint >= HIGH_THRESH && pulseLow)
{
windSpeed = pulseLength / 1000.0 * 2.4; // in KPH
server.log("Windspeed " + windSpeed + "kmh / " + pulseLength + "ms");
pulseLength = 0;
pulseLow = false;
}
else if (dataPoint <= LOW_THRESH)
{
pulseLow = true;
}
}
agent.send(“windSpeed”, windSpeed);
}`
output from this is
2014-09-16 11:02:36 UTC+2 [Device] Windspeed 1.596kmh / 665ms 2014-09-16 11:02:37 UTC+2 [Device] Windspeed 1.7568kmh / 732ms 2014-09-16 11:02:38 UTC+2 [Device] Windspeed 1.5336kmh / 639ms 2014-09-16 11:02:38 UTC+2 [Device] Windspeed 1.5912kmh / 663ms 2014-09-16 11:02:39 UTC+2 [Device] Windspeed 1.4088kmh / 587ms 2014-09-16 11:02:39 UTC+2 [Device] Windspeed 1.5288kmh / 637ms 2014-09-16 11:02:40 UTC+2 [Device] Windspeed 1.4304kmh / 596ms 2014-09-16 11:02:41 UTC+2 [Device] Windspeed 1.5624kmh / 651ms 2014-09-16 11:02:41 UTC+2 [Device] Windspeed 1.5216kmh / 634ms 2014-09-16 11:02:42 UTC+2 [Device] Windspeed 1.7136kmh / 714ms
But it still seems wrong, longer time still gives a higher number, but should give a lower.
MikeyDK
23
Might have found the problem! pulseLength / 1000.0 should have been 1000.0 / pulseLength instead.
`pulseLength <- 0;
pulseLow <- false;
function onBufferFull(buffer, length)
{
// process the buffer:
local bufferLength = length/2; // each sample is 2 bytes
local windSpeed;
for(local i = 0; i < bufferLength; i++)
{
pulseLength++;
// read the value
local dataPoint = buffer.readn('w');
if (dataPoint >= HIGH_THRESH && pulseLow)
{
windSpeed = (1000.0 / pulseLength) * 2.4; // in KPH
server.log("Windspeed " + windSpeed + "kmh / " + pulseLength + "ms");
pulseLength = 0;
pulseLow = false;
}
else if (dataPoint <= LOW_THRESH)
{
pulseLow = true;
}
}
agent.send(“windSpeed”, windSpeed);
}`
returns
2014-09-16 11:06:26 UTC+2 [Device] Windspeed 3.4384kmh / 698ms 2014-09-16 11:06:27 UTC+2 [Device] Windspeed 3.70943kmh / 647ms 2014-09-16 11:06:27 UTC+2 [Device] Windspeed 3.2345kmh / 742ms 2014-09-16 11:06:28 UTC+2 [Device] Windspeed 3.3195kmh / 723ms 2014-09-16 11:06:29 UTC+2 [Device] Windspeed 3.13316kmh / 766ms 2014-09-16 11:06:30 UTC+2 [Device] Windspeed 3.39943kmh / 706ms 2014-09-16 11:06:30 UTC+2 [Device] Windspeed 3.10881kmh / 772ms 2014-09-16 11:06:31 UTC+2 [Device] Windspeed 3.20427kmh / 749ms 2014-09-16 11:06:32 UTC+2 [Device] Windspeed 2.95203kmh / 813ms 2014-09-16 11:06:33 UTC+2 [Device] Windspeed 3.29218kmh / 729ms 2014-09-16 11:06:33 UTC+2 [Device] Windspeed 3.069054kmh / 782ms 2014-09-16 11:06:34 UTC+2 [Device] Windspeed 3.44333kmh / 697ms 2014-09-16 11:06:35 UTC+2 [Device] Windspeed 3.42368kmh / 701ms 2014-09-16 11:06:35 UTC+2 [Device] Windspeed 3.79147kmh / 633ms
and just some testing of the calculation
Windspeed 9.6kmh / 250ms Windspeed 4.8kmh / 500ms Windspeed 2.4kmh / 1000ms Windspeed 1.92kmh / 1250ms Windspeed 1.6kmh / 1500ms Windspeed 1.2kmh / 2000ms
hugo
24
Yep, that looks right - I said you needed reciprocal
MikeyDK
25
Almost ready to get first batch made.