Potentiometer as a keyboard (Leonardo)

I'm having a problem with programming on Arduino Leonardo to create some keyboard functions. I am using a potentiometer and according to the value read by the analog port, I need a button to be pressed, BUT ONLY ONCE.

I tried to use the commands

while (1){} and for ( ; ; ){} but it didn't work.

Here is an example of the script I am making. If anyone can help me, it will be really cool. Thanks in advance.

const int minADCRange = 0;
const int maxADCRange = 1023;
int result;

void setup() {
 // put your setup code here, to run once:

Keyboard.begin();
}

void loop() {
 // put your main code here, to run repeatedly:

int potentiometer = analogRead(A0);

result = map(potentiometer, minADCRange, maxADCRange, 0, 2);

switch (result){
 case 0: Keyboard.press('A');
 break;
 case 1:Keyboard.press('B');
 break;
 case 2: Keyboard.press('C');
 break;
 default:
 break;

 Keyboard.end();
}
delay( 10 );
}

Thks

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

————

I need a button to be pressed, BUT ONLY ONCE.

if you want to do something only once, put the code in the setup.

I’d say that var1 and var2 are poorly named, calling them minADCRange and maxADCRange for example would help with code readability.

Note as well that you will get ‘2’ only if you are at maxADCRange (that’s the way the map() function works) whereas you’ll get 0 on the first half of the range and 1 on the other half minus the very last value

J-M-L:
Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

————if you want to do something only once, put the code in the setup.

I'd say that var1 and var2 are poorly named, calling them minADCRange and maxADCRange for example would help with code readability.

Note as well that you will get '2' only if you are at maxADCRange (that's the way the map() function works) whereas you'll get 0 on the first half of the range and 1 on the other half minus the very last value

I think now the way I posted is correct. Thank you for the tips.

But I still have the problem I mentioned, if I put this function in the "setup" the code will run only once and it will no longer be available, however when I put it in the loop, it keeps repeating the key I need.

What I would like is that when the potentiometer reaches a certain range of values, a button is activated and released only once. For example, when the potentiometer is between 0 and 400, press the "A" key and release, without repeating. But what happens is "AAAAAAAAAAAAAAAA ..." infinitely.

I'm not able to interrupt the loop, using the break function. I've tried using the while and the for and it still didn't work.

Thank you.

So it’s not only once seems it is every time the value changes you want to send the key, right?

If so just memorize the previous range and until the current range is the same as the previous, don’t send the key again

See the StateChangeDetection-example for how to react on a changing value.

Declare a variable for storing the previous value of "result"

Then in each iteration of loop(), compare "result" with "previousResult"

  • if different, do Keyboard.press() (and Keyboard.release()), and save current "result" as "previous result"

Note that Keyboard.press() must be followed by a Keyboard.release() - just like normal typing on a keyboard. (some devices might require a small delay berfore Keyboard.release())

When using Keyboard.end(), keyboard emulation is stopped, and Keyboard.begin() must be used to start it again.

Sorry for my ignorance, but in this case are you saying to use the “if” command instead of using the “switch / case” command?

I’m putting an image with the idea of this project and what I would like the arduino to do.

If you could show me how it would look in the code or how I can implement it in the initial code that I already did, it would be of great help if you don’t ask for too much.

thanks in advance.

If or switch, it does not matter. You just need to remember the previous state.

The question is not if you are asking for too much or not - this forum is for learning.

Explore the State Change Detection example and give it a try, declare a variable that can take 3 states, call it previous state. Declare another one, currentState in which you calculate what’s the state of the button and compare if there has been a change.

No, you should add "if" to see if "result" has changed, and only press keys if it has.

Something like this:

int previousResult;                // Declare a variable for storing the previous value of "result" 
:

loop() {
  :
  int potentiometer = analogRead(A0);
  result = map(potentiometer, minADCRange, maxADCRange, 0, 2);

  if (result != previousResult) {  // Check if "result" has changed. If not, skip keypressess

    switch (result){
      case 0: Keyboard.press('A');
      break;
    :
    }   //End of "switch"

    previousResult = result;       // Save current "result" for later
  
  }                                // End of "if"
  :
}                                  // End of "loop"

You should also add Keyboard.release(). Otherwise the keys will remain pressed and autorepeated like on a normal keyboard.

And remove Keyboard.end() even if it never will be reached and probably removed by the optimizer.