What I am attempting to do is write a function to handle the "i2c read request" for all of the sensor modules; I would also like to repeat the "all the sensor readings" every 5 seconds or possibly longer than 5 seconds.
Depending on what command I send to the module indicates how long I should wait in milliseconds. For example if I send the "R" command, I am required to wait 1second or 1000milliseconds to invoke the I2C1.readFrom function. Most of the other commands that I send only require that I wait a total of 300milliseconds before I invoke the I2C1.readFrom function. This just insures that I give the sensor modules enough time to calculate the results before I readFrom the sensor module.
I am not sure If i should write multiple functions for the different sensors(ph, ec, temp) or just write one function to read from them all? So far I started to write a function before I asked this question to read from them all, but only started with the ph sensor as I would like to get something working and then branch off from there.
Code:
function getphi2cData(phi2c) {
var data = I2C1.readFrom(phi2c);
var toChar="";
var i;
for(i=0; i<d.length; i++) {
toChar += String.fromCharCode(d[i]);
if(i==(d.length-1)) {
console.log(toChar);
toChar="";
}
}
}
function geteci2cData(eci2c) {
}
function geti2cData(phi2c, eci2c) {
I2C1.writeTo(phi2c, "R,25.3");
setTimeout(function (e) { getphi2cData(phi2c); }, 1000);
//setTimeout(function (e) { geteci2cData(eci2c); }, 1000);
}
I remed out the geteci2cData(eci2c) function because I wasn't sure if that would execute before the getphi2cData(phi2c) finished processing the data. However, I think I should be ok because as Gordon mentioned, "other tasks cannot execute until the current task has finished."?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
What I am attempting to do is write a function to handle the "i2c read request" for all of the sensor modules; I would also like to repeat the "all the sensor readings" every 5 seconds or possibly longer than 5 seconds.
Depending on what command I send to the module indicates how long I should wait in milliseconds. For example if I send the "R" command, I am required to wait 1second or 1000milliseconds to invoke the I2C1.readFrom function. Most of the other commands that I send only require that I wait a total of 300milliseconds before I invoke the I2C1.readFrom function. This just insures that I give the sensor modules enough time to calculate the results before I readFrom the sensor module.
I am not sure If i should write multiple functions for the different sensors(ph, ec, temp) or just write one function to read from them all? So far I started to write a function before I asked this question to read from them all, but only started with the ph sensor as I would like to get something working and then branch off from there.
Code:
I remed out the geteci2cData(eci2c) function because I wasn't sure if that would execute before the getphi2cData(phi2c) finished processing the data. However, I think I should be ok because as Gordon mentioned, "other tasks cannot execute until the current task has finished."?