Getting weird behavior in if statement.

Hi all, I have an Arduino UNO and I am using the 1.8.5 ide. I have a MAX7219 and ADS1115 connected also.

My issue is that when my code runs, it properly prints to serial all prints in the setup code and in the loop before the if, but when the conditions are met for the if statement none of the prints are written to serial. Also none of the digits on the display are changed, but the variable that controls the if is properly reset at the end of the if statement. It then just continues to do the print at the beginning of the loop and resetting the variable that controls the if when the if conditions are met. Also sometimes the variable that controls the if isn't set to 0 like the code at the bottom of the if says, but is sometimes set to 1 instead.

#include <LedControl.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>

//pin 12 is connected to the DataIn 
//pin 11 is connected to the CLK 
//pin 10 is connected to LOAD 
LedControl lc=LedControl(12,11,10,1); 
Adafruit_ADS1115 ads1115;
int16_t zero_offset = 0;

void setup() {
  Serial.begin(115200);
  lc.shutdown(0,false);
  lc.setIntensity(0,8);
  lc.clearDisplay(0);
  lc.setDigit(0,1,0,false);
  lc.setDigit(0,3,0,false);
  lc.setDigit(0,2,0,false);
  lc.setDigit(0,0,0,false);
  Serial.println(F("Screen Initialized."));
  ads1115.setGain(GAIN_SIXTEEN); // 16x gain
  ads1115.begin();
  Serial.println(F("Started ads1115. Starting Loop."));
  zero_offset = ads1115.readADC_Differential_0_1();
  Serial.print(zero_offset);
  Serial.println(F(": is zero offset"));
}

int i = 0;
int16_t results[5];

void loop(void) {
  Serial.println(F("Adding result"));
  results[i] = ads1115.readADC_Differential_0_1();
  Serial.print(results[i]);
  Serial.println(F(": Is new result."));
  Serial.print(i);
  Serial.println(F(": is i")); //everything to here prints <=
  if(i>4)
  {
    Serial.println(F("Creating average variable"));
    int16_t average = 0;
    Serial.println(F("Starting For"));
    for(int j = 0; j < 5; j++)
    {
      Serial.print(F("Average is currently: "));
      Serial.println(average);
      average += results[j];
    }
    average = average / 5;
    average = (average - zero_offset) * 7.833;
    Serial.println(average);
    char digits[5];
    dtostrf(average, 4, 2, digits);
    //Serial.print(digits[3]);
    lc.setDigit(0,1,atoi(&digits[3]),false);
    lc.setDigit(0,3,atoi(&digits[2]),false);
    lc.setLed(0,2,0,true);
    lc.setDigit(0,2,atoi(&digits[1]),false);
    lc.setDigit(0,0,1,false);
    delay(100);
    i = 0;
  }
  else
  {
    i++;
  }
}

Here is a sample of output:

Screen Initialized.
Started ads1115. Starting Loop.
1: is zero offset
Adding result
0: Is new result.
0: is i
Adding result
0: Is new result.
1: is i
Adding result
1: Is new result.
2: is i
Adding result
0: Is new result.
3: is i
Adding result
1: Is new result.
4: is i
Adding result
1: Is new result.
1: is i
Adding result
0: Is new result.
2: is i

Any help is much appreciated. I've been stuck for a while.

char digits[5];
    dtostrf(average, 4, 2, digits);

Try here

From the above link:

The caller is responsible for providing sufficient storage in s.

TolpuddleSartre:

char digits[5];

dtostrf(average, 4, 2, digits);



[Try here](http://www.microchip.com/webdoc/AVRLibcReferenceManual/group__avr__stdlib_1ga060c998e77fb5fc0d3168b3ce8771d42.html)

jremington:
From the above link:

The caller is responsible for providing sufficient storage in s.

I changed average to double and increased the size of digits to 20 just in case, but after uploading the changes there was zero effect on the behavior. Also changing digits to string with no array won't compile, and looking at other code I think the char array is proper, but I could be wrong.

  results[i] = ads1115.readADC_Differential_0_1();
....
  if(i>4)

Too late!

I think the char array is proper, but I could be wrong.

You definitely need more than 5 bytes (four characters plus zero terminator) in the digits array, as well as to keep in mind that array indexing begins with the 0th element.

jremington:
You definitely need more than 5 bytes (four characters plus zero terminator) in the digits array, as well as to keep in mind that array indexing begins with the 0th element.

I agree and I said in my response that I increased the array size, I was just saying that I think that it being of type char was proper.

TolpuddleSartre:

  results[i] = ads1115.readADC_Differential_0_1();

....
 if(i>4)



Too late!

What do you mean too late? What would I need to change?

You MAY NOT store a value in results[5], but the damage is already done by the time you check if (i>4).

jremington:
You MAY NOT store a value in results[5], but the damage is already done by the time you check if (i>4).

OOH I see I'm overflowing the array because I don't make sure i is below 5 until after I add. I changed it to i>=4 and it now works. I feel kinda stupid that should have been more obvious haha. It was confusing me because the prints under it were working, so I thought that something in the if was wrong.

Thank you guys so much for your help!