Setup() code Not running.

Hello,

I have Arduino Due,and Want to run my Code in setup loop but it fails.
My intention is to run the code only once.But the purpose fails in the setup loop.

I want to open "Spotlight Search" on my mac by pressing cmd+space.

I fail to execute this task in setup() loop and but in loop() it works and never stops.
I tried to run an infinite loop after my code in loop so it executes one But it doesn't work.

All the cases gets compiled but only one case actually works and that is the code in loop.

#include "Keyboard.h"
#include "Mouse.h"

char ctrlKey = KEY_LEFT_GUI;
int i=1;
void setup() {
  // put your setup code here, to run once:
  Keyboard.begin();
  // Keyboard.press(KEY_LEFT_GUI);
    //  Keyboard.press(32);
         //   delay(100);               //deosnt work
     // Keyboard.releaseAll();
     //Start();          //doesnt work


}
void loop()
{


  Keyboard.press(ctrlKey);
    Keyboard.press(32);
        delay(100);
     Keyboard.releaseAll();
     Keyboard.print("Terminal");     //works but doesnt stops
   
     while(true)
     {
           //doesnt work
     }
  
      
}

/*void Start()
{
  Keyboard.press(KEY_LEFT_GUI);
      Keyboard.press(32);
            delay(100);
      Keyboard.releaseAll();

}*/

AIM: To make code cmd+space execute only once.

That boggles my mind.

Anything in setup() should run exactly once.
Anything in loop() should run repeatedly after setup() has run.

     while(true)
     {
           //doesnt work
     }

should cause loop() to get stuck and never run again.

Please change something (say, "Terminal" to "Terminal 2") to verify that you actually compiled and loaded this code.

Also, in the future,

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

Good Luck!

The autoformatting (step 1) is missing, making the code a little harder to read.

Because the while loop doesn't do anything, it's probably optimized away by the compiler. Put this statement:

   i++;

in the while statement block and see if it goes into an infinite loop.

vaj4088:
That boggles my mind.

Anything in setup() should run exactly once.
Anything in loop() should run repeatedly after setup() has run.

     while(true)

{
          //doesnt work
    }




should cause loop() to get stuck and never run again.

Please change something (say, "Terminal" to "Terminal 2") to verify that you actually compiled and loaded this code.


Also, in the future,

To post code and/or error messages:

1) Use CTRL-T in the Arduino IDE to autoformat your code.
2) Paste the autoformatted code between code tags (the </> button)
so that we can easily see and deal with your code.
3) Paste the complete error message between code tags (the </> button)
so that we can easily see and deal with your messages.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

Good Luck!

The autoformatting (step 1) is missing, making the code a little harder to read.

Sir I tried But it keeps on looping
It completely ignores the setup() code

Hi vidit,

What happens if you put a println in your setup code? Do you see that?

Pat.

Hello Sir,

The code doesn't get executed in Setup() but gets executed in Loop().

#include "Keyboard.h"
#include "Mouse.h"

char ctrlKey = KEY_LEFT_GUI;
int i = 1;
void setup() {
  // put your setup code here, to run once:
  Keyboard.begin();
  Keyboard.println("HEllO");



}
void loop()
{

  Keyboard.println("HEllO");
  // Keyboard.press(ctrlKey);
  //  Keyboard.press(32);

  //eyboard.releaseAll();
  //while (true)
  //{
  //}
  // Keyboard.print("Terminal");
  //delay(100);
  //Keyboard.write(KEY_RETURN);



}

void Start()
{
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press(32);
  delay(100);
  Keyboard.releaseAll();

}

Hi vidit,

Can you supply 2 files? One that works and one that doesn't? It is difficult to tell which code you posted with the comments.

I'd like to recreate the exact conditions.

Also, Start isn't called. I assume it is not part of this problem.

Pat

Also, try this from the Arduino tutorial and let me know what happens:

void setup() {
 //start keyboard communication
 Keyboard.begin();
 //send a keystroke
 Keyboard.print("Hello!");
 //end keyboard communication
 Keyboard.end();
}

void loop() {
//do nothing
}

econjack:
Because the while loop doesn't do anything, it's probably optimized away by the compiler.

Nonsense. The compiler cannot change "stay here forever" to "ignore this".

The USB enumeration between the PC and Due could take a couple of seconds, thus, if you add a delay after the keyboard initialization, your code should work.

I just tried the following example and it worked OK.

void setup() {
  Keyboard.begin();
  delay(2000);
  Keyboard.println("Hello!");
}

void loop() {
}

Regards,

-p

Palliser:
The USB enumeration between the PC and Due could take a couple of seconds, thus, if you add a delay after the keyboard initialization, your code should work.

I just tried the following example and it worked OK.

void setup() {

Keyboard.begin();
  delay(2000);
  Keyboard.println("Hello!");
}

void loop() {
}




Regards,

-p

Thank you so much for you help.
It worked.