# Can you start a sketch with Arduino button?

**URL:** <https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [June 12, 2022, 12:49am UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494 "2022-06-12T00:49:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![friedadedoncker](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@friedadedoncker](https://discourse.processing.org/u/friedadedoncker)\
**Post date:** [June 12, 2022, 12:49am UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/1 "2022-06-12T00:49:45Z")

</div>

Hi!

I recently purchased [this](https://be.farnell.com/multicomp/ns106-m/keyswitch-spst-2pos-mom-rdm/dp/1735382?CMP=e-email-sys-orderack-GLB) 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!

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [June 12, 2022, 1:40am UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/2 "2022-06-12T01:40:23Z")

</div>

Hi

Here is example you can use one switch or 4

> **[Processing and Arduino, drawing rectangles](https://forum.arduino.cc/t/processing-and-arduino-drawing-rectangles/486765)**
>
> For a new course I’m attending I have to create a connection between Processing and Arduino. I want that if a certain switch is ON that a rectangle is drawn in Processing. For this I have 3 switches and one button. The three switches represent a...

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 12, 2022, 1:41am UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/3 "2022-06-12T01:41:23Z")

</div>

Hello @friedadedoncker,

Take a look at the tutorial here:

> **[Electronics](https://processing.org/tutorials/electronics/)**
>
> Control physical media with Processing, Arduino, and Wiring.

`:)`

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [June 12, 2022, 9:38am UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/4 "2022-06-12T09:38:19Z")

</div>

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](https://shop.pimoroni.com/products/pro-micro-5v-16mhz?variant=7487693313) (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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 12, 2022, 2:20pm UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/5 "2022-06-12T14:20:59Z")

</div>

Also…

Reference:  
[https://processing.org/reference/launch\_.html](https://processing.org/reference/launch_.html)

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

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 12, 2022, 5:33pm UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/6 "2022-06-12T17:33:10Z")

</div>

**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](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?

---

<div class="post-metadata">

**Author:** ![friedadedoncker](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@friedadedoncker](https://discourse.processing.org/u/friedadedoncker)\
**Post date:** [June 12, 2022, 5:33pm UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/7 "2022-06-12T17:33:23Z")

</div>

Thanks! This is what I was looking for.

Didn’t know the launch function excisted.

---

<div class="post-metadata">

**Author:** ![friedadedoncker](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@friedadedoncker](https://discourse.processing.org/u/friedadedoncker)\
**Post date:** [June 12, 2022, 5:34pm UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/8 "2022-06-12T17:34:54Z")

</div>

Thanks will try this as well.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 12, 2022, 6:50pm UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/9 "2022-06-12T18:50:42Z")

</div>

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

```auto
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…

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 12, 2022, 7:35pm UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/10 "2022-06-12T19:35:30Z")

</div>

> launch(“notepad.exe”);

That’s not a Processing sketch.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 13, 2022, 12:07am UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/12 "2022-06-13T00:07:46Z")

</div>

Hello,

Updates…

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

```auto
// 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.

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 13, 2022, 12:15am UTC](https://discourse.processing.org/t/can-you-start-a-sketch-with-arduino-button/37494/13 "2022-06-13T00:15:48Z")

</div>

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.
