Watching variables and values

This example details how to debug your code by :

  1. identifying a hardware or software bug
  2. monitoring your code execution
  3. watching several variables and values while your code executes

 

1) Sketch: 3_Debugging \ 1_WatchingVariables

In the given code, something is going wrong. According to the value of variable “myVar”, the on-board LED on pin 13 should turn on. However, it irritatingly keeps turned off. How on earth is that possible? It looks like there is a problem somewhere, but how do we find it?

#include <HITIComm.h>


// pins assignment
const int pin_LED = LED_BUILTIN;


// run once at power on
void setup() 
{
    // initialize library
    HC_begin();
  
    // set pins mode
    pinMode(pin_LED, HIGH); // pin 13 -> OUTPUT
}


// run repeatedly after setup()
void loop()
{
    // communicate with HITIPanel
    HC_communicate();

    int myVar = 50000;
    
    if(myVar == 50000)
    {
        digitalWrite(pin_LED, HIGH);
    }
    else
    {
        digitalWrite(pin_LED, LOW);
    }
}

 

2) Hardware or Software bug?

When dealing with microcontrollers and electronics, a bug could have different origins and it is important to immediately narrow the search. The first thing to do is to identify if a bug origin is:

  • Hardware (broken component, bad electrical wiring…)
  • Software (programming mistake, no more SRAM…)

Let’s have a look at pin 13 using the DIGITAL I/O Control Panel (“IO” button).

image398.png

As we can see, the output on pin 13 is not activated while it should, so the problem is software. If the problem is inside our code, we have to look at why the output is not set to High by the call to digitalWrite(pin_LED, HIGH). There are 2 possibilities:

  • This function is in a block of code that is never executed
  • This function is correctly called but there is an underlying problem (core library issue, incorrect board settings…)

 

3) Monitoring your code execution

Let’s check if the block of code containing digitalWrite(pin_LED, HIGH) is executed as expected. To do that, we will use a boolean variable called “executed” that becomes true only if this block of code executes and we will watch this variable on Digital Data 0.

Update the code as follows and upload it:

// run repeateadly after Setup()
void loop()
{
    // communicate with HITIPanel
    HC_communicate();
  

    // debugging variable
    bool executed = false;
    
    
    // suspicious code
    // ---------------------------------------------
    int myVar = 50000;
    
    if(myVar == 50000)
    {
        executed = true;
        digitalWrite(pin_LED, HIGH);
    }
    else
    {
        digitalWrite(pin_LED, LOW);
    }
    // ---------------------------------------------


    // monitor in HITIPanel
    HC_digitalDataWrite(0, executed); // Digital Data 0
}

Display the DIGITAL DATA Control Panel (“HITI” button). Add a Command Panel for Digital Data 0 and name it “executed”.

image399.png

We notice that variable “executed” surprisingly stays false, which means the block of code in the IF statement is NOT executed.

if(myVar == 50000)
{
    // this block is never executed

    executed = true;             // never executed
    digitalWrite(pin_LED, HIGH); // never executed
}

 

4) Monitoring several values

The only explanation is that the IF condition is false. To double confirm this, we use Digital Data 1 to monitor this condition. Moreover, we investigate why it is false by looking at the value of variable “myVar” using Analog Data 0.

Update the code as follows and upload it:

// monitor in HITIPanel
HC_digitalDataWrite(0, executed);         // Digital Data 0
HC_digitalDataWrite(1, (myVar == 50000)); // Digital Data 1
HC_analogDataWrite (0, myVar);            // Analog Data 0

Then add the required Command Panels to watch these new data:

image400.png

By looking at the values, the diagnostic is pretty clear. The condition is false because the value of “myVar” is -15536 instead of 50000. How is that possible?

 

5) Checking your data types

If you assign a value to a variable and you realize that the real value hold by this variable is totally different from what you asked, then this is because the data type of your variable (boolean, char, integer…) is not adapted to this value.

In our example, we declared “myVar” as an Integer. However, the max value that integers can hold is +32767 in Arduino. So, trying to assign the value 50000 entails a conversion error during the assignment. Thus, the bug comes from this line :

int myVar = 50000;

To correct this, we must choose a variable type which can hold the value 50000. An Unsigned Integer will do the job :

unsigned int myVar = 50000;

Update your code and upload it. The value of myVar is now correct, the IF condition is true, the block of code is executed, and the LED turns on as expected.

Arduino-computer-control-software-customizable-interface-panel-debugging-debugger-debug-code-program-development-tool-4.png

 

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.