Hello.
I am currently running Arduino UNO alongside a NXP microcontroller, both of which are connected to an INA219 current sensor and an SSD1306 128x64 OLED screen. Both SCL and SDA have have a 10k pull up resistor to 5V, and everything is wired properly.
The Arduino is able to read the values from the current sensor and properly display them on the OLED screen on its own. I'm currently using NXP microcontroller to send its current "State" (small integer value 0~4) to the Arduino via I2C. Once the transfer is complete, the NXP MCU releases the I2C bus. Using just the serial monitor, I have confirmed that the value is being recorded, the Arduino can continue to read the current sensor, and is able to update the state value again should another transfer receieved.
The problem I'm having right now is that now when I try to display something on the OLED after receiving the "state", the program would freeze (Both the Serial monitor and OLED screen). However sometimes (very rarely), the program goes through and the OLED screen (and Serial monitor) updates the state value accordingly.
I'm assuming that my error has something to do with the ISR handler from onRecieve(). That is, the ISR forces the OLED to cease communications with the Arduino in order to receive the information from the NXP MCU and once it finishes, it attempts to start where it left off and continue speaking to the OLED (which it can't anymore since the I2C bus was already changed and used). However if this is the case, I find it strange that the program can still pull values from the current sensor using the I2C and have them displayed on the Serial monitor. It's only when I attempt to display the values on the OLED when the program freezes.
I've tried restarting the I2C protocol in the ISR handler by reinstating wire.begin() and display.begin again however this had no effect.
Here is my code:
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float energy = 0;
float test;
volatile float state;
unsigned long previousMillis = 0;
unsigned long interval = 100;
void setup(void)
{
Wire.begin(0x8C);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
ina219.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(500);
display.clearDisplay();
}
void loop(void)
{
Serial.println(millis());
ina219values();
Serial.println(loadvoltage);
//==============Errors if included============================
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 20);
display.println(current_mA);
display.setCursor(50, 20); // x axis, y axis
display.println("A");
display.setCursor(0, 30);
display.println("STATE =");
display.setCursor(50, 30);
display.println(state);
display.display();
//=====================*/
Serial.println(state);
delay(1000);
}
void receiveEvent(int howMany) {
state = Wire.read();
}
void ina219values() {
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);
energy = energy + (loadvoltage * current_mA)/3600;
}
Any help is greatly appreciated.