#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
float RateRoll, RatePitch, RateYaw;
float AccX, AccY, AccZ;
float AngleRoll, AnglePitch;
float LoopTimer;
bool countdownRunning = false;
bool countdownComplete = false;
unsigned long timer;
void gyro_signals(void) {
Wire.beginTransmission(0x68);
Wire.write(0x1A);
Wire.write(0x05);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x1C);
Wire.write(0x10);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0x68,6);
int16_t AccXLSB = Wire.read() << 8 | Wire.read();
int16_t AccYLSB = Wire.read() << 8 | Wire.read();
int16_t AccZLSB = Wire.read() << 8 | Wire.read();
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(0x8);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(0x68,6);
int16_t GyroX=Wire.read()<<8 | Wire.read();
int16_t GyroY=Wire.read()<<8 | Wire.read();
int16_t GyroZ=Wire.read()<<8 | Wire.read();
RateRoll=(float)GyroX/65.5;
RatePitch=(float)GyroY/65.5;
RateYaw=(float)GyroZ/65.5;
AccX=(float)AccXLSB/4096;
AccY=(float)AccYLSB/4096;
AccZ=(float)AccZLSB/4096;
AngleRoll=atan(AccY/sqrt(AccX*AccX+AccZ*AccZ))*1/(3.142/180);
AnglePitch=-atan(AccX/sqrt(AccY*AccY+AccZ*AccZ))*1/(3.142/180);
}
void setup() {
Serial.begin(57600);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Wire.setClock(400000);
Wire.begin();
delay(250);
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission();
pinMode(5, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay(); //the intro
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(35, 8);
display.println("WELCOME TO");
display.setCursor(15, 18);
display.println("CUBE ALARM CLOCK");
display.display();
delay(4000);
}
void loop() {
gyro_signals();
Serial.print("Acceleration X [g]= ");
Serial.print(AccX);
Serial.print(" Acceleration Y [g]= ");
Serial.print(AccY);
Serial.print(" Acceleration Z [g]= ");
Serial.println(AccZ);
delay(50);
if (AccX <= -0.9 && AccY < 0.5 && AccZ < 0.5) {
if (!countdownRunning && !countdownComplete) { // check if countdown is not already running and not already complete
countdownRunning = true;
timer = millis(); // start the timer
for (int i = 2; i >= 1; i--) {
display.clearDisplay();
display.setCursor(64, 10);
display.print(i);
display.display();
delay(1000); // countdown every 1 second
}
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500); //
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500); //
digitalWrite(5, LOW); // turn off the buzzer
countdownRunning = false;
countdownComplete = true;
}
}
else if (AccX < 0.5 && AccY <= -0.9 && AccZ < 0.5) {
if (!countdownRunning && !countdownComplete) { // check if countdown is not already running and not already complete
countdownRunning = true;
timer = millis(); // start the timer
for (int i = 4; i >= 1; i--) {
display.clearDisplay();
display.setCursor(64, 10);
display.print(i);
display.display();
delay(1000);
}
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
countdownRunning = false;
countdownComplete = true;
}
}
else if (AccX > 0.9 && AccY < 0.5 && AccZ < 0.5) {
if (!countdownRunning && !countdownComplete) { // check if countdown is not already running and not already complete
countdownRunning = true;
timer = millis(); // start the timer
for (int i = 6; i >= 1; i--) {
display.clearDisplay();
display.setCursor(64, 10);
display.print(i);
display.display();
delay(1000);
}
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
countdownRunning = false;
countdownComplete = true;
}
}
else if (AccX < 0.5 && AccY >= 0.9 && AccZ < 0.5) {
if (!countdownRunning && !countdownComplete) { // check if countdown is not already running and not already complete
countdownRunning = true;
timer = millis(); // start the timer
for (int i = 8; i >= 1; i--) {
display.clearDisplay();
display.setCursor(64, 10);
display.print(i);
display.display();
delay(1000); //
}
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
delay(500);
digitalWrite(5, HIGH); // turn on the buzzer
delay(500);
digitalWrite(5, LOW); // turn off the buzzer
countdownRunning = false;
countdownComplete = true;
}
}
else {
countdownComplete = false;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(30, 8);
display.println("Let's Start");
display.setCursor(35, 18);
display.println("The Timer");
display.display();
delay(200); //prevent the mistake
}
}