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
2 Answers 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]) );
-
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– ArunMKumar2014年12月05日 09:18:16 +00:00Commented Dec 5, 2014 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– Peter Bloomfield2014年12月05日 10:15:54 +00:00Commented Dec 5, 2014 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– ArunMKumar2014年12月05日 12:29:28 +00:00Commented Dec 5, 2014 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 thenew
keyword), but I don't know if that would break something else in your code/libraries.Peter Bloomfield– Peter Bloomfield2014年12月05日 13:03:28 +00:00Commented Dec 5, 2014 at 13:03
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.
addr_t childAddr[NUM_CHILDS] = { 0x10, 0x02};
and also tried assigning the valuess inside my Task, still the array reads as Zero.