24

Programs may, at times, have runtime errors. These are sometimes hard to find and can easily be missed. Is there any way to test the program before actually burning it onto the board ?

asheeshr
  • 927
  • 1
  • 10
  • 25
  • Might be related to http://arduino.stackexchange.com/q/84/25 – powtac Apr 10 '13 at 10:15
  • Also maybe related to http://arduino.stackexchange.com/questions/20/can-i-develop-with-the-arduino-using-an-ide-with-more-features?rq=1 –  Apr 10 '13 at 17:37
  • You may have the issue the error only occurs on a physical Arduino (I couldn't give an example, though). – Polar Apr 10 '13 at 23:13
  • @Polar In terms of program logic, I cant imagine how. Could you, maybe, post *any* example ? – asheeshr Apr 11 '13 at 12:56

3 Answers3

19

There are a few Arduino Simulator projects out there.

Perhaps one of the more mature ones is the Virtronics Simulator for Arduino, YouTube video here.

Virtronics Arduino Simulator

The Virtronics page linked above also lists a few other Arduino simulators, both free and paid.

Given the interest the Arduino evokes, there are likely to be many more such simulators out there, so no point trying to list them all in an answer here.

What is worth noting is that there is also an Arduino Simulator iPhone app: This is not a recommendation, not having seen it in operation yet.


On a side note:

The Arduino is in itself a prototyping / experimentation board. It is ideal for programming experimental code, debugging it, modifying and then re-flashing fresh code, pretty much as many times as one likes. If the code hangs, reset and reflash with any changes.

Therefore, the merit of using a simulator, which can never perfectly emulate the various real-world timing or other issues an application might face, is questionable.

If the cost of the Arduino is the concern, there are a couple of options open:

  • Inexpensive Arduino Nano clones off eBay - I have several Meduino Nano Enhancement boards, they work excellently, and are priced at under $10 including world-wide shipping: Meduino Nano Enhancement
  • Make your own Arduino - The Arduino site walks you through making one on a breadboard, which does not even need you to solder anything!
Anindo Ghosh
  • 50,188
  • 8
  • 103
  • 200
  • Thanks for sharing the link to the Virtronics Simulator! – powtac Apr 10 '13 at 10:16
  • Yes, it is much easier to debug and reflash. ATMega328 flash is rated at 10000 rewrite cycles, so even if you rewrite it 10 times every single day you have more than two years of fun before you :-) Quite a lot of fun for an hobbyist. If you're just a bit more into it, you can use newer boards to do the debugging and when they start to wear out (say after 5000 cycles?) you can use them for stable applications an buy a new one for debugging. – LorenzoDonati4Ukraine-OnStrike Jul 05 '14 at 21:48
8

You can find runtime errors if you can manually step through your program with the Arduino connected and debug (after downloading code to the Arduino). This is available in Visual Micro although it requires Visual Studio. You can set breakpoints, evaluate variables, and change variable values. You can also get visualization of of memory over time:

enter image description here

  • I haven't used it for profiling, and I've only used it briefly, but it integrates pretty well and runs fine for me. I really appreciate the ability to debug without resorting to Serial.print –  Apr 14 '13 at 20:48
  • Very neat. How mature is this project -- or, rather, has this IDE run smoothly in your experience? – boardbite Apr 14 '13 at 13:11
4

One way to do this would be to create a wrapper program for the actual code which simulates all the inputs and accepts outputs (thereby creating a feedback loop) as per the real environment. This would take variable amount of effort depending on the type of program, degree of testing and number of inputs.

Keep in mind that while writing the wrapper program, you should follow a black-box approach.

enter image description here

Otherwise, your outer code may not test the program as well as possible as keeping in mind the actual code while creating the test code may bias you to ignore boundary cases or problem areas (This has been observed to happen while doing White-Box Testing which is the alternative).

asheeshr
  • 927
  • 1
  • 10
  • 25
  • Do you know of any examples? Most Arduino code I have seen lacks proper unit tests even for the parts that do not require input/output. – Jakob Apr 10 '13 at 17:55
  • @Jakob I usually write my own code for the testing. It is practical for small projects. – asheeshr Apr 11 '13 at 05:38