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