If you want to run the Arduino sketch on your desktop, you just have to implement the Arduino core library for your PC.
It may not be as bad as it sounds. If your program makes only minimal use of the Arduino core, you may implement only the parts you really need. For example, this partial implementation is enough to run your program:
#include <cstdint>
#include <iostream>
#include <unistd.h> // for usleep()
using std::cout;
using std::endl;
#define INPUT_PULLUP 2
void pinMode(uint8_t, uint8_t) {}
uint8_t digitalRead(uint8_t) { return 0; }
void delay(int t) { usleep(t * 1000UL); }
struct {
void begin(int) {}
template<typename T> void print(T x) { cout << x; }
template<typename T> void println(T x) { cout << x << endl; }
} Serial;
int main()
{
void setup();
void loop();
setup();
for (;;) loop();
}
I tested this on Linux, by simply appending your program right below
this. If you are using Windows, you will have to replace the usleep()
function with the appropriate Windows call (and <unistd.h> with the
appropriate header). The serial stream goes to the standard output. You
may redirect it to the USB serial port if you wish (trivial to do in
Linux, I assume it should be doable on Windows). You may also want to
replace digitalRead() with something that simulates a more realistic
user interaction.
Keep in mind that the sizes of the basic data types can be different on
the Arduino and on the PC. On an AVR-based Arduinos, int and pointers
are 16 bits while long is 32 bits. This is an “IP16” data model. An
ARM-based Arduino would typically be ILP32, whereas a 64-bit PC is LP64,
unless it runs Windows, in which case it's LLP64. This can make a
difference, especially if some of your calculations are prone to
overflow with the smaller data models.
Edit: I recently stumbled upon NCORE, a native core for Arduino. It may be a good fit for what you are trying to do. From the page description:
The native core allows you to compile and run Arduino sketches on the PC, generally with no modification. It provides native versions of standard Arduino functions, and a command-line interpreter to give inputs to your sketch that would normally come from the hardware itself.