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.