Customize Visual Studio Using Arduino Macros

by Visual Micro 19. October 2012 13:48

The Arduino plugin for Visual Studio automatically creates a Visual Studio macro for every Arduino command. This enables us to add Arduino commands to menus and tool bars. It also allows us to assign shortbut keys to all of the Arduino commands.

Clicking "Customize>Add Command>Addins" on any Visual Studio tool bar or within the menus provides a list of "Visual.Micro" commands as shown below

The following image shows a button that, when clicked, quickly selects a specific Arduino board. In this case a custom board is being selected hence the strange name.

Warning: On rare occassions Visual Studio becomes confused and which causes visual micro menus and tool bar controls to disappear. The documented solution for this problem is to use the visual studio resetaddin command. Running this command will remove any customizations you have made that reference visual.micro macros. I am not currently sure if it is possible to backup your customizations of the visual studio ide. The safest way to work might be to create a visual studio macro that creates the arduino buttons, shortcuts and menus based on visual micro micros. Doing so allows you to re-run your macro and re-create your customizations at any time in the future.

A guide to installing new non-arduino hardware or custom arduino cores

by Visual Micro 22. September 2012 17:35

A guide to installing new non-arduino hardware or custom arduino cores

...


Arduino allows new non-arduino hardware to be installed to the arduino ex folder or to the arduino sketchbook folder.

Adding new hardware cores and libraries to your "Sketch Book Folder" is probably the best way to work because it means that you don't have to re-install the new hardware each time you upgrade to a new Arduino IDE.

For the purposes of this demonstration we are using "avr-netino". The image below shows a successful compile of the 4 example projects in the avr-netino-19Jan2012.zip on google code.

I am currently running Visual Studio and arduino 1.0.1, but I think it will work the same for arduino 1.0. 

These are the steps I made to install this new hardware and associated files.

Overview

The zip contains 3 folders. Hardware, Libraries and Examples

Installation

1) Copy the contents of the "Hardware" folder to your "[Sketch Book Folder]\Hardware" folder. 
Nb: Create the Hardware folder if it does not exist

2) Copy the contents of the "Libraries" folder to "[Sketch Book Folder]\Libraries"
Nb: Create the Libraries folder if it does not exist.

3) Copy the "avr-netino" folder from the "examples" in the zip to the sketch book folder.

The location of your sketch book folder can be found in the Arduino ide "File>Preferences" window or by selecting "File>Open Arduino File" in Visual Studio. My sketch book folder is "My Documents\Arduino"

Re-start Visual Studio and you should see the new hardware on the boards list. Both Visual Studio and Arduino should compile correctly with this configuration.

Automate the Arduino IDE for Visual Studio Interface

by Visual Micro 19. September 2012 15:22

We can build our own tool bar layouts or add shortcuts to frequently used Arduino commands to the Visual Studio user interface.

How To

Right click any Visual Studio tool strip and select "Customize" which will on the bottom of the right mouse menu.

When the Customize window opens (see below), click keyboard to assign shortcut keys. Click the  "Commands" tab to add commands to menus and tool bars.

When adding a command or shortcut the Visual Micro commands will be displayed under the "Addins" section 

Below you can see the possibility to add Arduino commands such as board selection to the "File" menu.

...

Warning: On rare occassions Visual Studio becomes confused and which causes visual micro menus and tool bar controls to disappear. The documented solution for this problem is to use the visual studio resetaddin command. Running this command will remove any customizations you have made that reference visual.micro macros. I am not currently sure if it is possible to backup your customizations of the visual studio ide. The safest way to work might be to create a visual studio macro that creates the arduino buttons, shortcuts and menus based on visual micro micros. Doing so allows you to re-run your macro and re-create your customizations at any time in the future.

Arduino - Memory Usage and Disassembly Reporting

by Visual Micro 24. August 2012 14:29

The August 2012 release of the Arduino plugin for Visual Studio provides useful options to display memory usage and program disassembly reports as part of the Arduino compile process. The disassembly view might not be useful for arduino beginners but the memory usage report might be espcially useful for beginners.

Arduino processors have a relatively small amount of "running" data memory which is often quickly consumed by new users adding their own debug messages to arduino programs. Arduino debug messages are often serial messages such as:- Serial.println("Hello World, I am here at this place in my code!")

Long serial text messages like the one above will quickly fill up the arduino memory which might only be 2048 bytes (characters). Serial messages are just one example of memory consumption. If the Arduino attempts to overflow the available memory it will crash (freeze). A crash is both difficult to debug and also can be quite disasterous so best avoided!

It is true that the arduino has other small memory areas where data could be stored. The other memory areas are for another article and do not remove the need to monitor memory usage.

Arduino Memory Usage Report

The memory uage report is enabled using the visual studio project properties window as shown below. The properties window is displayed by selecting the project node in the viual studio solution explorer and pressing F4 (or "View>Properties Window")

How to enable Arduino memory and disassembly reporting

The arduino memory usage report is automatically displayed after each compile in the "Micro Build" output window shown below. The report shows memory usage after compilation.

NB: The memory usage report does not show the dynamic memory that might be allocated by an arduino program after it has started running on an arduino chip. Dynamic memory can be reported in visual studio using the visual micro debug upgrade or by adding code to your arduino program. See this reference for more info.

Arduino Program Disassembly Report

The disassembly view takes arduino learning to the next level or provides useful information for experienced arduino programers. The disassembly view is automatically displayed in the "Micro Disassembly" output window after each compile (see top picture of how to enable disassembly reports). Below you can see an exampe of the disassembly of the diydrones ArduPilot program

References

Microsoft Visual Studio Profressional is available for 3 years free (if you are an individual and not a professional web designer or working for a web design company)

The Visual Micro plugin for Visual Studio can be downloaded for free from www.visualmicro.com

Arduino - Learning by Example using Visual Studio and APM

by Visual Micro 24. August 2012 13:02

Selecting an Arduino development tool that provides code assistance is a big help for new arduino users and also to users who are new to a particular arduino program. Features that can be very useful are:- help, examples, code completion, error correction and intellisense.

Help and Examples

Arduino provides a number of examples to help us understand the basics of micro-controller development. Many arduino libraries, created by the community, are also provided with examples.

The "File" menu in the Arduino IDE provides access to all of these examples. In the Arduino IDE we click an example to open it as a new sketch project. In the Arduino IDE, that editing and saving the example will be replaced with modified code. In Visual Studio we get to choose if we would like to create a copy of an example or use the original. We also get a few others options described later on in this page.

Examples are a useful facility and are often overlooked by new users. Examples help us understand the core components of any arduino project. By understanding the core components such as sensors and servos we are able to then create or understand larger Arduino programs.

A good "example" being the ArduPilotMega (APM) drone project. This project has resulted in a few massive but highly crafted arduino programs such as ArduPilot and ArduCopter. The APM hardware involves sensors such as 3 axis:- giro, accelerometer, compass and a gps. The hardware can also drive servos or electronic speed controllers (esc) for rc cars, planes, copters and boards. The heart of the APM circuit board is a standard Arduino micro-processor with some additional sensors ++.

Micro explorer showing apm examples and error correction

At this point I should add that there are other drone projects that use a variety of other arduino based solutions, I am not recomending which is the best I am simply using APM as an example. A reason for using APM as an example is that the project is so incredibly well written, it is an enourmous project that has been re-worked almost to perfection (from an arduino programming point of view!). The code is well formatted, the program is very flexible and can optionally include a large number of different types of sensors (and options). 

A great benfit for new APM users are the examples provided by the diydrones.com community. Every variation of every sensor supported by diydrones.com programs written for APM is available as an example. This makes it possible to write small simple experimental programs or use the examples as they are. These small programs are a great wayt to understand how each sensor, servo, motor, esc, etc. works.

The Visual Studio Arduino plugin provides the micro explorer tool  (shown above) includes these examples but also other reference and tutorials.  The explorer allows arduino examples to be opened, new arduino projects to created from examples and/or to simply open individual example sources for inspection (or copy/paste).

The explorer provides the same refererence examples that are available in the arduino ide with the addition of advanced references for the AVR microcontroller and gcc compiler. The latter being useful for more advanced users. A custom google search facility fixed to arduino.cc, arduino wiki, arduino forum and visualmicro.com provides a "www search" without leaving the Visual Studio development tool.

Arduino Code Completion and Intellisense

In the picture below you can see the micro explorer tool providing a drill down into the APM/Arduino example sources. The image also includes source code with an inteltional error to demonstrate Arduino code completion and error correction in Visual Studio.

Pressing the '.' (full stop) automatically displays a list of available members of the object called g in the ArduPilot project. Pressing F12 will goto the definition of an item. SHIFT+F12 will find all references to the currently selected code. All these functions and more are on right click menus and the menus at the top of visual studio.

How to explorer an arduino project using Visual Studio intellisense

References

For Arduino development, if you do not have a copy of Microsoft Visual Studio then grab a copy of Atmel Studio which is based on Visual Studio 2010 and is free

The Visual Micro plugin for Visual Studio can be downloaded for free from www.visualmicro.com

 

Arduino Debugger Extensibility Performance Considerations

by Visual Micro 7. August 2012 14:00

Aug 2012: First draft - The Visual Micro Extension Framework

Visualizations provide a simple way to build powerful fully featured graphical plugins. It is possible to code both efficient and inefficient visualizations, the latter should be void!

Unfortuately all this power and flexiblity means that it is possible to create unwieldy and slow plugins that consume fast amounts of cpu power. This document provides a place to develop useful tips and suggestions that will help ensure the debugger continues to run at an efficient speed.

The Visual Micro interface provides a number of standard events that should be used to perform a little work as possible. For example in the OnDebugInit event is called once each time a debug session starts. The event should be used to initialise as much as possibly ensuring that a timer is used when possibly to prevent blocking.

The OnDebugExpressionsChanged event is called every time a breakpoint is hit. It is a high volume message and should be used to perform as little work as possible. Off-loading heavy processing to a regulated timer using a flag is highly recommended.

This OnDebugExpressionsChanged event also procides access to the dynamic xml properties from the MicroExtensions.xml config file. Looking up xml poperties is relatively heavy on cpu load as opposed to using class variables to store property settings. For this reason it is recommended that the OnDebugExpressionsChanged event is used to copy regularly used xml config properties into class variables. This will enable functions that are called regularly during a debug session to refer to the class properties and NOT the xml config properties.

When developing .NET visualizations (UserControls and Forms) testing using the slowest possible pc is highly recommended. A good target to aim for seems to be no more than a 3% increase in cpu load on a 1.8Ghz intel processor.

You can easily test the load your extension causes by adding a "Pause" checkbox to your control or form. Stop responding to the OnDebugExpressionValuesChanged event when the checkbox is true. Start a debug session and add a breakpoint that your visualization is watching for intoto the loop().  This causes a huge amount of debug messages to be sent to the pc. Clikcing the Pause checkbox should clearly show in windows task manager the additional cpu load.

The Visual Micro Extensibility Framework

by Visual Micro 7. August 2012 11:57

Aug 2012: First draft, - About The Visual Micro Extension Framework

Debugger visualizations are controls and/or windows written in any .NET language that enable us to graphically display remote debug data from a micro-controller such as an arduino. This means that any open source .net instrument/control project that can be found on the internet can be used as a visualization. The micro extension framework is a set of easy to implement inheritable classes that enable an application to interact with the arduino debugger and also with visual studio. Visualizations are loaded with full trust and have access to a computer in the same way that other visual studio addins can access a computer.

The Visual Micro inheritance system is optional, but provides an easy way to manage a debug state server. Below is an example of a >NET form constructor requesting notification of 3 visual micro debugger events. (OnDebugInit, OnDebugStateChanged, OnDataExpressionValuesChanged)

public SketchDebuggerExample1Window()
{
InitializeComponent(); //standard vs component init

 

  • this.OnDebugInit += onDebugInit; //fires when debug is started before connect to micro
  • this.OnDebugStateChanged += onDebugStateChanged; //fires whenever a debug state change occurrs
  • this.OnDataExpressionValuesChanged += onExpressionValuesChanged; //fires when a watched expression breakpoint is hit

 

}

Event sigantures

  • void onDebugInit(object sender, DebugInitEventArgs e)
  • void onExpressionValuesChanged(object sender, ExpressionValuesChangedArgs e)
  • void onDebugStateChanged(object sender, DebugStateChangedArgs e)

The Visual Micro debug upgrade is supplied with a few basic working examples such as Digital Pin Viewer, Analog Graphs and other examples, found on the internet and adapted to be intelligent visualizations. All of the example are supplied with full source, attributed to the original authors, and may be feely copied or extended.

Visualizations are therefore simply just .NET dll(s) and all that is needed to register them into Visual Micro is a MicroExtension.xml file. The xml file provides a unique name for the visualization, a caption, path to the dll and namespace.className of the control to be loaded. The file also allows "Watched" expressions "of interest" to be registered and setting of the "Startup" type.

 

"StartUp" types include:-

  • open when watched expression is hit
  • open when debug starts
  • open when user demands

The captions of all open/running Visualizations will appear on the "Running Visualizations" menu of the debug button in the serial tool window. This menu item allows us to hide/show active visualizations for the current project. (Settings are retained after vs closes)

The captions of any "When User Demands" visualizations will appear on the "Other Windows" menu of the the debug button in the serial tool window. This menu allows us to enable/disable a visualization for the current project. (settings are retained after vs closes)

Visualizations can be placed in specific folders in any one of three locations. Under the vm program folder, under My Document, under a sketch folder. The latter being specific visualizations for a single sketch project.

