Last active
December 17, 2016 14:59
-
-
Save tomgidden/aa1be3d7e5e6b7bf89ff6390e6f123a5 to your computer and use it in GitHub Desktop.
Espruino test code for Velostat + copper based mat (i-Mattress-Sens-U-Pedic 2000)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ipins = [A3, A4, A5]; | |
var opins = [B2, B3, B4]; | |
var xs = opins.length; | |
var ys = ipins.length; | |
function readCol(x) { | |
var j, k, res=[]; | |
// First, set the column lines to either +3.3V or "pin open circuit" | |
for (j=0; j<xs; j++) { | |
if (j == x) { | |
// Set selected column as +3.3V | |
pinMode(opins[j], 'output'); | |
digitalWrite(opins[j], 1); | |
} | |
else { | |
// Set unselected columns as floating ("Digital output that | |
// only ever pulls down to 0v. Sending a logical 1 leaves the | |
// pin open circuit") | |
pinMode(opins[j], 'opendrain'); | |
digitalWrite(opins[j], 1); | |
} | |
} | |
// For each row in the selected column... | |
for (j=0; j<ys; j++) { | |
// Condition all other rows to 0V | |
for (k=0; k<ys; k++) { | |
if (k==j) continue; // Skip selected row | |
// Set unselected rows as output 0V | |
pinMode(ipins[k], 'opendrain'); | |
} | |
// Read value (0.0 - 1.0) from selected row | |
res[j] = analogRead(ipins[j]); | |
} | |
// Return an array of all row values (0.0 - 1.0) in the column | |
return res; | |
} | |
var max = 0; | |
function upd_max (v) { | |
if (v > max) max = v; | |
return true; | |
} | |
function idx_val (v) { | |
return (" "+Math.round(100*v/max)).substr(-3); | |
} | |
function exec() { | |
var x, y, arr, res=[]; | |
for (x=0; x<xs; x++) { | |
res[x] = arr = readCol(x); | |
// arr.every(upd_max); | |
} | |
max = 1; | |
res.every(function (c) { | |
console.log(c.map(idx_val).join("\t")); | |
}); | |
console.log(' '); | |
} | |
function init() { | |
while(true) exec(); | |
//(exec, 100); | |
} | |
init(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment