Leonardo only works after upload via IDE

i have a simple mouse mover program that works fine after an upload.
i can then exit the IDE and it works fine.
but if i just plug it in without the ide, the leonardo doesnt do anything.

do i need to set up a port or something?

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


int buttonstate;
int buttonPin=8;

String btn2str = "start";

char cc;
int len;
int onetime=0;
int jj=0;

void setup() { // initialize the buttons' inputs

  // initialize mouse control:
  delay(3000);
  Mouse.begin();
  Keyboard.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buttonPin, INPUT);
  delay(200);
  flash5times(1000);

  flash5times(100); 
  Keyboard.println(btn2str);
  
}

void loop() {

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  buttonstate = digitalRead(buttonPin);
  
  delay(100);

  // move mouse every 5 min
  
  if (jj>50) {
    flash5times(300);
    Mouse.move(100, 100);
    delay(50);
    Mouse.move(-99,-99);   

    delay(50); 
    Mouse.move(100, 100);
    delay(50);
    Mouse.move(-101,-101);
    jj=0;
  }
  
  delay(500);
  jj++;
 
}

Is that everything?
(I'm asking because - it seems there's stuff missing.)

Below simple sketch prints the start message to the active window 3 seconds after powering up the Leonardo.

Maybe move your delay(3000) to after the begin() calls.

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

String btn2str = "start";

const uint8_t safetyPin = A0;


void setup()
{
  pinMode(safetyPin, INPUT_PULLUP);

  Mouse.begin();
  Keyboard.begin();
  delay(3000);

  while (digitalRead(safetyPin) == HIGH)
  {
  }

  Keyboard.println(btn2str);
}

void loop()
{
  // put your main code here, to run repeatedly:

}

You will need to short A0 to GND; this is just a safety measure that I recommend so mistakes in the code can be suppressed by removing the short.

If you replace above loop() by below, the mouse will also make 5 moves

void loop()
{

  static uint8_t counter;

  while (counter < 5)
  {
    Mouse.move(10, 10);
    counter++;
    delay(1000);
  }
}

Conclusion:
Nothing special needed.

thats it, it is just a simulated mouse move even X minutes to keep my screen from locking every time i walk away.
the security lockout is set to 5 minutes on my laptop so anytime I get up, i have to login again (even tho i work from home by myself)
the buttons previously actually typed my passwords but i have stripped that out for now.
i am brushing off an old program from 6 years ago which worked great but maybe there is something about the new board givng me problems.

i tried this code and grounded A0. i still have the same issue.
i upload from the IDE, it runs fine. the mouse moves.
but if i dont use the IDE and just plug in or restart the leonardo, it does nothing.
just the blue light, no yellow and no mouse moves.

I copied and tried to Compile (Post No.3) → Fail:
'flash5times' was not declared in this scope

I won't ask any more questions.

sorry, the fcn was missing from the bottom.

here is all the code.

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


int buttonstate;
int buttonPin=8;

String btn2str = "start";

char cc;
int len;
int onetime=0;
int jj=0;

void setup() { // initialize the buttons' inputs

  // initialize mouse control:
  delay(1000);
  Mouse.begin();
  Keyboard.begin();

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buttonPin, INPUT);
  delay(200);
  flash5times(1000);

  flash5times(100); 
  Keyboard.println(btn2str);
  
}

void loop() {

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  buttonstate = digitalRead(buttonPin);
  
  delay(1000);

  // move mouse every 3 min   
  
  if (jj>180) {
    flash5times(300);
    Mouse.move(100, 100);
    delay(50);
    Mouse.move(-99,-99);   
    flash5times(100);
    delay(50); 
    Mouse.move(100, 100);
    delay(50);
    Mouse.move(-101,-101);
    jj=0;
  }
  
  delay(10);
  jj++;
 
}


void flash5times(int delayms){
  int ii;

  for (ii=0; ii<5; ii++) {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(delayms);
        digitalWrite(LED_BUILTIN, LOW);
        delay(delayms);
  }
      
} 

that is the board i am using.

not sure if this is relevant but each time i start the IDE, have to select the board/port, usually shown as COM3 or COM7

image

I think you will find that keyboard.h waits for the serial connection to be available. So in fact you are testing that that function does, in fact, work that way.

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