To create a visualization we create a normal .net class project and set the COM Visible property to true (Project>Properties>Build>Assembly>Com Visible checkbox. Adding a UserControl or Form to the class project and inheriting MicroDebugDataVisualizationWindowBase or MicroDebugDataVisualizationControlBase

To gain access to these inheritable classes the project must contain a reference to Visual.Micro.Debugger.Data.Extensions" assembly that is available in the global assemblies list. Tip: Right click the project and select "Add Reference". Alternatively copy and rename one of the provided Visual Micro examples which will already be configured with the reference :)

  • using Visual.Micro.Debugger.Extensions
  • using Visual.Micro.Xml

The xml object is an optional helper, if you have experience with xml you might have a better solution.

If you copy an existing Visualization we must replace the projects "Assembly" guid with a new one. Create a new guid using Tools>Create Guid. Set the new guid against the project by right clicking the project and clicking the Advaned or Assembly button

Click to enlarge. Soon to be released open source debugger visualizations. Digital and analog visualizations are included with the debugger upgrade. Build visualizations using ANY data from an Arduino

How can we debug our visualizations

Currently, the controls shipped with visual micro are .NET3.5 format so that they work for both VS 2008 and 2010 users. If you are using VS2010 and want to debug a visualization it is recommended that you switch the .NET version (project properties) to 4.0. If you do not do this then you will not be able to use "Start>debugging" in your c# or vb projects. Instead you will have to use "Tools>Attach to process" once you have an Arduino sketch open in Visual Studio. "Start>debugging" is the recommended debug option because it will allow you to edit/change your code during an active debug session.

It gets a bit confusing because to debug your visualization you will have two debug sessions running in two different visual studio instances. One for your visualization and one for the arduino :) You should also bear in mind that if you havd a sloe machine or if you hit a breakpoint in your C#/Vb code the pc will fall behind realtime with the arduino. This is not a probleme except it might cause confusion. So after breaking into a visualization and fixing a bug, I suggest re-starting the debug session.

When you try to start>debug a class project in visual studio you may encounter a message telling you that class projects can't be debugged directly. Our extension is a Visual Studio plugin so we actually want to debug visual studio when it is running our extension.

To make the debug work correctly, right click the project and open the project properties window. Click the "Debug" tab and click the radio button called "Start external program:". Enter the full path to your visual studio program in the box. An example being "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe". On the same screen, set the "Working directory:" to the folder containing the devenv.exe, an example might be "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\"

Once you have set .NET4 and configured the options above you can switch the visualization project to "Debug" mode and press F5. This will start visual studio so you can open a sketch project that will use the visualization. Configure the sketch for "Full" debug and press F5 to start the arduino debugger. When the visualization opens in the arduino debugger your breakpoints should be hit in the visualization control debugger.

Important Point: When a visualization has been loaded into a tool window by an instance of visual studio, the visualization (dll) will not be unloaded/released until the Visual Studio instance exists. A hidden visual studio tool window is still open and available. You will not be able to re-compile the visualization while the assembly remains loaded into another visual studio instance

Configuration Settings

Whilst a visualization dll can not be re-loaded without re-starting visual studio it is possible to alter the settings in the MicroExtension.xml which is re-loaded each time the debugger starts. This also means that is is possible to add new extensions without re-starting visual studio, Visual Micro will auto detect new xml files each time the debugger starts.

The MicroExtension.xml file also provides the facility for "User Settings" allowing the visualization to dynamically render different graphics based upon xml properties. This means that a single visualization control can be highly customizable without the need to re-compile or re-build the visualization dll.

Below is an example of a very basic visualization. The project provides 3 cicular dial instrument controls (like speedometers). When the arduino debug starts the visualization is notified and is also provided with the xml object of the MicroExtension.xml along with simple functions to provide the xml data to the c#/vb code.

The c#/vb visualization example responds to the visual micro "OnDebugInit" event and initializes its graphical controls based upon the xml properties. This means that you can create a flexible visualization control that can be used in different ways by different people or in different projects.

In the xml example below the xml is a know visual micro structure except for "ConfigSettings". This section(s) is entirely user defined but it is hoped we find and agree a standard that we can all work with.

The ConfigSettings section can be accessed in c#/vb as follows:-

  • XmlNode ConfigSettingSensor1;
  • ConfigSettingSensor1 = MicroGetVisualizationConfigSetting("Sensor1");
  • ValueExpressionNameSensor1 = XmlHelper.GetAttribute(ConfigSettingSensor1, "ValueExpressionName")

MicroExtension.Xml example follows

<MicroDebugExtension Startup="WhenHit"
Name="SketchDebugExtension1"
Caption="Sketch Debug Example 1"
Enabled="1">

  <Visualization Type="ToolWindow" 
AssemblyName="bin/SketchDebuggerExample1.dll"
FullTypeName="VisualizationExamples.SketchDebuggerExample1Window"
Startup="WhenHit">
    <Watch>
