Trying to use screen taps for input

I have an android app that reads a magnetometer over USB and displays the x,y,z data as PVector. It’s ok but what I want is to be able to calibrate the magnetometer data. What I tried is to divide the screen into thirds. Tap the top third of the screen and the X axis data is grabbed. Middle third, Y. And lower third grabs the Z axis data.

To control it I have a counter, so if the counter equals 0, that grabs the zero end of the X axis and increments the counter by 1. If I tap that area again, it grabs the X axis but on the other 180 degree end of the axis, and increments the counter by 1 again.

I’m saving the data as a String and printing it on the screen using text(). The problem is it ignores the counter. It recognizes which area of the screen I tap, but when I try to test for the counter value, it makes no difference and still updates the first axis end. I’m only including the first set of variables that’s supposed to be grabbed when the top third is tapped. :crazy_face: Thanks for any help with this.
Here’s the code snippet:

        if(mousePressed){

            if (mouseX < width/2 && mouseY < height/3 && count1 == 0) {  //1/3

                textBoxXPlus_X_0 = Double.toString(q[6]);
                textBoxXPlus_Y_0 = Double.toString(q[7]);
                textBoxXPlus_Z_0 = Double.toString(q[8]);
           //     System.out.println(textBoxXPlus_X_0 + " " + textBoxXPlus_Y_0 + " " + textBoxXPlus_Z_0);

                count1 = 1;
            }
            if (mouseX < width/2 && mouseY < height/3 && count1 == 1) {  //1/3

                textBoxXPlus_X_180 = Double.toString(q[6]);
                textBoxXPlus_Y_180 = Double.toString(q[7]);
                textBoxXPlus_Z_180 = Double.toString(q[8]);
                count1 = 2;
            }

            if (mouseX < width/2 && mouseY < height/3 && count1 == 2) {

                textBoxXMinus_X_0 = Double.toString(q[6]);
                textBoxXMinus_Y_0 = Double.toString(q[7]);
                textBoxXMinus_Z_0 = Double.toString(q[8]);
                count1 = 3;
            }

            if (mouseX < width/2 && mouseY < height/3 && count1 == 3) {
                textBoxXMinus_X_180 = Double.toString(q[6]);
                textBoxXMinus_Y_180 = Double.toString(q[7]);
                textBoxXMinus_Z_180 = Double.toString(q[8]);

                count1 = 0;

            }

Just to explain further, q[6],q[7],q[8] is the x,y,z data from the magnetometer. This snippet is trying to get the values of q[] for four points along the X axis. Two points are on one end of the X axis and 180 degrees from those are the other two points.

I think I got something that will work. Instead of testing for where the tap is && what’s the count, I test for the location of the tap and increment the count, then I separately test for the count and update the variable.

I did a quick check by printing the count to the screen and it increments. I only want to update one variable set per tap.

I thought I solved it and I was wrong. The problem now is that the taps are inconsistent. Sometimes I’ll tap and it sees it as two taps, sometimes more, but rarely only a single tap. Other than that, when it increments the count, a variable gets updated just not consistently.

So, I thought maybe ketai can do it. That only confused me more. I have no idea how to get ketai gesture to work. I see it has a 0nTap(float x, float y) method but it doesnt do anything unless I call it somehow. What I tried was

OnTap(mouseX, mouseY);

That was fun. I watched the counter cycle from 0 through 12 then reset, repeat without any taps at all. All my variables updated without any taps at all.

So basically, I want to tap the screen and update one variable at a time and I don’t know how.

Thanks for any suggestions, ideas, a kick in the head, help.

Could you write a code to test that doesn’t need the hardware?
(with vars declared)

I think you mean without the USB hardware code? It will take some time but I will do that.

I removed the code that was trying to use ketai gestures and tested with just mousePressed() incrementing the counter. If I tap staccato, it increments by 1 tap at a time.

So maybe the solution is to control the duration of the tap. As long as the tap is faster than some limit and slower than some limit?

Without code it’s difficult to test.
But if you think that for some reason double clicks occur on your device, you could try something like:

boolean tab_allowed;

void setup() {
  size(500, 500);
  background(255);
  tab_allowed = true;
}

void draw() { 
}

void mousePressed () {
  if(tab_allowed) {
    tab_allowed = false;
    println("tab");
  }
}

void mouseReleased () {
  tab_allowed = true;
}

Thanks, noel! The mouseReleased() did the trick. Just use a normal tap and the count increments by only 1 and eventually after enough taps resets to 0.

Here’s the basic code:

/*
  3DScatter v1.1 - June 2012
  Produce, in Processing, a 3D scatter graph from serial data
  Copyright (c) 2011 Hon Bo Xuan <honboxuan at gmail dot com>
  All rights reserved.

  This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import android.view.MotionEvent;

import java.util.ArrayList;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PVector;
import Jama.*;
import ketai.ui.*;
import static processing.core.PApplet.concat;

public class Scatter extends PApplet {
  
    PFont font;
    float[] q = new float[10];
    int count = 0;
    int Count = 0;
    int[] vals;
    PVector[] vecs = new PVector[100000];
    int[] Vals;
    PVector[] Vecs = new PVector[100000];
    int x_max = 0, x_min = 0, y_max = 0, y_min = 0, z_max = 0, z_min = 0;
    int x_total = 0, y_total = 0, z_total = 0;
    int x_mean = 0, y_mean = 0, z_mean = 0;
    int x_length = 0, y_length = 0, z_length = 0;
    float length_mean = 0, x_gain = 1, y_gain = 1, z_gain = 1;
    float zoom = 0.5f;
    KetaiGesture gesture;
    boolean tap;
    float x;
    float y;
    
       public  void setup() {

        // frame.addMouseWheelListener(new MouseWheelInput());

        vals = new int[3];
        Vals = new int[3];
        gesture = new KetaiGesture(this);

    }
    public  void draw() {
      
         findCentroid();
        findGain();
        background(0);


        translate(width/2, height/2);

        String Counts = "Count: " + count;
        text(Counts, -width/2 + 20, height/2 -280);
        /////////////////   text(gain, -width/2 + 20, height/2 - 20); //Print before flipping Y (mag Z axis) and rotation        
        // String centre = x_mean + ", " + y_mean + ", " + z_mean;
        String centre = vals[0] + ", " + vals[1] + ", " + vals[2];
        //////////////////  textFont(font, 20);
        fill(255, 255, 255);//   text(centre, 0.5 * x_mean + 20, -0.5 * z_mean, 0.5 * y_mean); //Print before flipping Y (mag Z axis)
        text(centre, 0.5f * vals[0] + 20, -0.5f * vals[2], 0.5f * vals[1]); //Print before flipping Y (mag Z axis)
        scale(zoom, -zoom, zoom); //Flip Y (mag Z axis)

        noFill();
        stroke(100);
        strokeWeight(1);
        //////////////////////     box(1200);

        stroke(0, 255, 255);
        strokeWeight(8);

        point(vals[0], vals[2], vals[1]); //Centroid

        stroke(255, 0, 0);
        strokeWeight(2);
        line (300, 0, 0, 275, 0, 25); //Arrow head
        line (300, 0, 0, 275, 0, -25);
        line (320, -20, 0, 340, 20, 0); //Axis label
        line (320, 20, 0, 340, -20, 0);
        line(-300, 0, 0, 300, 0, 0); //Mag X Red

        stroke(0, 0, 255);
        strokeWeight(2);
        line (0, 0, 300, 25, 0, 275); //Arrow head
        line (0, 0, 300, -25, 0, 275);
        line (0, 20, 320, 0, 0, 330); //Axis label
        line (0, 0, 330, 0, 20, 340);
        line (0, 0, 330, 0, -20, 330);
        line(0, 0, -300, 0, 0, 300); //Mag Y Blue

        stroke(0, 255, 0);
        strokeWeight(2);
        line (0, 300, 0, 25, 275, 0); //Arrow head
        line (0, 300, 0, -25, 275, 0);
        line (-10, 350, 0, 10, 350, 0); //Axis label
        line (10, 350, 0, -10, 320, 0);
        line (-10, 320, 0, 10, 320, 0);
        line(0, -300, 0, 0, 300, 0); //Mag Z Green
        vals[1] = Math.round(q[6]/2);
        vals[0] = Math.round(q[7]/2);
        vals[2] = Math.round(q[8]/2);
        Vals[1] = Math.round(q[6]);
        Vals[0] = Math.round(q[7]);
        Vals[2] = Math.round(q[8]);

        if(mousePressed) {
            if (mouseX < width / 2 && mouseY < height / 3) {  //1/3

            }
        }



        if (count == 1){
         //   count = 0;

        }
        if (count == 2){
          
        }
        if (count == 3){
        }
        if (count == 4){
          count = 0;
        }

        vecs[Count] = new PVector(vals[0], vals[2], vals[1]);
        Vecs[Count] = new PVector(Vals[0], Vals[2], Vals[1]);

        Count++;

        for (int i = 0; i < Count; i++) {

            PVector v = vecs[i];


            stroke(255);
            strokeWeight(3);
            point(v.x, v.y, v.z);

            PVector V = Vecs[i];
            stroke(0, 255, 0);
            strokeWeight(3);
            point(V.x, V.y, V.z);

        }

    }
    
        public void mousePressed () {
        if(tap) {
            tap = false;
            println("tab");
           // count++;
        }
    }
   public void mouseReleased () {
        tap = true;
        count++;
    }
    
    
    void findCentroid() { //Fixed to find centroid, all points equally weighted
        if( Count > 1) {
            x_total += vals[0]; //With 10,000 sample limit, should not overflow
            x_mean = x_total / Count;
            y_total += vals[1];
            y_mean = y_total / Count;
            z_total += vals[2];
            z_mean = z_total / Count;
        }
    }
    void findGain() { //Still simplistic
        //Find lengths between max and min of x, y and z
        //Find mean and gain of each axis
        if( Count > 1) {
            if (vals[0] > x_max)
                x_max = vals[0];
            if (vals[0] < x_min)
                x_min = vals[0];
            if (vals[1] > y_max)
                y_max = vals[1];
            if (vals[1] < y_min)
                y_min = vals[1];
            if (vals[2] > z_max)
                z_max = vals[2];
            if (vals[2] < z_min)
                z_min = vals[2];
        }
        x_length = x_max - x_min;
        y_length = y_max - y_min;
        z_length = z_max - z_min;
        length_mean = (x_length + y_length + z_length) / 3;
        x_gain = x_length / length_mean;
        y_gain = y_length / length_mean;
        z_gain = z_length / length_mean;
    }

//**************************************************************************************************
//******************************************************************************************************

    public void settings() {  fullScreen(P3D); }

    static public void main(String[] passedArgs) {
        String[] appletArgs = new String[] { "ScatterPlot" };
        if (passedArgs != null) {
            PApplet.main(concat(appletArgs, passedArgs));
        } else {
            PApplet.main(appletArgs);
        }


    }


}
     
1 Like