CMPS12 + OLED display help - beginner

I'm very new to this so apologies if I'm doing something stupid.

I've setup a cmps14 compass with an OLED display and have merged two codes I found together. It kind of works... except...

I have a strange fault down the left side of the display.
The display updates and the numbers change for the first 5-10 seconds and then it freezes.

any idea what I'm doing wrong?

I tried making my circuit in Fritzing, but the CMPS14 is not available as a component (Frustrating!)

/*****************************************
*   CMPS12 Serial example for Arduino    *
*        By James Henderson, 2014        * 
*****************************************/

#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define CMPS_GET_ANGLE8 0x12
#define CMPS_GET_ANGLE16 0x13
#define CMPS_GET_PITCH 0x14
#define CMPS_GET_ROLL 0x15
#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);

SoftwareSerial CMPS12 = SoftwareSerial(2,3);

unsigned char high_byte, low_byte, angle8;
char pitch, roll;
unsigned int angle16;

void setup()
{
  Serial.begin(9600);              // Start serial ports
  CMPS12.begin(9600);  
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}

void loop()
{

    CMPS12.write(CMPS_GET_ANGLE16);  // Request and read 16 bit angle
  while(CMPS12.available() < 2);
  high_byte = CMPS12.read();
  low_byte = CMPS12.read();
  angle16 = high_byte;                // Calculate 16 bit angle
  angle16 <<= 8;
  angle16 += low_byte;
  
  CMPS12.write(CMPS_GET_ANGLE8);  // Request and read 8 bit angle
  while(CMPS12.available() < 1);
  angle8 = CMPS12.read();
  
  CMPS12.write(CMPS_GET_PITCH);   // Request and read pitch value
  while(CMPS12.available() < 1);
  pitch = CMPS12.read();
  
  CMPS12.write(CMPS_GET_ROLL);    // Request and read roll value
  while(CMPS12.available() < 1);
  roll = CMPS12.read();

     display.clearDisplay();
display.setTextSize(0);
display.setTextColor(WHITE);
display.setCursor(0,10);
  display.print("roll: ");            // Display roll data
  display.print(roll, DEC);
  
  display.setCursor(0,20);
  
  display.print("pitch: ");          // Display pitch data
  display.print(pitch, DEC);
  
  
  display.setCursor(0,30);
  display.print("angle full: ");       // Display 16 bit angle with decimal place
  display.print(angle16 / 10, DEC);
  display.print(".");
  display.print(angle16 % 10, DEC);
  display.display();
  

  display.setCursor(0,40);
  display.print("angle 8: ");        // Display 8bit angle
  display.print(angle8, DEC);
   display.display();
  display.clearDisplay();
  delay(500);                           // Short delay before next loop
}

Thanks
Alastair

Does not make a whole lot of sense.

Have you tried some troubleshooting?

display.clearDisplay();
display.setTextSize(0);
display.setTextColor(WHITE);
display.setCursor(0,10);
  display.print("roll: ");            // Display roll data
  display.print(roll, DEC);
  
  //display.setCursor(0,20);
  
  //display.print("pitch: ");          // Display pitch data
  //display.print(pitch, DEC);
  
  
  //display.setCursor(0,30);
  //display.print("angle full: ");       // Display 16 bit angle with decimal place
  //display.print(angle16 / 10, DEC);
  //display.print(".");
  //display.print(angle16 % 10, DEC);
  //display.display();
  

  //display.setCursor(0,40);
  //display.print("angle 8: ");        // Display 8bit angle
  //display.print(angle8, DEC);
   //display.display();
  delay(500);                           // Short delay before next loop
}

Does the above code change give you an error?

and how many of these display.display(); are needed to update the display? Just one?

Well done posting code this way, using code tags!
Code that hangs after 10 seconds.... I ask You to post an engineering wiring diagram, pen and pencil, but not any painting named Fritzing
I want to check for power overload somewhere.

Thanks for you help! i tried your code but it displays 0's as compass readings. Now I'm very confused!

hi railroader,
here's a wiring diagram
cmps + oled.pdf (347.8 KB)

Did the display mess up, is the question I want to know if not then uncomment a few more lines. did it mess up, then uncomment a few more lines, till you find the spot where the code messes up the display.

Ah i see what you’re trying to do! Will try again tomorrow as its getting late here in Belgium!

so this gave me a crazy screen, but I then uncommented the last display.display(); and it worked with showing only the roll data.

yes, only one display.display(); is needed in total.

I think the issue with the updates freezing might be related to a memory issue. I get a message after uploading...

"Global variables use 1695 bytes (82%) of dynamic memory, leaving 353 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur."

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.