Can you start a sketch with Arduino button?

Hi!

I recently purchased this keyswitch.

I was wondering if there’s any way to run a sketch (with this switch). What code would I need for this?
Something like: if … == 0 once, run file?

Thanks!

Hi

Here is example you can use one switch or 4

Hello @friedadedoncker,

Take a look at the tutorial here:

:)

Hi @friedadedoncker, There’s lots of information on how the Arduino can alter the execution of the sketch once it’s running. Starting it without a cooperating program running already, is something else. Some types of Arduino can emulate a keyboard. I have Sparkfun Pro Micro (sadly price has gone up since I bought mine). I think the Leonardo will do, or any with the ATmega32U4 chip, and I think any with the ‘U’ in the name.

Emulating a keyboard you can send e.g. ‘windows-key r myName.exe enter’ and the program will run. It’s not absolutely reliable, there’s a small chance something else will take the focus while you are doing that. Some keyboards have a button for e.g. media player. I guess you could find how that works and define your own key to run your own program.

1 Like

Also…

Reference:
https://processing.org/reference/launch_.html

Example:
https://discourse.processing.org/t/cant-open-bat-file/29299/4

:)

Re: launch() reference

There recently was a question here about using launch() for a Processing sketch. The poster could not get it to work and I had trouble also: https://discourse.processing.org/t/launch-function-does-not-work/37459/3

I wound up using Runtime.getRuntime().exec(); which did launch a Processing sketch. By any chance do you have a demo using launch() for this purpose?

1 Like

Thanks! This is what I was looking for.

Didn’t know the launch function excisted.

Thanks will try this as well.

It works for this simple example in Processing 3.5.4 and Processing 4.0b8:

void setup() 
  {
  size(200, 200);
  }

void draw() 
  {
  // draw() must be present for mousePressed() to work
  }

void mousePressed() 
  {
  println("NotePad");
  // Any one of these work on W10:
  //launch("C:/Windows/System32/notepad.exe");
  //launch("C:\\Windows\\System32\\notepad.exe");
  launch("notepad.exe"); // It is set in my path in Windows
  }

Updates in a post below…

:)

1 Like

launch(“notepad.exe”);

That’s not a Processing sketch.

Hello,

Updates…

I did some testing on Windows 10 Pro and added comments to sketch below:

// This will launch a Processing 4.0b8 sketch when run on Processing 3.5.4 or 4.0b8:
//launch("D:/Program_Portable/processing-4.0b8/processing-java --sketch=/Users/me/Documents/Processing4b8/Grid_diagonals_1_0_2 --run");

// This will launch a Processing 3.5.4 sketch when run on Processing 3.5.4 or 4.0b8:
//launch("D:/Program_Portable/processing-3.5.4/processing-java --sketch=/Users/me/Documents/Processing4b8/Grid_diagonals_1_0_2 --run");

// Only Processing 4.0b8 is in my Windows path!

// Below is of interest:
// if run from Processing 3.5.4 it opens a 3.5.4 sketch
// if run from Processing 4.0b8 it opens a 4.0b8 sketch

// Processing 4.0b8 folder added to Windows path:
launch("processing-java --sketch=/Users/me/Documents/Processing4b8/Grid_diagonals_1_0_2 --run");

You will have to change paths and sketch names to run on your system.

:)

1 Like

Thanks. For some reason that won’t work on a Mac (v. 11.6 Big Sur). Documentation for launch() states that there might be operating system inconsistencies. Runtime.getRuntime.exec(String) will work on a Mac.

1 Like