/*
Arduino | coding-help
Nicole Thursday, May 14, 2026 10:14 PM
hello everyone.
i am working on a project with my arduino uno.
it involves an mpu6050 and an OLED screen SSD1306.
The code was made so that when the accelerometer is tilted,
the screen says hello but I am a beginner and it will not work.
each component works when tested separately but not together.
the wired might be fried or the code because tbh i am not very
familiar with C++ or this in general.
id love if anyone can help plss i can send pictures in a second!
this is the fried code and the fried wiring. pls if anyone know
what might be wrong id appreciate it!
*/
#include <Wire.h>
#include <Servo.h>
#include <U8g2lib.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// pin constants
const int BTN_PIN = 12;
const int BUZZ_PIN = 11;
const int RED_LED_PIN = 10;
const int GRN_LED_PIN = 9;
const int SERVO_PIN = 8;
// user constants
const int LOCKED_ANGLE = 0;
const int UNLOCK_ANGLE = 90;
const unsigned long ONE_SEC = 1000;
// global variables
// servo
int servo_locked = 1;
//button
int button_state;
int last_button_state = HIGH;
//double press thing
int press_count = 0;
long firstPressTime = 0;
long doublePressTime = 400; //window
//wake-sleep
int system_awake = 0;
int tilt_threshold = 2;
unsigned long prevTime = 0;
// create objects
Servo lockServ;
Adafruit_MPU6050 mpu;
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R2);
void move_servo() {
if (servo_locked == 1) {
lockServ.write(UNLOCK_ANGLE);
servo_locked = 0;
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GRN_LED_PIN, HIGH);
} else {
lockServ.write(LOCKED_ANGLE);
servo_locked = 1;
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GRN_LED_PIN, LOW);
}
}
void show_hello() {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0, 40, "HELLO");
} while ( u8g2.nextPage() );
delay(1000);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB10_tr);
u8g2.drawStr(0, 20, "Please enter");
u8g2.drawStr(0, 40, "your password");
} while ( u8g2.nextPage() );
}
void sleep_everything() {
lockServ.write(LOCKED_ANGLE);
servo_locked = 1;
press_count = 0;
u8g2.clear();
}
void setup() {
Wire.begin();
Serial.begin(9600);
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB10_tr);
//u8g2.setFont(u8g2_font_ncenB14_tr);
if (!mpu.begin()) {
Serial.println(F("MPU6050 not found"));
for (;;); // Don't proceed, loop forever
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GRN_LED_PIN, OUTPUT);
pinMode(BUZZ_PIN, OUTPUT);
lockServ.attach(SERVO_PIN);
// initialize
lockServ.write(LOCKED_ANGLE);
digitalWrite(RED_LED_PIN, HIGH);
u8g2.firstPage();
do {
u8g2.drawStr(0, 20, "Ready!");
} while ( u8g2.nextPage() );
Serial.println(F("Ready!"));
}
void loop() {
//read accelerometer
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
//tilt i think help me
int tilted = 0;
if (a.acceleration.x > tilt_threshold) {
tilted = 1;
}
if (a.acceleration.x < -tilt_threshold) {
tilted = 1;
}
if (a.acceleration.y > tilt_threshold) {
tilted = 1;
}
if (a.acceleration.y < -tilt_threshold) {
tilted = 1;
}
if (millis() - prevTime >= ONE_SEC) {
Serial.print("X: ");
Serial.print(a.acceleration.x);
Serial.print(" Y: ");
Serial.print(a.acceleration.y);
Serial.print(" Z: ");
Serial.println(a.acceleration.z);
Serial.print("Tilted: ");
Serial.print(tilted);
Serial.print("\tAwake: ");
Serial.println(system_awake);
Serial.println();
prevTime = millis();
}
//screen wakes up
if (tilted == 1 && system_awake == 0) {
delay(300);
system_awake = 1;
show_hello();
}
// stay asleep or go to sleep
if (tilted == 0 && system_awake == 1) {
delay(500);
system_awake = 0;
sleep_everything();
}
//button works only when awake i think
if (system_awake == 1) {
button_state = digitalRead(BTN_PIN);
if (last_button_state == HIGH && button_state == LOW) {
press_count++;
if (press_count == 1) {
firstPressTime = millis();
} else if (press_count == 2) {
if (millis() - firstPressTime <= doublePressTime) {
move_servo();
} else {
tone(BUZZ_PIN, 300, 400);
}
press_count = 0;
}
last_button_state = button_state;
delay(100);
}
if (press_count == 1 && millis() - firstPressTime > doublePressTime) {
press_count = 0;
tone(BUZZ_PIN, 300, 400); //if pressed one tone goes off logic
}
last_button_state = button_state;
}
}