Having Trouble with using "Keyboard.write()" command with Arduino DUE

Hello everyone I am having difficulty in making sure that I follow what is covered in the warning below.

"WARNING: When you use the Keyboard.write() command, the Arduino takes over your keyboard! Make sure you have control before you use the command. A pushbutton to toggle the keyboard control state is effective."

In my project I wish to automatically send a Keystroke to a Connected Computer’s Space Bar
that will allow software on the computer to change when sent after the Laser Beam has been interrupted.

I have been successful in setting up and coding a LED an output to come on when the Laser Beam is interrupted.

I thought it would be as easy as to replace the code line that turns on the LED (digitalWrite(2,HIGH); //turn led ON) to "Keyboard.write(32); //Sends a Keystroke to a Connected Compter's Space Bar ASCII value 32"

I am not sure on how to set up physically and modify my code to incorporate a "pushbutton to toggle the keyboard control state is effective".

I would appreciate any assistance.

I have attached my code.

LASERBEAMCONTROLkeyboardspacebar.ino (1.4 KB)

Hi Delta_G,
Thank you for your reply to my post.
The Arduino micro-controller platform is not my usual micro-controller platform that I use and I am still getting used to its anomalies.

I understand what you mean by wiring up and using a push-button including the use of an "if" statement.

The Project requires a Laser Beam to activate a sensor and the interruption of the Laser Beam would send a keystroke, not a push-button to send a keystroke.

I thought I had the "if" statement covered by:

if(sensorValue < 700) //setting a threshold value (if the light signal level is less than 700)

And by using the Keyboard.write(32) would send:

Keyboard.write(32); //Sends a Keystroke to a Connected Compter's Space Bar ASCII value 32

To Send a Keystroke.

What I really do not understand is how I am to ensure that I am following the "Warning" as below:

"WARNING: When you use the Keyboard.write() command, the Arduino takes over your keyboard! Make sure you have control before you use the command. A pushbutton to toggle the keyboard control state is effective."

I have seen a lot of information on sketches running a set-up using a push-button that will connect pin 2 to ground and a 10k resistor.

I am not sure if this is what I have to do to negate any problems to regain control of my keyboard when I do not need my project to control my keyboard.

Also I forgot to mention I am using a Mac Book and its keyboard to send a keystroke to from the Arduino DUE.

Kind Regards
Spirocontroller

Spirocontroller:
I am not sure if this is what I have to do to negate any problems to regain control of my keyboard when I do not need my project to control my keyboard.

The reason for the warning is that if there is a bug in your code that causes keystrokes to be sent uncontrollably over and over it can be difficult to upload a new sketch to the Arduino because as soon as you plug it in it starts sending keystrokes. There is a way around this explained in the reference but it's not a pleasant experience for beginners to encounter. One alternative to a button would be to add a delay in setup() before any keystrokes are sent long enough for you to easily upload a new sketch if there is a problem.

Many thanks for your advice Pert.

I tried to add a delay in the setup(), Ikept on getting error messages when tried to verify the code.
There must be something I was missing.

Had a good think about it, would placing a long enough delay command at the end of the "void loop()" code be good enough to give me time to upload a new sketch if there is a problem.

I understand that by placing a long delay at the end of the loop may be a problem, if my project requires an immediate return to the beginning of the code.

At this stage I can live with a long delay before restarting the loop.

Again thank you for your advice.

Cheers

Spirocontroller:
I tried to add a delay in the setup(), Ikept on getting error messages when tried to verify the code.
There must be something I was missing.

Do a Tools > Auto Format on your updated sketch that's giving the error messages and then post it here using code tags(</> button on the toolbar).

You really only need the delay while developing your code. Once you're sure everything is working correctly you can comment it out. Of course developing your code is when having a long delay in startup() is the most annoying so it may be worth the extra effort of adding a temporary button if you're doing a lot of edit/upload cycles. You don't need a real button and certainly no resistor is required. Just use pinMode(n, INPUT_PULLUP) and a jumper wire from an unused pin to ground.

Again many thanks for your incredible patients with me.

You have been incredible in filling in the missing bits from Arduino’s References and Tutorials.

I more than support the notion of Open Source Platforms where they are made open to all share, including Arduino. If we don’t, and lock resources down with patents and so on, we will lock down the resources and they do not grow for the better.

I finally understand the situation now with the issues I am facing and I have enough information from you to get on with it from here.

Your Fantastic!

My profile presents me as a “Newbie”, this is unfortunate because I am not a “Newbie” to life and the Electrical/Electronic Industry. (I am a “Newbie to forums and Arduino, not all Microcontrollers and forms of control)

I would like to indulge and share with you an open source project that I am involved in, a number schools each building a Life Size 3D printed Robot that uses voice control and cameras in its eyes to mimic human movement.

The ages of the students range from 10years to 14years and in one of the schools it is an all girl school.

Have a look at the You Tube “Inmoov Amazing First 3D printed robot Open Source” it uses two Adruino Boards (Uno R3 and Mega 2560)

Cheers
Spirocontroller.