Hello,
i want my Diggi Keyboard to constantly use the keys F1-F4, this is no problem for me to do.
But i want that the F9 key will be pressed after 10 minutes and this all in should be in loop, so i have 2 loops running at the same time with 2 diffrent events.
Could some help me please?
Yes, show your code.... See How to use the forum please.
#include "DigiKeyboard.h"
void setup() {
}
void loop() {
DigiKeyboard.sendKeyStroke(KEY_F1); //pressing F1-F4
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F2);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F3);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F4);
DigiKeyboard.delay(500);
}
void loopF9() {
DigiKeyboard.sendKeyStroke(KEY_F9); //using F9
DigiKeyboard.delay(600000); //delay of 10 minutes
}
this is my code, i know that 2 void loop() dont work.
I want the first loop to be sapmmed constan and the loopF9 to be used evrey 10 minutes.
See:
Demonstration code for several things at the same time
Using millis() for timing. A beginners guide
When delay() is running, nothing else can happen. You can use the trick from the example "BlinkWithoutDelay" to do things at a timed interval.
#include "DigiKeyboard.h"
void setup()
{
}
void loop()
{
static unsigned long startTime = 0;
DigiKeyboard.sendKeyStroke(KEY_F1); //pressing F1-F4
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F2);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F3);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F4);
DigiKeyboard.delay(500);
if (millis() - startTime >= 600000UL) //every 10 minutes
{
startTime += 600000UL;
DigiKeyboard.sendKeyStroke(KEY_F9); //using F9
}
}
johnwasser:
When delay() is running, nothing else can happen. You can use the trick from the example "BlinkWithoutDelay" to do things at a timed interval.#include "DigiKeyboard.h"
void setup()
{
}
void loop()
{
static unsigned long startTime = 0;
DigiKeyboard.sendKeyStroke(KEY_F1); //pressing F1-F4
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F2);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F3);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F4);
DigiKeyboard.delay(500);
if (millis() - startTime >= 600000UL) //every 10 minutes
{
startTime += 600000UL;
DigiKeyboard.sendKeyStroke(KEY_F9); //using F9
}
}
to change the time to use F9 i just have to change if (millis() - startTime >= 600000UL) and startTime += 600000UL;.
is this right so i want to happen evrey 5 minutes i change both 600000 values to 300000?
You changed the delay of F9, but the other F stuff is still blocking...
May1337:
Quote from: May1337 Fri Sep 21 2018 09:21:32 GMT-0400 (Eastern Daylight Time)to change the time to use F9 i just have to change if (millis() - startTime >= 600000UL) and startTime += 600000UL;.
is this right so i want to happen evrey 5 minutes i change both 600000 values to 300000?
Yes. If you think you might want to do this more than once you should declare a constant so you only have to change it in one place.
const unsigned long F9Interval = 10UL * 60UL * 1000UL; // 10 Minutes
void loop()
{
static unsigned long startTime = 0;
DigiKeyboard.sendKeyStroke(KEY_F1); //pressing F1-F4
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F2);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F3);
DigiKeyboard.delay(500);
DigiKeyboard.sendKeyStroke(KEY_F4);
DigiKeyboard.delay(500);
if (millis() - startTime >= F9Interval) // every interval
{
startTime += F9Interval;
DigiKeyboard.sendKeyStroke(KEY_F9); //using F9
}
}
What if i want to make more then 2 loop´s?
#include "DigiKeyboard.h"
void setup()
{
}
void loop()
{
static unsigned long startTime = 0;
DigiKeyboard.sendKeyStroke(KEY_F1); //pressing F1-F4
DigiKeyboard.delay(250);
DigiKeyboard.sendKeyStroke(KEY_F2);
DigiKeyboard.delay(250);
DigiKeyboard.sendKeyStroke(KEY_F3);
DigiKeyboard.delay(250);
DigiKeyboard.sendKeyStroke(KEY_F4);
DigiKeyboard.delay(250);
if (millis() - startTime >= 240000UL)
{
startTime += 240000UL;
DigiKeyboard.sendKeyStroke(KEY_F9); //using F9
}
if (millis() - startTime >= 1140000UL)
{
startTime += 1140000UL;
DigiKeyboard.sendKeyStroke(KEY_F12); //using F12
}
}
i would use the last const too but what if i want F9 evrey x seconds and F12 evrey y seconds?
Use a separate start time variable for each timer.
so should it work like in my code at the last post?
May1337:
so should it work like in my code at the last post?
No. Like I said: "Use a separate start time variable for each timer." If they share a start time, only the shortest interval will trigger and that reset the start time. The longer intervals will never trigger. That is why, like I said, "Use a separate start time variable for each timer."