/*
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 <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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 int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 32; // not enough memory for 128x64
const int OLED_RESET = -1;
const int SCREEN_ADDRESS = 0x3C;
// 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;
// create objects
Servo lockServ;
Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void move_servo() {
if (servo_locked == 1) {
lockServ.write(UNLOCK_ANGLE);
servo_locked = 0;
} else {
lockServ.write(LOCKED_ANGLE);
servo_locked = 1;
}
}
void show_hello() {
//hello
display.clearDisplay();
display.setTextSize(3);
//display.setTextColor(SSD1306_WHITE);
display.setCursor(20, 0);
display.println(F("Hello"));
display.display();
delay(1000);
//please out passcode
display.clearDisplay();
display.setTextSize(1);
//display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.println(F("Please put in"));
display.setCursor(22, 15);
display.println(F("passcode..."));
display.display();
}
void sleep_everything() {
lockServ.write(LOCKED_ANGLE);
servo_locked = 1;
press_count = 0;
display.clearDisplay();
display.display();
delay(50);
display.clearDisplay();
display.display();
}
void setup() {
Wire.begin();
Serial.begin(9600);
display.setRotation(2); // flips screen upside down
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK); // overwrite pixels
display.clearDisplay(); // clears Adafruit splash screen
display.display();
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);
display.print(F("Ready!"));
display.display();
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;
}
/*
//not working :(
Serial.print("X: ");
Serial.print(a.acceleration.x);
Serial.print(" Y: ");
Serial.println(a.acceleration.y);
Serial.print(" Z: ");
Serial.print(a.acceleration.z);
Serial.print(" tilted: ");
Serial.print(tilted);
Serial.print("awake: ");
Serial.println(system_awake);
*/
//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;
}
}