Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Eamon Magd
Published © GPL3+

Waffle Detection System - The PERFECT time to leave a Stroop

Detect temperature changes with our Arduino-based Stroopwafel detection system, featuring sensors, an LCD display, and auditory/visual alert

BeginnerFull instructions provided2 hours33
Waffle Detection System - The PERFECT time to leave a Stroop

Things used in this project

Hardware components

DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
I2C 16x2 Arduino LCD Display Module
DFRobot I2C 16x2 Arduino LCD Display Module
×1
Buzzer
Buzzer
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

arduino configuration

Code

Code for arduino side

C Header File
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Constants for the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int tempPin = A3; // analog pin for the temperature sensor

// Constants for the buzzer
const int buzzerPin = A1;

// Define note frequencies (in Hz)
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

// Notes of the Tetris theme (Korobeiniki)
int melody[] = {
  NOTE_E5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_A4,
  NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5,
  NOTE_C5, NOTE_A4, NOTE_A4, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_G5, NOTE_F5,
  NOTE_E5, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_C5,
  NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4
};

// Note durations: 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
  4, 8, 8, 4, 8, 8, 4, 4,
  4, 4, 4, 8, 8, 4, 8, 8,
  4, 4, 4, 4, 4, 2, 4, 8,
  8, 4, 8, 8, 4, 4, 4, 8,
  8, 4, 8, 8, 4, 4
};

void playTetrisTheme() {
  for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote], noteDuration);
    delay(noteDuration * 1.50); // Adding a longer pause for slower tempo
    noTone(buzzerPin);
  }
}

void setup() {
  lcd.init();         // initialize the lcd
  lcd.backlight();    // Turn on the LCD screen backlight
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  pinMode(buzzerPin, OUTPUT);
  delay(2000); // Wait for the sensor to stabilize

  // Initialize the initial temperature
  initialTemperature = readTemperature();

  // Initialize serial communication at 9600 baud
  Serial.begin(9600);
}

float readTemperature() {
  int tempReading = analogRead(tempPin);
  float voltage = tempReading * (5.0 / 1023.0);
  return voltage * 100.0; // LM35 outputs 10mV per degree Celsius
}

float initialTemperature = 0.0;
int messageCount = 0;

void loop() {
  static bool musicPlayed = false;
 
  float currentTemperature = readTemperature();

  // Display the temperature on the LCD
  lcd.setCursor(6, 0);
  lcd.print(currentTemperature);
  lcd.print(" C   "); // Add some spaces to ensure old values are cleared

  // Check for 20% reduction
  if (!musicPlayed && currentTemperature <= initialTemperature * 0.8) {
    playTetrisTheme(); // Play the Tetris theme
    musicPlayed = true; // Ensure the music is played only once
  }

  // Check for serial messages
  if (Serial.available() > 0) {
    Serial.read(); // Read the incoming byte (message)
    messageCount++; // Increment the message count
  }

  // Check if message count exceeds 20
  if (messageCount > 20) {
    playTetrisTheme(); // Play the Tetris theme
    messageCount = 0; // Reset the message count to prevent retriggering
  }

  delay(1000); // Update the temperature reading every second
}

python side

Python
import cv2
import serial
import time

# Set up serial communication with the Arduino
# Replace '/dev/tty.usbmodem1431201' with the appropriate port name for your system
arduino = serial.Serial(port='/dev/tty.usbmodem1431201', baudrate=9600, timeout=1)

# Allow some time for the Arduino to initialize
time.sleep(2)

# Start video capture from the default webcam (0 is the default webcam index)
cap = cv2.VideoCapture(0)

# Check if the camera opened successfully
if not cap.isOpened():
    print("Error: Could not open camera.")
    exit()

# Read the first frame
ret, prev_frame = cap.read()
if not ret:
    print("Error: Could not read frame from camera.")
    cap.release()
    cv2.destroyAllWindows()
    exit()

# Convert the first frame to grayscale
prev_frame_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
prev_frame_gray = cv2.GaussianBlur(prev_frame_gray, (21, 21), 0)

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    if not ret:
        print("Error: Could not read frame.")
        break

    # Convert current frame to grayscale
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame_gray = cv2.GaussianBlur(frame_gray, (21, 21), 0)

    # Compute the absolute difference between the current frame and the previous frame
    frame_diff = cv2.absdiff(prev_frame_gray, frame_gray)

    # Threshold the difference to get the binary image
    _, thresh = cv2.threshold(frame_diff, 25, 255, cv2.THRESH_BINARY)
    thresh = cv2.dilate(thresh, None, iterations=2)

    # Find contours in the binary image
    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # If contours are found, movement is detected
    if len(contours) > 0:
        print("Movement detected!")
        arduino.write(b'1')  # Send a signal to the Arduino
    else:
        print("No movement")

    # Display the original frame and the threshold frame (optional)
    cv2.imshow('Frame', frame)
    cv2.imshow('Threshold', thresh)

    # Update the previous frame
    prev_frame_gray = frame_gray.copy()

    # Press 'q' to quit
    if cv2.waitKey(1) == ord('q'):
        break

# Release capture and close all windows
cap.release()
cv2.destroyAllWindows()

# Close the serial connection
arduino.close()

Credits

Eamon Magd
4 projects • 2 followers
Hi, I’m Eamon! An inventor, Computer Scientist and gamer, with an endless list of ideas. I have a yellow cockatiel.
Contact

Comments

Please log in or sign up to comment.