0

I have written a code for my Arduino which spans multiple files, In the file containing the main task, i have a Global Array like this

addr_t childAddr[NUM_CHILDS] = { CHILD1_I2C_ADDR, CHILD2_I2C_ADDR };

where

#define NUM_CHILDS  0x02
#define CHILD1_I2C_ADDR         0x10
#define CHILD2_I2C_ADDR         0x02

Now to debug my issue, in my Task i do this

Serial.print(childAddr[0]);
Serial.print(childAddr[1]); 

All i receive is

00

I looked at the issue online, someone pointed it towards overshooting the RAM, I am working on a Pro Mini with ATmega328P with 2K RAM. This is my Output for analyzing the eld with "avr-size"  utility

  • In desperate attempts to solve this i even tried addr_t childAddr[NUM_CHILDS] = { 0x10, 0x02}; and also tried assigning the valuess inside my Task, still the array reads as Zero. – ArunMKumar Dec 5 '14 at 3:59
  • I found a workaround for this, I declared the array locally and am using it. As per my understanding i think, C++ initializes all global variables to Zero, What eludes me is the fact that, i tried initializing them inside a function and they still read as zero. – ArunMKumar Dec 5 '14 at 4:12
  • what type is addr_t? – BrettAM Dec 5 '14 at 5:41
  • Its is an unsigned char – ArunMKumar Dec 5 '14 at 6:54
  • Your code on Github does not have that array at global scope, so it is almost impossible to debug code which we can't see. – Nick Gammon Jul 6 '15 at 6:29
2

Since addr_t is a character type, I suspect Serial.print() is trying to output the ASCII character representation of your two values. ASCII codes 0x10 and 0x02 are non-printing characters, so it might just be outputting 0's for lack of anything better to do.

To output the data as actual numbers, try casting them to integers:

Serial.print( static_cast<unsigned int>(childAddr[0]) );
Serial.print( static_cast<unsigned int>(childAddr[1]) );
| improve this answer | |
  • Sorry, It does not work either, I have defined the array locally within the function now, and it seems to work fine with/without the static_cast<unsigned int>(childAddr[0]) I am getting the correct values with my version and your version of Serial Print. Pretty strange though. Is there any kind of compiler setting for this? – ArunMKumar Dec 5 '14 at 9:18
  • No, there should be no setting which affects it. It might help if you add all your code to the question though (if possible). There could be other issues like masking or initialisation order. – Peter Bloomfield Dec 5 '14 at 10:15
  • well the code is really long to be posted here... I will give a link to my repo at github. github.com/ArunMKumar/IIEC/blob/Active/Project.cpp In this file if you search for addr_t childAddr you can get to the part i am talking about, currently it is declared in function scope, it was dclared at line45. also i appreciate your efforts in helping me solve this. – ArunMKumar Dec 5 '14 at 12:29
  • @ArunKumar It looks like initialisation order could be the problem, although I can't tell just from that code. Basically, the code that's trying to use the global array might be executing before the global array has actually been setup. It can happen if you run code in the constructor of an object that is instantiated globally or statically. A possible solution is to create your objects on the heap during setup() (i.e. using the new keyword), but I don't know if that would break something else in your code/libraries. – Peter Bloomfield Dec 5 '14 at 13:03
0

You should try to post a minimal piece of code that reproduces your problem. From what you posted I assembled this:

typedef unsigned char addr_t;

#define NUM_CHILDS  0x02
#define CHILD1_I2C_ADDR         0x10
#define CHILD2_I2C_ADDR         0x02

addr_t childAddr[NUM_CHILDS] = { CHILD1_I2C_ADDR, CHILD2_I2C_ADDR };

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println(childAddr[0]);
  Serial.println(childAddr[1]); 
  }  // end of setup

void loop ()
  {
  }  // end of loop

Output is:

16
2

Clearly the problem isn't where you think it is.


By the way, in your file Project.h you have this:

#include "Project.h"

Why is it including itself?


void commRxISR(int count){
    /*
        ISR for the dats received on the I2C bus
    */
    Serial.write("Inside serial receive ISR\n");
    while (1);
    while (0 < Wire.available()){
        MyNode.commChild.commWriteInBuffer(Wire.read());    // Project specific implementation
    }
}

The infinite loop in the ISR is interesting. Also don't do Serial prints inside an ISR. They can (almost certainly will) hang the system.

| improve this answer | |

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.