Keyboard.press only one character in a range (Arduino Leonardo - HC-SR04)

Hello to everyone,

My code is very simple for a simple function, i need my Arduino Leonardo with a Ultrasonic Sensor (HC-SR04) works like a keyboard to send two letters (k,j).

I don’t understand how i can include a variable to press only one time the letters, the code continue to repeat the letter, in another way when there is a range of distance i need one time the letter when the subject is out of this range I need only one time the other letter.

This is my code

#define trigPin 6
#define echoPin 5

#include <Keyboard.h>



void setup()
{
  
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Keyboard.begin();
}
void loop()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance >= 130  || distance <= 2)
  {
    
    Keyboard.press(74);
    delay(100);
    Keyboard.releaseAll();
  }
  else
  {
    
    Keyboard.press(75);
    delay(100);
    Keyboard.releaseAll();
  }
  delay(500);
}

Hope you can help me out

Thanks

  if (distance >= 130  || distance <= 2)
  {
   
    Keyboard.press(74);
    delay(100);
    Keyboard.releaseAll();
  }
  else
  {
   
    Keyboard.press(75);
    delay(100);
    Keyboard.releaseAll();
  }

This will as you’ve discovered, send the same value over and over. What you need to do is compare the current distance to the previous distance, and send a keystroke only if there is a (significant) change.

That means, of course, that you need another variable to keep track of the previous distance, and update it with the current distance at the end of loop(). See the state change detection example for inspiration.

Thanks for this reply,

I understand that i need this new variable that update every time, but at the end of every loop() when the variable it is update i need to set the Keyboard.press and i still have the same problem.

Maybe i don't understand what you mean...

Maybe i don't understand what you mean...

I don't think that there is any maybe about it.

// Add a global variable
long prevDistance = 0;
long GetDistance()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  return distance;
}
void loop()
{
   long currDistance = GetDistance();
   if(currDistance != prevDistance)
   {
      // The distance change. Do something
   }
   prevDistance = currDistance;
}

Where the Do something comment is is where you decide what to do based on the current distance, since a change occurred.

If you want to do something only when a significant change occurs,

   if(abs(currDistance - prevDistance) > significantDistance)

Thanks for this reply,
I understood exactly in this way,
but with this method I continue to have the same result,

Keyboard.press(74);

continue to be pressed and continue to stamp it.

PaulS:
Where the Do something comment is is where you decide what to do based on the current distance, since a change occurred.

The problem is what i need to tell to the board to create a command that in a range of distance press Only One Time the key and if the distance remain in that range don't press anything.

The problem is what i need to tell to the board to create a command that in a range of distance press Only One Time the key and if the distance remain in that range don't press anything.

That is what my code does.

Ok,

so where is my mistake?

#define trigPin 6
#define echoPin 5

#include <Keyboard.h>


long prevDistance = 0;

void setup()
{
  
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Keyboard.begin();

  
}

long GetDistance()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  return distance;
}


void loop()
{
  long currDistance = GetDistance();
   if(currDistance != prevDistance & & currDistance >= 130  || currDistance <= 2)
   {
       Keyboard.press(74);
    delay(100);
    Keyboard.releaseAll();
   }
    
   prevDistance = currDistance;
  
}

It continue to press me the J

   if(currDistance != prevDistance & & currDistance >= 130  || currDistance <= 2)

You should NOT be using compound if statements if you do not understand them. & & is NOT the same as &&.

You should use parentheses to make the order of evaluation clear. What the compiler thinks the order is and what you think the order is may not be the same thing.

What action you take when there is a change should be decided in the body of the “is there a change” statement.

   if(currDistance != prevDistance)
   {
      if(currDistance >= 130  || currDistance <= 2)
      {
      }
   }