Trying to make a glove with flex sensors that controls keyboard inputs on my pc

I am attempting to use an arduino leonardo to send keyboard inputs for gaming. For example, when I bend one of the flex sensors more than a certain limit, the arduino then uses the "Keyboard.press('w')" command to send the letter 'w' as an input. this has worked perfectly for 1 sensor, but when I try to read 2 at once, it doesn't recognize the second one and does nothing.

this is the code that i used

#include <Keyboard.h>

void setup()
{
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

  Keyboard.begin();
}

void loop()
{
  
  while (analogRead(A0) < 150){
    Keyboard.press('w');
    delay(10);
  }
  while (analogRead(A0) > 150) {
    Keyboard.release('w');
  }
  while (analogRead(A1) < 150) {
    Keyboard.press('e');
    delay(10);
  }
  while (analogRead(A1) > 150) {
    Keyboard.release('e');
  }
}

I then found out that you can not use both inputs at the same time and that they have to be delayed in order for it to read only one pin at a time, so my final question is: how do i implement this delay in the code?

you are using while loops

while (analogRead(A0) < 150){
    Keyboard.press('w');
    delay(10);
  }

you will be stuck forever in the above loop while A0 < 150
similar with the other loops

try reading the analogue inputs and using if statements, e.g. along the lines of

void loop()
{
  int a0=analogRead(A0);
  if (a0 < 150){
    Keyboard.press('w');
    delay(10);
  }
 else {
    Keyboard.release('w');
  }

similar for A1

1 Like

so what loop should i use?

i see, i will try that

it has worked perfectly, thank you for the reply!

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