Go Down

Topic: Unexpected results with variable initiation (Read 565 times) previous topic - next topic

yerg2k

Mar 24, 2007, 04:17 pm Last Edit: Mar 24, 2007, 04:19 pm by yerg2k Reason: 1
Hello all,
I seem to be getting unexpected result with the compiler and I'll try my best track this down and document what exactly is happening. I spent a good amount of time hung up on this one.

Around the same time that I moved to multidimensional arrays, I started having problems reading from my sensors. The old manner was like such:

Code: [Select]
...
int shtData = 3;
int shtClk = 2;
...
void setup()
{
 pinMode(ledPin, OUTPUT);   // sets the digital pin 13 as output
 pinMode(piezoPin, OUTPUT);  
 pinMode(dbp, OUTPUT);
 Serial.begin(115200);        // open serial  
 pzbeep();
 pinMode(shtData, INPUT);
 pinMode(shtClk, OUTPUT);
 digitalWrite(shtData, HIGH);
 SHT_Connection_Reset();
}

void loop()
{
...
   logPtr = logPtr + 1;
   if (logPtr > logSize) logPtr = 0;    logTime = millis();
   SHT_Measure(5);
   localLogH[logPtr] = retVal;


and all was well. Then, to further complicate things, a total of 8 sensors was added. Four of these are actually two separate SHT11's with both temperature and humidity. The new code was something like this:

Code: [Select]
int shtClk = 2;                    // Common clock for both SHT11's
int shtData;                       // Selectable data pin


But there is a problem. For some reason, further on in the program, shtClk reads 0. Somewhere along the way, this variable is either not getting set, or getting reset. By putting a shtClk=2 in my setup() routine right before resetting the connection, everything works as expected. But initializing the variable in the definitions, it does not retain its value. Everything else in my program works properly, and I have checked to make sure that my arrays were been properly referenced and nothing was overwriting memory at any point.

I know that this is not a very... defining description of the rest of the program, but I can post additional information if necessary. I'm more curious if I am misunderstanding how initialization of variables works, or if this is actually a bug, and if anyone has encountered it before. I can move around where my program sets the variable in my setup() routine, to see if I can't figure out where it goes wrong.

Thanks for the help.

mellis

I know you said you checked this, but my guess it that you're overwriting memory somewhere (e.g. writing to array index outside of its bounds).  As you suggest, I would try moving the assignment around (or printing the value of your variable at various points) to try to identify at which line(s) its value changes.  

It's possible you're attempting to use more RAM than is available on the chip and that this is what's causing the strange behavior.  There's only 512 bytes available in total and some are used by the core Arduino libraries.

Go Up