I'm new to this and i'm having the hardest time getting readings from the accelerometer when I have data going to the OLED. None of my pins are crossing so I'm not really sure the issue. Both are using SPI but I've assigned all their own pins.
I'm using an adafruit LIS3DH as well as a sparkfun micro oled breakout
Does something in my testing code look off? Is there a better way to approach this?
// Basic demo for accelerometer readings from Adafruit LIS3DH
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include <SFE_MicroOLED.h> // Include the SFE_MicroOLED library
// Used for software SPI
#define LIS3DH_CLK 12 //SCL(SCK)
#define LIS3DH_MISO 6 //SDO
#define LIS3DH_MOSI 2 //SDA(SDI)
// Used for hardware & software SPI
#define LIS3DH_CS 10 //CS
//////////////////////////
// MicroOLED Definition //
//////////////////////////
#define PIN_RESET 9 // Connect RST to pin 9
#define PIN_DC 5 // Connect DC to pin 8
#define PIN_CS 3 // Connect CS to pin 10
#define DC_JUMPER 0
// software SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK);
// hardware SPI
//Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);
// I2C
//Adafruit_LIS3DH lis = Adafruit_LIS3DH();
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif
void setup(void) {
#ifndef ESP8266
while (!Serial); // will pause Zero, Leonardo, etc until serial console opens
#endif
Serial.begin(9600);
Serial.println("LIS3DH test!");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1);
}
Serial.println("LIS3DH found!");
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lis.getRange());
Serial.println("G");
oled.begin();
oled.begin();
oled.clear(ALL);
}
void loop() {
lis.read(); // get X Y and Z data at once
// Then print out the raw data
Serial.print("X: "); Serial.print(lis.x);
Serial.print(" \tY: "); Serial.print(lis.y);
Serial.print(" \tZ: "); Serial.print(lis.z);
/* Or....get a new sensor event, normalized */
sensors_event_t event;
lis.getEvent(&event);
printTitle("Hello", 1);
oled.display();
/* Display the results (acceleration is measured in m/s^2) */
Serial.print("\t\tX: "); Serial.print(event.acceleration.x);
Serial.print(" \tY: "); Serial.print(event.acceleration.y);
Serial.print(" \tZ: "); Serial.print(event.acceleration.z);
Serial.println(" m/s^2 ");
Serial.println();
delay(200);
}
void printTitle(String title, int font)
{
int middleX = oled.getLCDWidth() / 2;
int middleY = oled.getLCDHeight() / 2;
oled.clear(PAGE);
oled.setFontType(font);
// Try to set the cursor in the middle of the screen
oled.setCursor(middleX - (oled.getFontWidth() * (title.length()/2)),
middleY - (oled.getFontWidth() / 2));
// Print the title:
oled.print(title);
oled.display();
oled.clear(PAGE);
}