Press any key every time delay

void setup() {
Keyboard.begin(); //Start the keyboard simulation
//

}

void loop() {

Look at this, i want that "B" be press every 3 seconds, but it gets pressed every 8 seconds, because "B" has to wait 5 seconds that "A" be pressed.

I want that "A" be press every 5 seconds and "B" every 3 seconds, separated.

So, get rid of the delays.
Look at the blink without delay example for clues.

What has this got to do with installation of the IDE?

I see that you posted in the wrong category and didn't post your entire sketch, or post with code tags. Please read here to learn how to fix those issues with your post:

What is the greatest common factor of 3 and 5? It's 1. So make your delay one second and

Pseudocode

repeat
{
if A >= 5
  {
  A = 0
  send key A
  }
if B >= 3
  {
  B = 0
  send key B
  }
  delay 1 second
}

Spice and flavour to suit...

1 Like

@royalarchers

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

Thank you Perry,

i edit and post the entire sketch.

I cant get rid of the delays because i need them.

I need some key be press every couting time, but i need those keys respect those times.

But without code tags. That is why you need to actually read what you are asked to read.

I solved that for you.

You didn't understand.
You need to get rid of not delays as "time intervals", but the delay() operators.
This is a blocking statement, it stops all other processes in the controller. As long as you use it in the program, you will not be able to get independent execution of two sequences

Um, not quite. OP can only get into trouble if additional tasks must run more than once per second, with my pseudocode.

1 Like

This is not a general solution, but a particular one :slight_smile:

It's someones first day at the keyboard. Let's let them go one step at a time.

1 Like
void loop() {

  //attack
  static unsigned long lastATime = 0;
  if (millis() - lastATime >= 5000)
  {
    lastATime += 5000;
    Keyboard.press('A');
    Keyboard.release('A');
  }

  //next target
  static unsigned long lastBTime = 0;
  if (millis() - lastBTime >= 3000)
  {
    lastBTime += 3000;
    Keyboard.press('B');
    Keyboard.release('B');
  }

}

Might want to increment A & B each loop... ?

Man, you gave it away. I wasn't going to reveal that until the end. :slight_smile: Notice I also didn't release the keys.

Do you ever wonder, hey, what did the OP finally do? ? :wink:

Kind of like, goodbye, and thanks for all the fish.

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