Hello im new to this an found some inclinometer builds im trying.
i made the oled screen and the gyroscope to work altrouhgt they are the chinese version.
Got them both to work on the arduino nano .
Heres my sketck wich i merged from different sites.
Hope you can help me figure out why i can see the data from the gyroscope in the oled (only shows 0)
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#if (SSD1306_LCDHEIGHT != 64)
#define SSD1306_LCDHEIGHT 64
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#include <Wire.h>
#include <SparkFun_ADXL345.h>
#define I2C_ADDR 0x3C // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
ADXL345 adxl; //variable adxl is an instance of the ADXL345 library
int x,y,z;
float xg,yg,zg;
float soh;
float toh;
float tilt;
float climb;
float angle;
float angle1;
String dir;
String dir1;
uint8_t degree[8] = {140,146,146,140,128,128,128,128};
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
const float alpha = .5;
double fXg = 0;
double fYg = 0;
double fZg = 0;
void setup(){
adxl.setRangeSetting(16);
// Digital Pins for buzzer
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC);
// init done
}
void loop(){
// Clear the buffer.
display.clearDisplay();
// text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10,0);
display.println("Max 45");display.setCursor(55,0);display.println("Warn @ 38"); //CHANGE 45/38 TO YOUR DESIRED ROLL ANGLE//
// text display tests
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10,13);display.print(angle, 0);display.print("\t");display.setCursor(65,13);display.print("Roll");
// text display tests
display.setTextSize(2);
display.setTextColor(WHITE);
if ((angle> 38) && (angle< 90));display.setCursor(47,13);display.println(dir); //CHANGE 38 TO YOUR DESIRED ROLL ANGLE//
// text display tests
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
display.setCursor(55,0);
if ((angle> 38) && (angle< 90)) display.println(" !WARNING! "); //CHANGE 38 TO YOUR DESIRED ROLL ANGLE//
if ((angle< -38) && (angle> -90)) display.println(" !WARNING! "); //CHANGE 38 TO YOUR DESIRED ROLL ANGLE//
if ((angle> 38) && (angle< 90)) tone(8,15); //CHANGE 38 TO YOUR DESIRED ROLL ANGLE//
if ((angle> 0) && (angle< 38)) noTone(8); //CHANGE 38 TO YOUR DESIRED ROLL ANGLE//
// text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10,35);
display.println("Max 45");display.setCursor(55,35);display.println("Warn @ 40"); //CHANGE 45/40 TO YOUR DESIRED PITCH ANGLE//
// text display tests
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10,50);display.print(angle1, 0);display.print("\t");display.setCursor(65,50);display.print("Pitch");
// text display tests
display.setTextSize(2);
display.setTextColor(WHITE);
if ((angle> 38) && (angle< 90));display.setCursor(47,50);display.println(dir1); //CHANGE 38 TO YOUR DESIRED PITCH ANGLE//
// text display tests
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
display.setCursor(55,35);
if ((angle1> 40) && (angle1< 90)) display.println(" !WARNING! "); //CHANGE 40 TO YOUR DESIRED PITCH ANGLE//
if ((angle1< -40) && (angle1> -90)) display.println(" !WARNING! "); //CHANGE 40 TO YOUR DESIRED PITCH ANGLE//
if ((angle1> 40) && (angle1< 90)) tone(9,15); //CHANGE 40 TO YOUR DESIRED PITCH ANGLE//
if ((angle1> 0) && (angle1< 40)) noTone(9); //CHANGE 40 TO YOUR DESIRED PITCH ANGLE//
display.setTextColor(BLACK, WHITE); // 'inverted' text
//display.println(3.141592);
display.setTextSize(2);
display.setTextColor(WHITE);
//display.print("0x");
//display.println(0xDEADBEEF, HEX);
display.display();
//delay(2000);
//display.clearDisplay();
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
Wire.begin();
setupL3G4200D(2000); // Configure L3G4200 - 250, 500 or 2000 deg/sec
delay(1500); //wait for the sensor to be ready
}
int L3G4200D_Address = 0x69; //I2C address of the L3G4200D
void getGyroValues(){
byte xMSB = readRegister(L3G4200D_Address, 0x29);
byte xLSB = readRegister(L3G4200D_Address, 0x28);
x = ((xMSB << 8) | xLSB);
byte yMSB = readRegister(L3G4200D_Address, 0x2B);
byte yLSB = readRegister(L3G4200D_Address, 0x2A);
y = ((yMSB << 8) | yLSB);
byte zMSB = readRegister(L3G4200D_Address, 0x2D);
byte zLSB = readRegister(L3G4200D_Address, 0x2C);
z = ((zMSB << 8) | zLSB);
}
int setupL3G4200D(int scale){
//From Jim Lindblom of Sparkfun's code
// Enable x, y, z and turn off power down:
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);
// If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);
// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you'd like to configure INT1
// or INT2 otherwise, consult the datasheet:
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
//CTRL_REG4 controls the full-scale range, among other things:
if(scale == 250){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
}else if(scale == 500){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
}else{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
}
// CTRL_REG5 controls high-pass filtering of outputs, use it
// if you'd like:
// writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}
void writeRegister(int deviceAddress, byte address, byte val) {
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
int readRegister(int deviceAddress, byte address){
int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // register to read
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1); // read a byte
while(!Wire.available()) {
// waiting
}
v = Wire.read();
return v;
}