1

I'm working on a little Arduino game project that uses neopixel strips as a display. Uploading every code change to the Arduino and testing it on the LEDs is slow and drains hardware lifetime.

Usually I test as much I can using the Serial Monitor, but I can't get it to clear from code which I learned is only possible e.g. when using a terminal. I did that, but it comes with other issues.

Is there a better way to test code on the dev machine instead of the Arduino? Is there a best practice to have a Serial Monitor (or anything else) act like a display that I can actually refresh/clear? Serial.println() empty lines to scroll the content is not very practical.

  • I personally always use either a terminal emulator (screen) or cat on a terminal if it's only for logging. I find a terminal quite handy for that sort of work. – Edgar Bonet Dec 14 '17 at 9:35
  • UECIDE's terminal is far superior. – Majenko Dec 14 '17 at 9:57
  • I was using "Serial Tools" for Mac and the screen command inside the terminal. Both work and both have the annoying issue that somehow I can't upload changes as the IDE says that the serial port is currently in use. So far I only managed to solve that by disconnecting and reconnecting again. Any advice for that? – Esshahn Dec 14 '17 at 10:00
  • Just quit screen before uploading. – Edgar Bonet Dec 15 '17 at 8:02
  • just get a $2 AVR that you don't care about "burning out". I often code new chunks in short JS first w/something like jsfiddle/codepen, using as close to C as possible (eg x.substring(0,x.indexOf(y)) instead of x.split(y)[0]. Once it works, i drop it into the sketch and add the types. Once it compiles, you've got a pretty good shot at it working as expected with minimal debugging. of course, you can run C w/o uploading too, but i've not found a decent "emulator", if that's the question. – dandavis Dec 15 '17 at 12:53
1

You can unit test your code by creating stubs of the functions you are calling, like replacing a couple of includes with their dumb counterparts. My friend Jan Beayens did something very valuable on that front and he is also the main author of a good replacement for the Arduino IDE when you start needing some more advanced capabilities: Sloeber is the product name and it also contains an improved version of the serial monitor with clear and scroll lock capabilities.

Go check it out.

Disclaimer : I'm a contributor of the project

  • 2
    Warning: It's a plugin for Eclipse. – user31481 Dec 16 '17 at 9:16
  • Why are you issuing a warning? Do you believe there's anything wrong with Eclipse? It is OSS and works perfectly on Linux, which are two factors you should appreciate... BTW, Sloeber also exists as a product, but that it is still dependent on Eclipse CDT. – Roberto Lo Giacco Dec 17 '17 at 10:10
  • 1
    Eclipse is an horrible IDE. I will not touch Eclipse with a 10 meters pole. – user31481 Dec 17 '17 at 10:20
  • I respect your opinion, but I completely disagree: Eclipse is my IDE of choice. – Roberto Lo Giacco Dec 17 '17 at 10:26
1

As you've probably noticed, trying to run tests over the serial port is frustrating. Worse, the relatively large delays associated with writing serial data can affect the accuracy of your measurements.

To cope with this, I wrote my own unit testing framework for Arduino, which lets you fully mock (simulate) the hardware so that you can verify the operation of the software.

There are some examples of how my library accomplishes that, in the project README. Here is one for pins:

unittest(pin_history)
{
  GodmodeState* state = GODMODE();
  int myPin = 3;
  state->reset();            // pin will start LOW
  digitalWrite(myPin, HIGH);
  digitalWrite(myPin, LOW);
  digitalWrite(myPin, LOW);
  digitalWrite(myPin, HIGH);
  digitalWrite(myPin, HIGH);

  assertEqual(6, state->digitalPin[1].size());
  bool expected[6] = {LOW, HIGH, LOW, LOW, HIGH, HIGH};
  bool actual[6];

  // convert pin history to an array, and verify expected results
  int numMoved = state->digitalPin[myPin].toArray(actual, 6);
  assertEqual(6, numMoved);

  // verify each element
  for (int i = 0; i < 6; ++i) {
    assertEqual(expected[i], actual[i]);
  }
}

You may also like the example for mocking Serial port data, although I probably need to improve that documentation.

The beauty of all this is that I was able to develop this framework and all these tests without ever plugging an Arduino board into my computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.