Chat freely about anything...

User avatar
By mtaivalmaa
#76539 I am trying to display Altitude data on the OLED but seem to have an I2C issue. If I break these into 2 seperate sketches the OLED works fine and the sensor works fine. Combined as below the OLED displays HELLO on startup but then never changes.

I originally tried to have the sensor on the same pins as the OLED but that didn't work the sensor never showed up on a I2C buss scan.

Any Help would be appreciated.

Mike

Code: Select all#include <Wire.h>
#include <SparkFunMPL3115A2.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 16

Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

//Create an instance of the object
MPL3115A2 myPressure;
float altitude;
float agl;
int start = 1;
int resetPin = 12;

void setup()   {
 
  Serial.begin(9600);
 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  display.clearDisplay();
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("HELLO");
  display.display();
 
  Wire.begin(12,13);
 
  myPressure.begin(); // Get sensor online
  myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
  myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
  myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
 
  agl = myPressure.readAltitudeFt();
 
}

void loop() {
  // put your main code here, to run repeatedly:

if (start = 1) {
int average = 0;
for (int i=0; i <= 1000; i++){ 
      average = average + (agl = myPressure.readAltitudeFt());
      delay(2);
   }
   agl = average/1000;
Serial.println(round(agl));
start = 0;
}
mystart:

altitude = myPressure.readAltitudeFt();
Serial.println(round(agl));
Serial.println(round(altitude));
Serial.println(round(altitude-agl));
int mymins = ((millis()/1000)/(60));
Serial.println(mymins);
 
display.clearDisplay();
display.setCursor(0,0);
display.print(round(altitude-agl));
display.display();

if (round(altitude-agl) >= 10) {
 Serial.println("WTF");
Serial.println("Going into deep sleep for 20 seconds");
  ESP.deepSleep(5); // 20e6 is 20 microseconds
}

delay(500);

goto mystart;







 
}


User avatar
By mtaivalmaa
#76634 So I changed to a oled driver that uses the wire.h directly and it is working but crashes the oled after 15 secs. the program is still running but oled goes black..





Code: Select all// Set display remap mode.
// displayRemap(bool mode) selects normal mode or 180 degree rotation mode.

#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <SparkFunMPL3115A2.h>

// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

// Define proper RST_PIN if required.
#define RST_PIN -1

SSD1306AsciiWire oled;
MPL3115A2 myPressure;

float agl;

//------------------------------------------------------------------------------
void setup() {

Serial.begin(9600);
Wire.begin();
Wire.setClock(400000L);

#if RST_PIN >= 0
  oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
  oled.begin(&Adafruit128x32, I2C_ADDRESS);
#endif // RST_PIN >= 0
  oled.setFont(Verdana_digits_24);


}
//------------------------------------------------------------------------------
void loop() {
 
  Wire.begin(2,14);
 
  myPressure.begin(); // Get sensor online
  myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
  myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
  myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
 
  agl = myPressure.readAltitudeFt();

Serial.print("AGL ");
Serial.println(round(agl));

Wire.begin(4,5);
//oled.setTextSize(4);
oled.clear();
oled.println(round(agl));

delay(750);


}