<Expressions>
<Expression Name="ValueOfDial1">analogReadingA</Expression>
<Expression Name="ValueOfDial2">analogReadingB</Expression>
</Expressions>
</Watch>

<ConfigSettings Name="Standard">
<ConfigSetting Name="Page"
FontSize="12pt"
/>
        <ConfigSetting Name="Sensor1"
DialText="Something 1"
MinValue="0"
MaxValue="1100"
NoOfDivisions="11"
NoOfSubDivisions="5"
ValueExpressionName="ValueOfDial1"/>
        <ConfigSetting Name="Sensor2"
DialText="Something 2"
DialColor="Green"
MinValue="0"
MaxValue="1200"
NoOfDivisions="12"
NoOfSubDivisions="5"
ValueExpressionName="ValueOfDial2"
/>
<ConfigSetting Name="Sensor3"
DialText="Something 3"
MinValue="0"
MaxValue="1200"
NoOfDivisions="12"
NoOfSubDivisions="10"
DialColor="Yellow"
ValueExpression="ValueOfDial3"
/>

</ConfigSettings>

</Visualization>
</MicroDebugExtension>

In what order does Arduino combine sketch files (pde/ino) during compilation?

by Visual Micro 18. July 2012 08:32

During compilation Arduino sorts (sequences) the sketch files as follows:-

  1. Main sketch file is always first
  2. Followed by sort based on alpha char codes

This is a correct example for sketch 'MySketch.ino' :-

  1. MySketch.ino
  2. AFile1.ino
  3. BFile2.ino
  4. aFile3.ino
  5. bFile4.ini

The release of visual micro due end of july 2012 applies the above order. Previously releases always ensured the man sketch was first but then incorrectly applied a standard alpha sort which resulted in an incorrect sequence as shown below:-

  1. MySketch.ino
  2. AFile1.ino
  3. aFile2.ino
  4. BFile3.ino
  5. bFile4.ini

Arduino avrdude upload error common causes

by Visual Micro 2. June 2012 14:58

Generally there are only a few reasons for avrdude upload errors during arduino serial/bootloader uploads:-

  • The wrong serial port is selected. Answer - Select the correct serial port from the serial ports list
  • The wrong Arduino board is selected. Answer - Select the correct board from the Arduino boards list
  • Tools>Upload using Programmer is incorrectly ticked. Answer - Untick the menu item

Arduino Upload Error Example

Uploading to I/O board using 'COM37'

avrdude: Version 5.11, compiled on Sep 2 2011 at 19:38:36

Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

Copyright (c) 2007-2009 Joerg Wunsch

[more detail...]

Arduino Leonardo Upload Differences

by Visual Micro 2. June 2012 08:15

The Arduino Leonardo is a usb device that works in a different way to older or existing Arduino hardware. When a Leonardo starts it breifly appears in programming mode on a different serial port to the one it normally uses.

When an Arduino upload request begins the specified Leonardo serial port is polled at 1200 baud causing the Leonardo to reset. If the port is not found, both the Arduino and Visual Studio IDEs assume that the Leonardo has been reset or was resetting/starting and skip the attempt to reset.

Both Visual Studio and the Arduino IDE, scan to detect the new programming port appearing in the computers serial ports list which indicates a Leonardo startup has been detected. If a new port is not detected then both IDEs' will wait for 5 seconds (on windows) before raising the error:- "Couldn’t find a Leonardo on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's reset button after initiating the upload."

On Microsoft windows, once the upload has completed, there is a 0.5 sec pause to ensure the Leonardo has re-booted and that the original serial port has re-connected.

After upload, Visual Studio confirms the actual upload port to the user using the "Sketch Output" tab. An example Leonardo upload message is shown below. Notice the switch from Leonardo serial COM37 to COM38

Compiling 'MySensor' for 'Arduino Leonardo'

Binary sketch size: 4844 bytes (of a 28672 byte maximum) (0.507 secs)

Uploading to I/O board using 'COM37'

Done uploading via 'COM38'

 

This document is a result of reading the source code for the arduino ide provided by arduino.cc

Arduino Leonardo - Windows Hardware USB Installation Guide

by Visual Micro 2. June 2012 05:43

Windows (tested on XP and 7)

The process for Windows XP and Windows 7 is similar. These screenshots are from an XP installation.

  • Plug in your board and wait for Windows to begin its driver installation process. If the installer does not launch automatically, Navigate to the Windows Device Manager (Start>Control Panel>Hardware) and find the Arduino Leonardo listing. Right click and choose Update driver.
  • If prompted to search for drivers online, choose "No, not this time". And click Next

