(Way off topic - sorry) Programming practice for Arduino

Sorry folks.

I am working on some Adruino code. I am even worse with it than with JavaScript.

What is also hurting me is that I can't as easily test it without uploading the code.

I am needing to practice loops.
I think 2 levels deep.

Would doing it in JS (in a function node) help me?
I know nothing beats doing it with what you need, but where I am: I still can't get my head around the loops.

(Hangs head. I used to be able to do this 30 years ago. But that was in BASIC....)

Look at C++ While Loop

Good examples

1 Like


I know more of JS than I do of C++.

But I get it that Arduino is more C++ than JS.
Actually: it is C++ pretty much isn't it?
Ok, it isn't. Or it would be called C++, I guess.

But thanks.

ITMT, I think I have it worked out.
Not exactly, but I have the structure now for the loops.
Woo Hoo - I think is to be said. :wink:

Just for the sake of it....

This is the code I made:

let a = ["a","b","c","d","e","f","g","h","i"];
let b = ["","","","",""];
let pointer = 0;
let pointer2 = 0;

let start = 1;
let size = 5;       //  size of display
let length = 9;     //  size of message (a)

node.warn("A is " + a);
node.warn("A pointer " + a[pointer]);

while (pointer2 < length)
    b[0] = a[pointer2];

    node.warn("B is " + b);

    while (pointer < length)
        b[ size - pointer ] = b[ size - pointer - 1 ];
        pointer = pointer + 1;
    pointer = 0;

    node.warn("Result " + b);
    pointer2 = pointer2 + 1;

I'm not going to say it is perfect, but I am seeing interesting outputs in the debug window.

I recently switched to micropython from arduino/c++ and couldn't be happier. This depends on the hardware you are using. Writing code on the microcontroller and executing directly is fantastic, no more compiling/uploading, not to mention string/array handling horror in c++

Possibly better to use nested for loops incrementing array indices up to the lengths of the arrays. The code is virtually the same as in js.

Thanks both.

I seem destined to suffer pain.

The problem I had is/was the loop structure.
(Or so I thought)

The JS seems to work fine. I set the names the same as I did for the Arduino and copied the structure to the IDE.

Uploaded and..... Doesn't work.

I added some Serial.print("message"); lines, and even they aren't happening.


Add some more to find out what is happening.

This could either be that the section of code isn't being executed or that you haven't set up the serial connection correctly. If you're getting outputs from other parts of the code, then @Colin's suggestion of adding a few more prints is a good idea. It's pretty much the same procedure as debugging JS by adding node.warn calls to track execution.

I've been doing a lot of Arduino/ESP8266/ESP32 development over the last few years, so hopefully can help if you're having problems. C++ isn't really that much different from JS, and you should pick it up quite quickly.

If is C++ though it has some limitations and a few hangovers from C.

You might be better off opening a new project with blink and then playing with some loops and serial prints just to get yourself up to speed. Then review your current code and see where you went wrong.

You might also try the latest beta version of the Arduino IDE which is rather more informative about issues.

I've tried that a few times over the years but kept getting frustrated by the fact that the framework never seems to get updated and is somewhat fragmented and then more frustrated by missing libraries for key hardware.

Of course, I use ESP8266 & ESP32 devices for my microprocessors, I don't use any actual Arduino's eny more. That means that I've swapped almost exclusively to using ESPhome which does all of the boring bits for you without code - just configuration - but still lets you use code where you really need it. It already covers a vast array of common sensors and controls and devices.

1 Like

Me too! Sometimes it makes sense to move on using newer stuff instead of sticking to what you eventually have left behind in the drawer - recycle more

Thanks everyone.

I've been getting help from the arduino people too. Seems they cracked it.

It is so .... frustrating (?) how when I am shown the answer it is so .... easy.
As I said there: things in retrospect as usually easy.

Oh well. I'm moving on now to the next step in this exciting voyage.

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