// Install from the Library Manager: // "Adafruit SSD1306" and "Adafruit GFX" // (the MPU6050 is read with Wire alone) #include #include #include const int BUZZER_PIN = 8; // The MPU6050's address (AD0 free: 0x68) const byte MPU_ADDR = 0x68; // If X or Y is not zero on a surface you // know is level, put the angle shown here const float OFFSET_X = 0.0; const float OFFSET_Y = 0.0; // Bubble going the wrong way? FLIP turns // an axis round, SWAP trades X and Y const bool FLIP_X = false; const bool FLIP_Y = false; const bool SWAP_AXES = false; // This close (degrees) counts as level; // it stops once tilted by OUT const float LEVEL_IN = 0.5; const float LEVEL_OUT = 1.0; // The level's sizes (pixels) const int CX = 31; const int CY = 31; const int RING_R = 30; const int TARGET_R = 7; const int BUBBLE_R = 5; const int TRAVEL = 23; // How many pixels one degree moves it const float SCALE = 2.0; // 128x64 screen; I2C address mostly 0x3C Adafruit_SSD1306 oled(128, 64, &Wire, -1); // Smoothed tilts (degrees) float tiltX = 0; float tiltY = 0; bool level = false; bool mpuOk = false; bool firstReading = true; int beepsLeft = 0; unsigned long beepAt = 0; unsigned long lastRead = 0; unsigned long lastDraw = 0; unsigned long lastPrint = 0; void writeRegister(byte reg, byte value) { Wire.beginTransmission(MPU_ADDR); Wire.write(reg); Wire.write(value); Wire.endTransmission(); } // Wakes it; true if the MPU6050 answers bool startMpu() { Wire.beginTransmission(MPU_ADDR); // Power register 0: out of sleep mode Wire.write(0x6B); Wire.write(0); if (Wire.endTransmission() != 0) return false; // A filter against shaking (~44 Hz) writeRegister(0x1A, 3); return true; } // Two bytes into one number (high first) int read16() { byte high = Wire.read(); byte low = Wire.read(); return (int16_t)((high << 8) | low); } // Reads the accelerometer's three axes; // false on an error bool readAccel(float &ax, float &ay, float &az) { Wire.beginTransmission(MPU_ADDR); Wire.write(0x3B); if (Wire.endTransmission(false) != 0) return false; if (Wire.requestFrom((int)MPU_ADDR, 6) != 6) return false; ax = read16(); ay = read16(); az = read16(); return true; } void readTilt() { float ax, ay, az; if (!readAccel(ax, ay, az)) { mpuOk = false; firstReading = true; return; } // Angle from gravity's share on each // axis: the higher the axis, the bigger float x = atan2(ax, sqrt(ay * ay + az * az)) * RAD_TO_DEG; float y = atan2(ay, sqrt(ax * ax + az * az)) * RAD_TO_DEG; if (SWAP_AXES) { float t = x; x = y; y = t; } if (FLIP_X) x = -x; if (FLIP_Y) y = -y; x -= OFFSET_X; y -= OFFSET_Y; // The first reading is taken as it is // (from 0 it would pass for level) if (firstReading) { tiltX = x; tiltY = y; firstReading = false; } // Smoothing: no jittery numbers tiltX += (x - tiltX) * 0.15; tiltY += (y - tiltY) * 0.15; } void checkLevel() { float worst = max(abs(tiltX), abs(tiltY)); if (!level && worst <= LEVEL_IN) { level = true; // Two short beeps when it is level beepsLeft = 2; beepAt = millis(); Serial.println("Level!"); } else if (level && worst > LEVEL_OUT) { level = false; } } void playBeeps() { if (beepsLeft > 0 && millis() >= beepAt) { tone(BUZZER_PIN, 1800, 60); beepsLeft--; beepAt = millis() + 140; } } // The angle in big print, a small degree // sign beside it void printAngle(int x, int y, float angle) { oled.setTextSize(2); oled.setCursor(x, y); oled.print(angle, 1); int end = oled.getCursorX(); if (end + 6 <= 128) { oled.setTextSize(1); oled.setCursor(end + 1, y); // 247 is the screen's degree sign oled.write(247); } } void drawScreen() { oled.clearDisplay(); if (!mpuOk) { oled.setTextSize(1); oled.setCursor(0, 20); oled.println("No MPU6050"); oled.println("Check the wires"); oled.display(); return; } // Left: ring, target and cross lines oled.drawCircle(CX, CY, RING_R, SSD1306_WHITE); oled.drawCircle(CX, CY, TARGET_R, SSD1306_WHITE); oled.drawFastHLine(CX - RING_R, CY, RING_R - TARGET_R, SSD1306_WHITE); oled.drawFastHLine(CX + TARGET_R + 1, CY, RING_R - TARGET_R, SSD1306_WHITE); oled.drawFastVLine(CX, CY - RING_R, RING_R - TARGET_R, SSD1306_WHITE); oled.drawFastVLine(CX, CY + TARGET_R + 1, RING_R - TARGET_R, SSD1306_WHITE); // The bubble runs to the high side; cut // short so it stays inside the ring float dx = tiltX * SCALE; float dy = -tiltY * SCALE; float far = sqrt(dx * dx + dy * dy); if (far > TRAVEL) { dx = dx * TRAVEL / far; dy = dy * TRAVEL / far; } oled.fillCircle(CX + round(dx), CY + round(dy), BUBBLE_R, SSD1306_WHITE); // Right: both axes' angles oled.setTextSize(1); oled.setCursor(66, 0); oled.print("X"); printAngle(66, 9, tiltX); oled.setTextSize(1); oled.setCursor(66, 29); oled.print("Y"); printAngle(66, 38, tiltY); if (level) { oled.setTextSize(1); oled.setCursor(66, 56); // Shown while it is level oled.print("LEVEL"); } oled.display(); } void setup() { Serial.begin(9600); Wire.begin(); // Fast I2C: the screen redraws sooner Wire.setClock(400000); if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("OLED not found"); // No screen: stop here while (true) {} } oled.setTextColor(SSD1306_WHITE); // For the degree sign oled.cp437(true); mpuOk = startMpu(); if (!mpuOk) Serial.println("MPU6050 not found: check the wires."); } void loop() { unsigned long now = millis(); if (now - lastRead >= 20) { lastRead = now; // Tries a lost connection again if (!mpuOk) mpuOk = startMpu(); if (mpuOk) readTilt(); if (mpuOk) checkLevel(); } playBeeps(); if (now - lastDraw >= 100) { lastDraw = now; drawScreen(); } if (mpuOk && now - lastPrint >= 500) { lastPrint = now; Serial.print("X: "); Serial.print(tiltX, 1); Serial.print("° Y: "); Serial.print(tiltY, 1); Serial.println("°"); } }