Arduino Leonardo Windows Driver Installation - 1

  • When asked to install automatically or from a specific location, select "Install from a list or specific location" and press Next
Arduino Leonardo Windows Driver Installation - 2
  • Choose "Search for the best driver in these locations", and check the box "incude this location in the search". Click the Browse button and navigate to your Arduino 1.0.1 or later installation. Select the drivers folder an click OK
Arduino Leonardo Windows Driver Installation - 3
  • Click Next
  • You will receive a notification that the Leonardo has not passed Windows Logo testing. Click on the button Continue Anyway
Arduino Leonardo Windows Driver Installation - 4
  • After a few moments, a window will tell you the wizard has finished installing software for Arduino Leonardo. Press the Finish button

Original article found on arduino.cc

 

 

 

How to - Arduino code syntax in c++ (.c. cpp .h)?

by Visual Micro 1. June 2012 15:59

We often want to structure the code in our Arduino projects to keep it simple and organised. The Arduino system allows us to create two main types of source files. The first type of source file are the "standard" Arduino source files. These standard files have a .pde or .ino extension and are, behind the scenes, automatically combined into a single file during program compilation. This provides some advantages in terms of simplicity but is not as flexible as using standard C/C++ soure files.

The second type of file are normal C++ files. In Visual Studio it is possible to add C++ files to Arduino projects using the built in menu commands or the additional Arduino "quick insert" commands shown on the "Add New Item" button on the toolbar. The quick insert creates either a .c or a .cpp files and a .h file.

When c++ files are used in an Arduino project the Arduino core is not automatically available to the source code, as it is with .pde/ino files. However, the Arduino core can be included in various ways one of which is described below:-

If you look in "[arduinoide folder]/hardware/arduino/cores/arduino" you will see the arduino core files for include ideas such as hardwareserial.h, string.h or avr/pgmspace.h. HardwareSerial.h provides access to the Arduino serial commands that are normally available in a .pde or ino files.

In Arduino IDE version 1.0 and above the main Arduino program include is "Arduino.h", in older versions it is "WProgram.h". If required we can use the automatically defined ARDUINO constant to check for and use the correct include based on the current Arduino IDE version.

The following code example is how to include the correct Arduino core header and also to provide access to Arduino Serial functions from mycode.cpp and mycode.h files:-

mycode.c or mycode.cpp:-

#if defined(ARDUINO) && (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "HardwareSerial.h"
#include "myc.h"

mycode.h:-

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "HardwareSerial.h"
#include "myc.h"

Tip: All arduino versions 1.0 and above have a version greater than or equal to 100

Reference Arduino In .cpp and .h Source Code

by Visual Micro 1. June 2012 12:00

When C++ files are used in an Arduino project the Arduino core is not automatically available to the source code, as it is with .pde/ino files. This document describes how to include the Arduino core manually in c++ files.

The following code example is how to include the correct Arduino core header and also to provide access to Arduino Serial functions from mycode.cpp and mycode.h files:-

mycode.c or mycode.cpp:-

#if defined(ARDUINO) && (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "HardwareSerial.h"
#include "myc.h"

mycode.h:-

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "HardwareSerial.h"
#include "myc.h"

Tip: All arduino versions 1.0 and above have a version >= 100. Example: Arduino 1.0.1 has a version of 101.

Tags:

General

Arduino Debug Release Information - Breakpoints Example

by Visual Micro 19. May 2012 13:59

The Arduino debugger upgrade for Visual Studio is due for release in June 2012. The date has slipped slightly due to high work loads. The debugger includes eveything required to set conditional breakpoints, trace messages, expression reporting step through pre-defined debug points. The Arduino debug tool does not require you to make any code modifications to your arduino source code so it is fully automated.

In the Arduino breakpoints below we see 5 breakpoints of various types. Two breakpoints have conditional expressions, one breakpoint produces a trace message and also reports the value of variable i. An unlimited number of Arduino variables to watch can be set per breakpoint, the message is also optional. Two breakpoints have hit conditions (n times before each hit)

Read more about the Arduino Debug Tool

Please EMAIL beta @ visualmicro.com if you would like to be a beta tester

Configure Arduino Debugging to use Software Serial

by Visual Micro 6. May 2012 12:00

The method of "Transport" used to send or receive debug messages on the Arduino defaults to standard serial ports such as Serial or Serial1 etc. The default (empty) is "Serial" however the RemotePort project property can be altered to another port suppoted by the micro-controller.

The "RemoteTransport" project property allows SoftwareSerial and spare digital pin(s) to be used as an alternative to the built-in Serial ports. The SoftwareSerial library is automatically included duiring debug compilation if required. The RemoteTx (and RemoteRx for Debug Break/Pause/Step) project properties should be populated with the digital pin numbers used for tx/rx on the Arduino. See the arduino.cc software serial guide when choosing digital pins

