Rohan Barnwal
Published © GPL3+

Smart Truck Alarm System

From a Fun Idea to a Showcase-Ready Hardware BuildπŸŽΆπŸ’‘

BeginnerShowcase (no instructions)1 hour17
Smart Truck Alarm System

Things used in this project

Story

Read more

Schematics

Connections

Code

Code

Arduino
#include <Wire.h>
#include <U8g2lib.h>
#include "RTClib.h"
#include "DFRobotDFPlayerMini.h"

// ================= OLED SETUP =================
// OLED1 β†’ SCL=22, SDA=24
U8G2_SSD1306_128X64_NONAME_F_SW_I2C oledHour(U8G2_R0, 22, 24, U8X8_PIN_NONE);

// OLED2 β†’ SCL=23, SDA=25
U8G2_SSD1306_128X64_NONAME_F_SW_I2C oledMin(U8G2_R0, 23, 25, U8X8_PIN_NONE);

// ================= RTC =================
RTC_DS3231 rtc;

// ================= DFPLAYER =================
DFRobotDFPlayerMini df;

// ================= PINS =================
const int BUSY_PIN = 43;
const int LED_PINS[] = {34, 36, 38, 40};
const int NUM_LEDS = 4;

// ================= STATES =================
enum SystemState {
  IDLE,
  PLAY_START,
  LOOPING,
  STOPPED
};

SystemState currentState = IDLE;

// ================= FLAGS =================
bool startTriggered = false;
bool stopTriggered = false;
bool lastBusyState = HIGH;
bool ledState = false;

// ================= LOOP TRACKS =================
int loopTracks[] = {2, 4, 4};
int loopIndex = 0;

// ================= TIMINGS =================
const int START_HOUR = 12;
const int START_MIN = 50;

const int STOP_HOUR = 12;
const int STOP_MIN = 55;

// =====================================================

void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);

  Wire.begin();

  // OLED Init
  oledHour.begin();
  oledMin.begin();

  oledHour.setBusClock(50000);
  oledMin.setBusClock(50000);

  // Pin Setup
  pinMode(BUSY_PIN, INPUT);

  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(LED_PINS[i], OUTPUT);
  }

  // RTC Init
  if (!rtc.begin()) {
    Serial.println("❌ RTC NOT FOUND");
    while (1);
  }

  // DFPlayer Init
  if (!df.begin(Serial1)) {
    Serial.println("❌ DFPlayer NOT FOUND");
    while (1);
  }

  df.volume(30);
  Serial.println("βœ… System Ready");
}

// =====================================================

void loop() {
  DateTime now = rtc.now();
  int hour = now.hour();
  int minute = now.minute();

  // ===== DISPLAY TIME =====
  displayTime(hour, minute);

  // ===== START CONDITION =====
  if (hour == START_HOUR && minute == START_MIN && !startTriggered) {
    startSequence();
  }

  // ===== STOP CONDITION =====
  if (hour == STOP_HOUR && minute == STOP_MIN && !stopTriggered) {
    stopSequence();
  }

  // Reset triggers
  if (!(hour == START_HOUR && minute == START_MIN)) startTriggered = false;
  if (!(hour == STOP_HOUR && minute == STOP_MIN)) stopTriggered = false;

  // ===== DFPLAYER TRACK HANDLING =====
  handleDFPlayer();

  // ===== LED CONTROL =====
  handleLEDs();

  delay(200);
}

// =====================================================
// πŸ”Š START SEQUENCE
void startSequence() {
  Serial.println("πŸš€ START SEQUENCE");

  df.play(1);  // 0001.mp3
  currentState = PLAY_START;

  startTriggered = true;
  loopIndex = 0;
}

// =====================================================
// πŸ›‘ STOP SEQUENCE
void stopSequence() {
  Serial.println("πŸ›‘ STOP SEQUENCE");

  df.stop();
  delay(200);
  df.play(3);  // 0003.mp3

  currentState = STOPPED;
  stopTriggered = true;
}

// =====================================================
// πŸ” DFPLAYER LOGIC
void handleDFPlayer() {
  bool currentBusy = digitalRead(BUSY_PIN);

  // Detect track end (LOW β†’ HIGH)
  if (lastBusyState == LOW && currentBusy == HIGH) {

    if (currentState == PLAY_START) {
      currentState = LOOPING;
      df.play(loopTracks[loopIndex]);
    }

    else if (currentState == LOOPING) {
      loopIndex = (loopIndex + 1) % 3;

      df.play(loopTracks[loopIndex]);

      Serial.print("πŸ” Loop Track: ");
      Serial.println(loopTracks[loopIndex]);
    }
  }

  lastBusyState = currentBusy;
}

// =====================================================
// πŸ’‘ LED CONTROL
void handleLEDs() {
  if (currentState == LOOPING) {
    ledState = !ledState;

    for (int i = 0; i < NUM_LEDS; i++) {
      digitalWrite(LED_PINS[i], ledState);
    }
  } else {
    for (int i = 0; i < NUM_LEDS; i++) {
      digitalWrite(LED_PINS[i], LOW);
    }
  }
}

// =====================================================
// πŸ–₯️ OLED DISPLAY FUNCTION
void displayTime(int hour, int minute) {
  char hStr[3], mStr[3];

  sprintf(hStr, "%02d", hour);
  sprintf(mStr, "%02d", minute);

  // Hour Display
  oledHour.clearBuffer();
  oledHour.setFont(u8g2_font_logisoso32_tr);
  oledHour.drawStr((128 - oledHour.getStrWidth(hStr)) / 2, 45, hStr);
  oledHour.sendBuffer();

  // Minute Display
  oledMin.clearBuffer();
  oledMin.setFont(u8g2_font_logisoso32_tr);
  oledMin.drawStr((128 - oledMin.getStrWidth(mStr)) / 2, 45, mStr);
  oledMin.sendBuffer();
}

Credits

Rohan Barnwal
44 projects β€’ 38 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments