So I just received my pH Meter and shield from df, however, I am running into a little trouble converting the library to JS. I have taken the Arduino code from here[http://www.dfrobot.com/wiki/index.php/PH_meter(SKU:_SEN0161)], but I can't seem to find an equivalent to "millis();". Is there a way around this? Am I even going in the right direction?
Thanks.
function SEN0161(pin) {
this.pin = pin;
}
SEN0161.prototype.getScale = function () {
pin = this.pin;
var val = digitalRead(pin);
var Offset = 0.00;
var LED = 13;
var samplingInterval = 20;
var printInterval = 800;
var ArrayLength = 40;
var pHArray = [ArrayLength];
var pHArrayIndex = 0;
var samplingTime = '';
var printTime = Date.now();
var pHValue,voltage;
//console.log(Date.now());
//console.log(digitalPulse(this.pin,1,5));
if(Date.now() - samplingTime > samplingInterval) {
pHArray[pHArrayIndex++] = analogRead(this.pin);
if(pHArrayIndex == ArrayLenth)pHArrayIndex=0;
voltage = this.averageArray(pHArray, ArrayLength) * 5.0 / 1024;
pHValue = 3.5 * voltage + Offset;
samplingTime = Date.now();
}
if(Date.now() - printTime > printInterval) {
console.log("Voltage:");
console.log(voltage,2);
console.log(" pH value: ");
console.log(pHValue,2);
digitalWrite(LED,digitalRead(LED)^1);
printTime = Date.now();
}
};
SEN0161.prototype.averageArray = function (arr, number) {
var i;
var max,min;
var avg;
var amount=0;
if(number <= 0){
console.log("Error number for the array to avraging!/n");
return 0;
}
if(number < 5) { //less than 5, calculated directly statistics
for(i=0; i < number; i++) {
amount += arr[i];
}
avg = amount/number;
return avg;
} else {
console.log(arr);
if(arr[0] < arr[1]) {
min = arr[0]; max=arr[1];
} else {
min=arr[1];
max=arr[0];
}
console.log(min);
console.log(max);
for(i=2; i < number; i++) {
if(arr[i] < min) {
amount += min; //arr<min
min = arr[i];
} else {
if(arr[i] > max) {
amount += max; //arr>max
max = arr[i];
} else {
amount += arr[i]; //min<=arr<=max
}
}
}
avg = amount/(number-2);
}
return avg;
};
exports.connect = function (pin) {
return new SEN0161(pin);
};
//exports.connect();
var sensor = exports.connect(A1);
console.log(sensor);
//console.log(sensor.getScale());
setInterval('sensor.getScale()', 1000);
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.
Hi,
So I just received my pH Meter and shield from df, however, I am running into a little trouble converting the library to JS. I have taken the Arduino code from here[http://www.dfrobot.com/wiki/index.php/PH_meter(SKU:_SEN0161)], but I can't seem to find an equivalent to "millis();". Is there a way around this? Am I even going in the right direction?
Thanks.