• After backing out and rebuilding up to including the onDown touch event and investigating how to detect an onUp untouch event, I ended up with a code containing the core elements shown below as listen() and read xy() methods (code uploaded as touch4.js). The read xy() method is not just for reading the coordinates onDown, but later also for possible dragging while touching (onTrack), and - last but not least - for 'untouch' (onUp). Since with every event time is provided in addition to the x-y coordinates, touch durations can be used to determine short, long, very long, and dobule touches or taps.

    The code incorporates the suggestions from @Gordon. The essentials in a nutshell:

    1. detect a touch by hardware event using setWatch() in listen() method- rational: no continuous polling; enter polling mode only while touching for tracking
    2. eliminate the setTimeout() for reading x and y in read xy() method - rational: execution of the JS code provides enough time for the pins to settle on output for powering plane one and on input for analog-reading of plane two.

      // touch4.js [1v70]
      
      function TOUCH(xn,xp,yn,yp,onDownCB) { // pins in example: C0,C1,C2,C3...
      this.xn = xn; // ...C0
      this.xp = xp; // ...C1
      this.yn = yn; // ...C2
      this.yp = yp; // ...C3
      this.onDownCB = onDownCB;
      this.x = this.x0 = this.x1 = -1;
      this.y = this.y0 = this.y1 = -1;
      this.t = this.t0 = this.t1 =  0;
      }
      
      var pt = TOUCH.prototype;
      
      pt.listen = function() {
      var _this = this;
      pinMode(this.xn,"input_pulldown");
      digitalRead(this.xp);
      digitalWrite([this.yn,this.yp],3);
      setWatch( function(){ 
      pinMode(_this.xn);
      _this.xy(_this._onDown);
      }, this.xp, {edge:rising, repeat:false} );
      };
      
      pt._onDown = function() {
      this.onDownCB(this, this.x0 = this.x, this.y0 = this.y, this.t0 = this.t);
      };
      
      pt.xy = function(callback) {
      this.t = new Date().getTime();
      digitalRead([this.yn,this.yp]);
      digitalWrite([this.xn,this.xp],2);
      this.x = (analogRead(this.yn)+analogRead(this.yn)­+analogRead(this.yn))/3;
      digitalRead([this.xn,this.xp]);
      digitalWrite([this.yn,this.yp],2);
      this.y = (analogRead(this.xn)+analogRead(this.xn)­+analogRead(this.xn))/3;
      if (callback) { callback.call(this); }
      };
      
      // -----
      
      var touchModule = 
      { connect: function(xn,xp,yn,yp,onDown) {
      var touch = new TOUCH(xn,xp,yn,yp,onDown); touch.listen();
      return touch;
      }
      };
      
      var cnt = -1, cnt2;
      touchModule.connect(C0,C1,C2,C3,function­(touch,x,y,t){
      cnt2 = 0; console.log(++cnt,cnt2,"onDown:",x,"-",y­,"@",t);
      setTimeout(function(){
      touch.xy(); 
      setTimeout(function(){
        console.log("untouched: ",touch.x,"-",touch.y,"@",touch.t);
        console.log("---");
        setTimeout(function(){ touch.listen(); },100);
      },100);
      },800);
      });
      
      

    This is the output for touching the four corners ( 240 (x) px horizontal x 320 (y) px vertical screen):

    1. top-left corner (x=0, y=0)
    2. top-right corner (x=239, y=0)
    3. bottom-left corner (x=0, y=319)
    4. bottom-right corner (x=239, y=319)

      >echo(0);
      =undefined
      0 0 onDown: 0.14363825945 - 0.09122860557 @ 239187.00858778625
      untouched:  0.45956104880 - 0.23576206098 @ 239992.94179389314
      ---
      1 0 onDown: 0.84319320464 - 0.09904122479 @ 243184.38072519082
      untouched:  0.45630579079 - 0.23633173113 @ 243990.34160305344
      ---
      2 0 onDown: 0.14290582640 - 0.85898120597 @ 246428.01812977099
      untouched:  0.28068462144 - 0.32902520281 @ 247233.97805343510
      ---
      3 0 onDown: 0.85588871086 - 0.85653976246 @ 249302.83683206106
      untouched:  0.27962666259 - 0.33260598662 @ 250108.79580152672
      ---
      > 
      

    The above data shows the trouble in the untouched stage: untouched values are in the range of touch min-max values. Elaborating on different items - even bringing in the reading of the xy from the old touch2.js code with the 1[ms] timeouts, which worked before for detecting the untouched state - did not help. Just having dropped the continuous polling and the timeouts for a faster and cycle/time-leaner execution, I did not want to go back and increase the timeouts to may-be have success.

    The analysis of the data - untouched values are influenced by the previously touched area - and the difference between touch2.js and touch4.js code - unintended output-clashes - made me think that there is not enough time to 'discharge' the previously powered plane sufficiently to be ready for reading.

    Adding very brief input with pull-down on the reading plane pin AFTER powering the powered plane provided the solution. Somehow, the lingering capacities between the planes in combination with the very sensitive, input-/sink-frugal ADCs lead to the 'misreadings'. Below the xy() method with the added lines to sink-away lingering capacities, and the new, related output data.

    pt.xy = function(callback) {
      this.t = new Date().getTime();
      digitalRead([this.yn,this.yp]);
      digitalWrite([this.xn,this.xp],2);
      pinMode(this.yn,"input_pulldown");     // <--- extra lines to sink-away
      pinMode(this.yn);                      // <--- ...lingering capacities 
      this.x = (analogRead(this.yn)+analogRead(this.yn)­+analogRead(this.yn))/3;
      digitalRead([this.xn,this.xp]);
      digitalWrite([this.yn,this.yp],2);
      pinMode(this.xn,"input_pulldown");     // <--- extra lines to sink-away
      pinMode(this.xn);                      // <--- ...lingering capacities 
      this.y = (analogRead(this.xn)+analogRead(this.xn)­+analogRead(this.xn))/3;
      if (callback) { callback.call(this); }
    };
    

    The new data show now untouched values that are comfortably outside of the range of the touched values.

    >echo(0);
    =undefined
    0 0 onDown: 0.15332265201 - 0.09342590473 @ 396399.69179389311
    untouched:  0.05224689097 - 0.03605198240 @ 397206.41984732821
    ---
    1 0 onDown: 0.85312174156 - 0.10058747234 @ 399303.29770992364
    untouched:  0.05485109737 - 0.03580783805 @ 400110.02385496185
    ---
    2 0 onDown: 0.13517458864 - 0.86378271152 @ 402053.27290076337
    untouched:  0.05843188118 - 0.03605198240 @ 402860.03339694655
    ---
    3 0 onDown: 0.84970372065 - 0.84156557564 @ 404042.44847328244
    untouched:  0.05875740698 - 0.03637750820 @ 404849.19465648854
    ---
    > 
    

    The pin-set sequence can now also be optimized towards the added lines. Looking at in what state the pins are left by the reading of xy(), I reduced listen() to just the pulling hight the second edge of the powered plane and call the xy() before the listen() at connect. But for some reason, this made the watch() to trip on a regular basis without a touch happening (and triggered xy() showed also an 'untouch'...). So I left the listen and optimized only the reading of xy(). Playing around while optimizing, I also noticed that keeping the pull-down had no adverse effect, so I eliminated its removal. (The complete code for the touch initialization and touch onDown event detection is uploaded as touch5.js file.)

    pt.xy = function(callback) {
      this.t = new Date().getTime();
      pinMode(this.yn,"input_pulldown");
      digitalRead(this.yp);
      digitalWrite([this.xn,this.xp],2);
      this.x = (analogRead(this.yn)+analogRead(this.yn)­+analogRead(this.yn))/3;   
      pinMode(this.xn,"input_pulldown");
      digitalRead(this.xp);
      digitalWrite([this.yn,this.yp],2);
      this.y = (analogRead(this.xn)+analogRead(this.xn)­+analogRead(this.xn))/3;
      if (callback) { callback.call(this); }
    };
    

    Next steps are now to bring back all the complete onDown, onTrack - track while touching and possibly dragging - and optional on onUp support.


    2 Attachments

About

Avatar for allObjects @allObjects started