I am troubleshooting a project that uses a XIAO nRF52840, SSD1306, AHT20, and HC05 on one breadboard, and a Pro Micro, AHT20, and HC05 on another. The idea is to display the temp and humidity from both the local (master) and remote (slave) locations on the master OLED display. Both the AHT20 and the SSD1306 are on the I2C bus of the local board. Almost everything works: I can retrieve and display the remote data on the local display.
When I add the same code that is used on the remote device to sample the temp and humidity from the AHT20 to the local code, the sketch stops. If I comment out the aht.getEvent statement, the sketch runs. It doesn't sample the data, but it doesn't crash.
What's very weird (to me at least) is that un-commenting the aht.getEvent statement causes the sketch to crash BEFORE the statement is executed. I demonstrated this by inserting print statements before the aht.getEvent statement. Without the aht.getEvent statement, all of the Serial.println statements print normally. With the statement, only "Serial monitor set to 9600" appears in the serial monitor. Also, with the statement, double-tapping the reset button is required or the upload gives errors related to the com port.
How can a line of code cause a problem before it's executed?
Also, should the AHT20 library be able to handle having 2 devices on the I2C bus, or do I need to access the AHT20 directly with hex code?
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_AHTX0.h>
#include <math.h>
const int button = 2;
int buttonState = 0;
int rtemp, rhumidity; //data from remote sensor
int ltemp, lhumidity; //data from local sensor
float lctemp,lftemp;
int lroundtemp, lroundhumidity;
Adafruit_AHTX0 aht;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
pinMode(button,INPUT); // button wired to gnd
delay(3000);
Serial.begin(9600);// Set data rate for the serial monitor
delay(3000);
Serial.println("Serial Monitor set to 9600");
delay(3000);
Serial1.begin(38400);// set the data rate for the Serial1 port to match the UART baud on HC05
delay(5);
Serial.println("Serial1 is now set to 38400\n");
Serial.println("line 46");
//delay(3000);
Serial.println("line 48");
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
Serial.println("line 56");
}
void loop()
{
*/ /*******************************************************************
This section gets Local data from the on-board AHT20
********************************************************************/
Serial.println("line 89");
sensors_event_t humidity, temp;
Serial.println("line 91");
aht.getEvent(&humidity, &temp); // populate temp and humidity objects
Serial.println("line 93");
delay(100);
//lctemp=temp.temperature;
lftemp=(lctemp*9.0/5.0)+32.0;
lroundtemp=(int)roundf(lftemp);
Serial.print("Temperature: ");
Serial.print(lroundtemp);
Serial.println(" °F");
delay(2000);
//lroundhumidity=(int)roundf(humidity.relative_humidity);