1: those resistor values don't get me "good" results for some reason when reading analog from A1
2: I'm not sure wiring a transistor in series with a 10k resistor with Collector on +3.3V & Emitter on A0 'll effectively give me ( for correct resistor value for 1 ) sort of an 'analogWatch' via 2 pins ?
I'm using the following code for my tests:
// pin 0 has the digital watch on it - we may change the pinMode if needed
pinMode(A0, "input_pulldown"); setWatch(function(e) { console.log('A0 pressed'); }, A0, { repeat:true, edge:'rising', debounce:100 });
// first thing I tried ;)
var N = 20;
var voltage = 0;
for (var i=0;i<N;i++) {
voltage += analogRead(A1)*3.3/N;
}
console.log('voltage: ' + voltage);
// when reading, get the biggest val (mod from @MaBe's "analogReadMedian()" )
analogReadBiggest = function(p, sampleCount) {
var i, median;
var samples = Math.floor(sampleCount);
var analogValues = new Array(samples);
// read analog values into array
i = samples;
while(i--) analogValues[i] = analogRead(p);
//sort array, smalest first and largest last
analogValues.sort(function(a, b){return a-b;});
return analogValues[analogValues.length-1]; // return biggest
};
I guess I'll have to be more cautious on the length of the wires used between uC & the resistors to have something ~constant in this circuit, hoping the erroneous readings come from that ..
---- update ----
My last try for today, unsuccessful :/
// trying to get this 5-way switch from 5 pins down to 1 digital
// ( for setWatch() ) & one analog ( to read the five different values through different resistors )
// pin 0 has the digital watch on it
pinMode(A0, "input_pulldown");
setWatch(function(e) {
console.log('A0 pressed');
console.log('A1 biggest is: ' + analogReadBiggest(A1, 1000));
console.log('A1 lowest is: ' + analogReadLowest(A1, 1000));
}, A0, { repeat:true, edge:'rising', debounce:100 });
pinMode(A1, "input"); // has external 10k pulldown resistor
// pin A1 has a 10k pulldown & various pullup resistors depending on the "path travelled" for 5-way switch
// to get the biggest val
analogReadBiggest = function(p, sampleCount) {
var i;
var samples = Math.floor(sampleCount);
var analogValues = new Array(samples);
// read analog values into array
i = samples;
while(i--) analogValues[i] = analogRead(p);
return analogValues.sort(function(a, b){return a-b;})[analogValues.length-1]; // return biggest
};
analogReadLowest = function(p, sampleCount) {
var i;
var samples = Math.floor(sampleCount);
var analogValues = new Array(samples);
// read analog values into array
i = samples;
while(i--) analogValues[i] = analogRead(p);
return analogValues.sort(function(a, b){return a-b;})[0]; // return lowest
};
// for a 10k pulldown, we get the below ( erratic ) outputs
// 1:
// 10k & nothing ( 500 readings using analogReadBiggest) -> 0.00537117570
// 10k & nothing ( 500 readings using analogReadLowest) -> 0.00512703135
// 2:
// 10k & 10k ( 500 readings using analogReadBiggest) -> 0.00537117570
// 10k & 10k ( 500 readings using analogReadLowest) -> 0.00561532005
// 3:
// 10k & 2.2k ( 500 readings using analogReadBiggest) -> 0.00512703135
// 10k & 2.2k ( 500 readings using analogReadLowest) -> 0.00512703135
// 4:
// 10k & 3.2k ( 500 readings using analogReadBiggest) -> 0.00537117570
// 10k & 3.2k ( 500 readings using analogReadLowest) -> 0.00537117570
// 5:
// 10k & 5.7k ( 500 readings using analogReadBiggest) -> 0.00512703135
// 10k & 5.7k ( 500 readings using analogReadLowest) -> 0.00512703135
Hoping to have a clearer view of the troubles in the morning ;)
++
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.
I just tested something like http://www.reuk.co.uk/wordpress/wp-content/uploads/2013/09/selector-switch-resistor-ladder-adc.jpg, but:
I'm using the following code for my tests:
I guess I'll have to be more cautious on the length of the wires used between uC & the resistors to have something ~constant in this circuit, hoping the erroneous readings come from that ..
---- update ----
My last try for today, unsuccessful :/
Hoping to have a clearer view of the troubles in the morning ;)
++