Here's sample code to demonstrate my problem:
#include <DigiKeyboard.h>
void setup() {
// put your setup code here, to run once:
delay(5000);
DigiKeyboard.print("TESTING TESTING");
pinMode(1,OUTPUT);
digitalWrite(1,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
}
My problem is that I can't get Digikeyboard.print() or other Digikeyboard commands (e.g. sendKeyStroke()) to do anything while they are located inside the setup function. The led command after the print command executes properly.
The print command will work if I place it inside the loop function. However, I want to only execute this code once.
Any assistance would be appreciated, and I would be happy to provide more information if necessary.
EDIT: I worked around this by using a if-expression inside the loop function to only run the code once. However, I am still interested in learning why my original code doesn't work.
1 Answer 1
The (hard to find) documentation on the DigiKeyboard example: http://digistump.com/wiki/digispark/tutorials/digikeyboard
States:
It is essential that DigiKeyboard.update() is called regularly (the beginning of the loop is good) to maintain USB communication. Using DigiKeyboard.delay() will automatically spam DigiKeyboard.update() calls during the delay period. If your loop code is fast just using DigiKeyboard.delay() will be sufficient.
Without looking deeper into it, I suspect that your initial call to DigiKeyboard.print()
fills a buffer and DigiKeyboard
needs subsequent repeated calls in order to empty the buffer.
Does the problem still exist if you put DigiKeyboard.update()
as the only statement in the loop()
function?
-
Problem still persists. However, after reading your cited part from the documentation, I changed
delay(5000)
toDigiKeyboard.delay(5000)
, and the code works properly.hur– hur2017年09月25日 17:28:54 +00:00Commented Sep 25, 2017 at 17:28 -
In your initial try, did you put the
DigiKeyboard.update()
in your run-once loop, or outside it?jose can u c– jose can u c2017年09月25日 17:30:22 +00:00Commented Sep 25, 2017 at 17:30 -
For your suggestion, I used the code written in the original post, and placed
DigiKeyboard.update()
as the only statement in theloop()
functionhur– hur2017年09月25日 17:32:12 +00:00Commented Sep 25, 2017 at 17:32 -
DigiKeyboard.delay()
will allow the USB interface to enumerate, which is essential before you can communicate in any form over the interface.Majenko– Majenko2017年09月25日 18:14:51 +00:00Commented Sep 25, 2017 at 18:14