Double Click for Analog Sensor with Threshold

I am trying to build a keyboard for MD patients. I have only five force sensors, each sensor encodes 4 letters (20 in total), which means I am missing 6 letters. I thought of using double-click, for example: one click= 'A', double-click='B'. I have found the following code: Code to detect single or double click - #13 by gerivega
However, when trying to change from DigitalRead to AnalogRead and add a threshold, the code is unable to work (type). I have also tried using the library OneButton but it has the same issue(digitalRead).

Does anyone have a suggestion on what to do?
Example of my current code for two sensors, where each sensor types four letters letter per click for each direction :

#include <Keyboard.h>
const int threshold = 800;
int up4 = 0;
int down4 = 0;
int right4 = 0;
int left4 = 0;

int up5 = 0;
int down5 = 0;
int right5 = 0;
int left5 = 0;

int yellow4= A0; // down
int white4 = A1;//left
int red4 = A2;//up
int blue4 = A3;//right 

int yellow5=A4;//down
int white5= A5;//left
int red5= A6;//up
int blue5 = A7;//right

void setup() 
{
  pinMode(red4, INPUT); 
  pinMode(blue4, INPUT);
  pinMode(white4, INPUT); 
  pinMode(yellow4, INPUT);

    pinMode(red5, INPUT); 
  pinMode(blue5, INPUT);
  pinMode(white5, INPUT); 
  pinMode(yellow5, INPUT);
  Serial.begin(9600);
  Keyboard.begin();
}

void loop()
{
  int up4 = analogRead(red4); 
  int right4 = analogRead(blue4);
  int down4 = analogRead(yellow4); 
  int left4 = analogRead(white4);

  int up5 = analogRead(red5); 
  int right5 = analogRead(blue5);
  int down5=analogRead(yellow5); 
  int left5 = analogRead(white5);

  if (right4>threshold & up4<threshold & down4<threshold & left4<threshold){
   Serial.println("right4");
   Keyboard.write('L');
   delay(300);
   Keyboard.releaseAll();
  }
    if (right5>threshold & up5<threshold & down5<threshold & left5<threshold){
   Serial.println("right5");
   Keyboard.write('Y');
   delay(300);
   Keyboard.releaseAll();
  }
  if (right4<threshold & up4>threshold & down4<threshold & left4<threshold){
   Serial.println("up4");
   Keyboard.write('M');
   delay(300);
   Keyboard.releaseAll();
  }
  if (right5<threshold & up5>threshold & down5<threshold & left5<threshold){
   Serial.println("up5");
   Keyboard.write('R');
   delay(300);
   Keyboard.releaseAll();
  }

   if (right4<threshold & up4<threshold & down4>threshold & left4<threshold){
    Serial.println("down4");
    Keyboard.write('N');
     delay(300);
     Keyboard.releaseAll();
  }
  if (right5<threshold & up5<threshold & down5>threshold & left5<threshold){
   Serial.println("down5");
   Keyboard.write('Z');
   delay(300);
   Keyboard.releaseAll();
  }
  if (right4<threshold & up4<threshold & down4<threshold & left4>threshold){
    Serial.println("left4");
    Keyboard.write('O');
     delay(300);
     Keyboard.releaseAll();
  }
      if (right5<threshold & up5<threshold & down5<threshold & left5>threshold){
   Serial.println("left5");
   Keyboard.write('P');
   delay(300);
   Keyboard.releaseAll();
  }

    
  
} 

You are aware that you can use analog inputs as digital inputs? Exception would be A6 and A7 on a 328P based board.

yes, I am aware. however, I am interested in the analog input features (since I want to change the threshold to improve the sensors' sensitivity)

& is for a binary AND, && is for a logical AND. And I would use some extra () to make sure that compiler does not give a different interpretation from what I expect. So it would become

if ((right4>threshold) && (up4<threshold) && (down4<threshold) && (left4<threshold))

Thank you :slight_smile:
I have attached my current code to give an idea of how to integrate this current code with the additions I am interested in.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.