• Looks great - it could make a really handy module...

    About pins, what you've got looks good. Personally, I'd change the code to use the multiple digitalRead/Write functionality - and just for safety I'd make sure you switch the pins to inputs before you set the other pins to outputs...

    I'm not sure if using digitalRead (instead of analogRead) will work for setting state, but it's worth a try.

    Also it's worth trying without the setTimeouts - as the code takes a while to execute anyway, you'll probably find you don't need them.

    var readXY = function(callback) {
      var xy = {};
      digitalRead([C2,C3]);
      digitalWrite([C1,C0],1);
      xy.x  = analogRead(C2);
      xy.x2 = analogRead(C3);
      digitalRead([C0,C1]);
      digitalWrite([C3,C2],1);
      xy.y  = analogRead(C0);
      xy.y2 = analogRead(C1);
      callback(xy);
    };
    

    or with multiple reads:

    var readXY = function(callback) {
      var xy = {};
      digitalRead([C2,C3]);
      digitalWrite([C1,C0],1);
      xy.x  = (analogRead(C2)+analogRead(C2))/2;
      xy.x2 = (analogRead(C3)+analogRead(C3))/2;
      digitalRead([C0,C1]);
      digitalWrite([C3,C2],1);
      xy.y  = (analogRead(C0)+analogRead(C0))/2;
      xy.y2 = (analogRead(C1)+analogRead(C1))/2;
      callback(xy);
    };
    

    One neat addition would be to detect taps without polling, and then to only poll when you know a finger is pressed. I'm not 100% sure if this works, but what about:

    function listen() {
      pinMode(C0,"input_pulldown");
      digitalRead(C1);
      digitalWrite([C2,C3],3);
      setWatch(function() {
        pinMode(C0); // reset pin state
        // start polling... when finger is released, stop polling and call 'listen' again
      }, C2, {edge:rising, repeat:false});
    

    That'd effectively stop using CPU cycles when a finger wasn't pressed. For other applications where you want long battery life it'd be handy too.

  • ...have a hard time to detect when to listen again. Btw, in the listen(), setWatch() has to be on C1 and not on C2, because latter has just been set to high and therefore will never rise... ;).

    The issue I have is after 'untouch' to read values different from when touched. With suggested readXY() I get values within the touched range. Interestingly, I did not get such values with my initial code (touch2.js). After 'fixing' the listen(), I used a hybrid of suggested listen() and my initially working readXY() code, but with the same result: for untouched I get values within the range of touched.

    So, I have to do a bit backing out....

About

Avatar for allObjects @allObjects started