3

Coming from JS, I find it very difficult to program Arduino in C++ (not even knowing which version or what features are supported), being bound to the loop and delay, not being able to (easily) use anonymous functions in parameters or async code.

I know JS isn't natively supported by Arduino, but I know languages can be transpiled.

I have seen many solutions, so just to pick few:
JohnnyFive J5, which runs via cable on a host PC,
NectarJS with their arduino-uno target,
or andrei-markeev/ts2c which converts ES3 to C89.

Each of them come with their own limitations and it's very difficult to find the right without prior experience with IoT. Did anyone walk this path already and found a viable solution?

Qwerty
  • 139
  • 5

3 Answers3

2

Even if such a transpiler exists (which I don‘t know) you will still have to deal with the loop structure and the fact that you should avoid using delays. On a microcontroller you have to forget about things like multithreading and even dynamic memory allocation should be used carefully. Most of those things that make a programmer‘s life easy on bigger systems are usually paid with ram or cpu power, but on a microcontroller with some kB RAM there are only few resources to pay with.

So my recommendation is to learn C++ instead of looking for ways to convert code from another language, which will probably introduce its own problems. You‘ll soon find yourself one layer below and there are exciting things that higher languages hide from you.

Sim Son
  • 1,878
  • 13
  • 21
0

Why don't you just start with a microcontroller that supports Javascript?

I find the Adafruit Circuit Playground Express quite fun to work with. You can still connect sensors and other hardware, the same as you can do with Arduino. It also has a bunch of sensors built-in.

You can try it out right now without owning a hardware board at adafruit.makecode.com. Click the Javascript tab on the top of the page.

Using a JS>Arduino transpiler will probably cause a lot of headache, since it often won't do what you expect it to do.

Kokodoko
  • 161
  • 1
  • 6
0

andrei-markeev/ts2c JavaScript/TypeScript to C transpiler looks promising.

I'll probably give it a go tonight.

According to the github page:

Produces readable C89 code from JS/TS code.

For example, this JavaScript:

console.log("Hello world!");

transpiles to the following C code:

#include <stdio.h>

int main() { printf("Hello world!\n"); return 0; }

src: https://github.com/andrei-markeev/ts2c

Shanimal
  • 101
  • 1