@otso
the line for deactivating was really in the wrong place. This happened when I moved things around for testing. Most of the times it was inside the loop. I used now the code below for testing.
I now ran a serious of tests with the code below using different intervall settings and timeout settings. Conclusion is:
- When setTimeout is not used the code behaves as expected
- When setInterval is larger for interval 2 than for interval 1 the code behaves as expected
- When setInterval is larger for interval1 than for interval 2 then the code does not behave as expected and prints the data in interval 2 to the console only every X times it prints it for data in interval 1. X depends on the value for setTimeout. The longer the timeout the more “normal” the code behaves.
I am using the code now without setTimeout. I would like to know why the code behaves as it does but I think I just let it rest now.
var Ruuvitag = require("Ruuvitag");
Ruuvitag.setEnvOn(true);
setInterval(function(){
pinMode(18, 'input_pulldown');
// setTimeout(function(){
var door = digitalRead(18);
var temperature = [Math.round(Ruuvitag.getEnvData().temp*100)/100];
console.log("Door = " + door);
console.log("Temperature: " + temperature);
NRF.setAdvertising({
0x2a3f : [door],
0x1809 : [encodeFloat100(temperature)],
});
pinMode(18, 'input');
//},10);
}, 20000);
setInterval(function(){
var pressure = [Math.round(Ruuvitag.getEnvData().pressure*10)/10];
var humid = [Math.round(Ruuvitag.getEnvData().humidity*10)/10];
var battery = [Math.round(NRF.getBattery()*100)/100];
console.log("Battery [V]: " + battery);
console.log("Pressure: " + pressure);
console.log("Humidity: " + humid);
NRF.setAdvertising({
0x2A6D : [encodeFloat10(pressure)],
0x2A6F : [encodeFloat10(humid)],
0xfffe : [encodeFloat100(battery)]
});
},60000);
function encodeFloat10(num) {
var d = Math.round(num*10);
return [ d&255, d >> 8 ];
}
function encodeFloat100(num) {
var d = Math.round(num*100);
return [ d&255, d >> 8 ];
}
Test results:
Test 1
setInterval 1: 2000ms setTimeout: 10ms
setInterval 2: 4000ms
approx. every 27 times (varies slightly) when when console.log is done for interval 1 it is also shown for interval 2
Test 2
setInterval 1: 2000ms setTimeout: 50ms
setInterval 2: 4000ms
approx. every 17 times when when console.log is done for interval 1 it is also shown for interval 2
Test 3
setInterval 1: 2000ms setTimeout: 500ms
setInterval 2: 4000ms
Every 5 times when when console.log is done for interval 1 it is also shown for interval 2
Test 4
setInterval 1: 2000ms setTimeout: 1000ms
setInterval 2: 4000ms
Every 3 times when when console.log is done for interval 1 it is also shown for interval 2
Test 5
setInterval 1: 4000ms setTimeout: 10ms
setInterval 2: 2000ms
Every second time console.log is done for interval 2 it also is shown for interval 1 (data of both console log data slightly mixed)
Test 6
setInterval 1: 2000ms no setTimeout used/disabled in the code below
setInterval 2: 4000ms
Every 2nd time console log is done for interval 1 it is also done for interval 2.