To use FastSerial the project must include all required library references and the RemoteTransport project property must be set to FastSerial. Other methods of debugger "transport" can be added by advanced users. We hope that in the future debugger transport will be configurable using a simple user interface. Please ask in the forum for more information.

All of the options shown below are default except for the FastSerial setting. 

click the image for a draft debugger overview tutorial

Tips...

  • To use a different Arduino serial port such as Serial2, enter Serial2 as the "RemotePort" property
  • To use a different PC serial port such as COM7, enter COM7 as the "LocalPort" property
  • To switch from the default speed of 115200, set the "LocalSpeed" to value from the pick list, RemoteSpeed will change automatically to match.
  • Apm users should not that the "Throttle" properties relate to debugger bandwidth throttling and are nothing to do with drones :) Leave the throttle at the defaults, learn more about the throttle via the forum.
  • If using SoftwareSerial on spare digital pins you should clear the RemotePort property and set the Remote Rx Tx Pins to the digital pins that are connected to the serial device or cable. If "Break/Pause" and "Startup Wait" are not enabled (default) then only the Arduino Tx pin is required

And a few hints that might mean something at some stage...

1) Validation Skip (project property) 

Set to False if you are not altering the arduino code in a way that might cause compilation errors. This will skip the "release" build of apm that happens prior to the debug build. (Saves time)

2) TracePoint Throttle (project property)

An unconditional breakpoint/tracepoint example in the 50hz loop is a good example for us to use...

By default, for newer users and non fastserial users the breakpoints are throttled so that the 50hz loop will only run at about 8-12hz. Visual micro ensures an 80 millisecond delay between breakpoints. 

a)
FastSerial, if available, is more efficient than normal Arduino Serial therefore you could change the project property called Throttle (ms) to, for example, 30 (milllis) which would improve the speed. Leaving the Throttle (ms) empty uses the default of 80ms.

or b)
Try setting "Throttle Enabled = False" to entirely disable throttling in which case apm will send as fast as it can but it is possible for the arduino buffer to overflow causing the debug to stop working or invalid serial packets to be received by the debugger

or c)
Set the breakpoint "HitCount" to 50. Because we are in the 50hz loop this should produce one debug breakpoint message every second allowing apm to function at full speed inbetween each breakpoint message

or d)
Set the "Hit Counter" project property to milliseconds and all the breakpoint "HitCounters" will no longer be treated as counts, instead the HitCounts will represent milliseconds. So a breakpoint "HitCount" of 1000 would produce a debug  message exactly once every second regardless of where you put the breakpoint.

IMPORTANT NOTES 

When sharing the serial port with the arduino code you might expect to see some "junk" in the serial window. This is normal, the debugger windows will workaround the "junk" and function correctly. If your arduino program reads the serial port then you will not be able to switch on the break/pause facility in which case you should use a different arduino serial port or SoftwareSerial on two digital pins.

The throttle setting is very dependant on the speed of your pc. If the arduino sends messages faster than your pc can process them the pc display will fall behind the arduino. The pc display will no longer be real-time and will be confusing.

Reminder: Please read our disclaimers !!

Break Points Can Be Disabled But ...

by Visual Micro 5. May 2012 18:46

You will notice that in Visual Studio breakpoints can be disabled. Visual Micro always sends all breakpoints to the Arduino during compilation but will stop reporting disabled breakpoints.

This means you can switch on/off breakpoints during a single debug session by enabling or disabling them.

If using debug break/pause with disabled breakpoints the breakpoint will still be active in the arduino but the pc will automatically send an F5. This means that disabled breakpoints will still cause the Arduino to work more slowly than normal but that the breakpoint will not require users to manually press F5. This feature might be useful in some situations or to freeze the variables window for certain variables.

It is recommended that breakpoints are exported and/or deleted when not being used.

BreakPoint Hit Count Condition

by Visual Micro 5. May 2012 18:36

Instead of, or in combination with a conditonal breakpoint expression (as described in earlier posts) to make an Arduino trace or break point we can use the "Hit Count" dialog.

The hit count dialog is opened by right clicking any break break and selecting "Hit Count" from the menu that appears (described in detail in earlier posts). The Hit Count dialog allows us to select 4 options.

The example below is self explanatory. If we are adding a break point into a frequent arduino loop we might set a hit count of 30000. In effect this will increment a counter in the code until 3000 is reached, then the breakpoint will be executed prior to the counter being reset to 0. To use a normal "count" for a hit counter then set the project or global property "Hit Count" to "Counter". The default "Hit Count" is "milliseconds"

Tip: The hit count is automatically combined with any conditional expression (as described in earlier posts). Example, we use "break when hit count is a multiple of 250 and we set the HitCount in the project properties to milliseconds (is the default) then we will see a breakpoint or tracepoint 4 times per second (every 250 ms)

How to control the Arduino execution speed during a debug session

 

note: when using non-breaking tracepoints the stream of debug messages are processed on the pc in batches of up to 10 per second (also depends on the amount of data). The time displayed in the pc debug trace is the time each message is processed on the pc, not the time the message was sent from the micro-controller. For example, we might use a tracepoint at 4hz (every 250ms). In this case the debug data will be an accurate reflection of the micro_controller every 4hz however the millisecond values in the pc trace might not appear enitrely consitent. 

Break/Pause Into Source Code

by Visual Micro 5. May 2012 17:59

2015 - The following is old documentation. In many ways it does still apply but the debug features are better described in our Documentation section

The example below shows both automated and manual arduino source debugging features. The "old style" manual debug of arduino is shown in the code. Some of the automated features are shown in the "When Breakpoint hit" dialog

Individual points in your Arduino source code can be configured to break/pause or simply trace/continue without pause. The configuration settings change depending on the type of break or trace point. When a break/pause is hit Visual Studio will automatically open and select the line of arduino source that contains the current break point.

Most points can be configured to either continue or break/pause using the breakpoint message window as shown below.

Untick the "Continue Execution" checkbox to force a pause. Behind the dialog in the example we can see that the Arduino is paused on a breakpoint. Pressing F5, clicking any of the Visual Studio "Start>debug" commands or sending any serial data will re-start the Arduino program.

This example shows a few of the options that allow Arduino source code debugging

IMPORTANT WARNINGS! It is only right that we make the following statements very clearly...

Before enabling Arduino debug "break/pause" you must carefully consider what will happen if the Arduino suddenly stops on a breakpoint. Will your flying machine drop out of the sky and injure someone or be damaged. Will a robot crash? Will a motor be stuck on full power? Will a device stop responding and cause a critical problem? This is entirely your responsibility, if you are in any doubt then ask questions on the forum but please do not use this feature until there is no doubt! The same applies to the ability for the Arduino to pause at startup.

All arduino debug pause features provided in Visual Studio can not be relied upon in sensitive or dangerous siutations. This is because of the use of the serial port for the debug "continue" command. In the current version any data transmitted over the debug serial port will cause the Arduino to continue program execution which might be unexpected in many cases such as electrical interference and stray serial data.

Debug break/pause should only be used with breakpoints that will be hit every so often otherwise you will spend your time hitting F5 (continue) lots and lots of times :) If you mistakenly set a break condition that happens to often you will find that you can untick the "connect" checkbox on the serial window to stop the serial comms. This will, ineffect, stop the debug session but bear in mind that the Arduino will still be in debug mode.

Because the Arduino can be in debug mode and might be paused while Visual Studio is not in debug mode it is suggested that you either upload a "release" version when you finish debugging. This will ensure the Arduino is always in a known state. 

It is strongly recommended that you switch on the DTR setting on the serial window or in the Visual Micro options. The DTR setting will ensure that the Arduino is reset whenever  the serial window connects and/or debugging starts. Without the DTR setting it possible to start debuging when the Arduino is already paused on a beakpoint which will be confusing for most users.

Debug Break/Pause is enabled by selecting the project in the Solution Exporer and viewing the properties window (not right mouse click properties, the Property Window). Set Enable Break/Pause to True. Each time Break/Pause is enabled for a project a warning is displayed.

 

Optionally make use of the _DEBUG symbol in your Arduino code

by Visual Micro 5. May 2012 17:36

Nov 2012 - This document is out of date. A new prject property called Define _DEBUG allows you to enable this property in the standard plugin. The #define will be called _DEBUG

When debuging is enabled the _VMDEBUG symbol allows code to be used "instead of" or "in combination with" the Visual Studio debug tools

The example below shows the wrong name for this symbol

 

Share The Arduino Serial Port With The Debugger

by Visual Micro 5. May 2012 17:16

The example below shows a single Arduino Serial Port being used for both debug and static serial messages.

The example goes one step further by making use of the DEBUG symbol allowing the static messages to only be included in the program when debuging is enabled for the project.

Notes

In the example above the code in the Serial.begin() in the setup() function is not required because both the debugger and the project are using the same port. However, if the defined(DEBUG) condition is removed from the code then Serial.begin() would be required. Therefore it is safer to add Serial.begin() if your sketch code directly uses the Serial port.

The same would apply if the debug "Remote Port" on the project properties was set to a port other than "Serial". In this case you would certainly need to initialise the Serial port.