Waffle Detection System - The PERFECT time to leave a Stroopwafel
Finding the perfect time to leave a stroopwaffel or any dessert on a coffee to enjoy warm!
Devices & Components
1
laptop
1
DHT11 sensor
1
20x4 LCD
1
Passive Buzzer
Software & Tools
1
python
Project description
Code
Stroopwaffel Adruino side
c
1#include <Wire.h> 2#include <LiquidCrystal_I2C.h> 3// Constants for the LCD 4LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display 5const int tempPin = A3; // analog pin for the temperature sensor 6// Constants for the buzzer 7const int buzzerPin = A1; 8// Define note frequencies (in Hz) 9#define NOTE_B0 31 10#define NOTE_C1 33 11#define NOTE_CS1 35 12#define NOTE_D1 37 13#define NOTE_DS1 39 14#define NOTE_E1 41 15#define NOTE_F1 44 16#define NOTE_FS1 46 17#define NOTE_G1 49 18#define NOTE_GS1 52 19#define NOTE_A1 55 20#define NOTE_AS1 58 21#define NOTE_B1 62 22#define NOTE_C2 65 23#define NOTE_CS2 69 24#define NOTE_D2 73 25#define NOTE_DS2 78 26#define NOTE_E2 82 27#define NOTE_F2 87 28#define NOTE_FS2 93 29#define NOTE_G2 98 30#define NOTE_GS2 104 31#define NOTE_A2 110 32#define NOTE_AS2 117 33#define NOTE_B2 123 34#define NOTE_C3 131 35#define NOTE_CS3 139 36#define NOTE_D3 147 37#define NOTE_DS3 156 38#define NOTE_E3 165 39#define NOTE_F3 175 40#define NOTE_FS3 185 41#define NOTE_G3 196 42#define NOTE_GS3 208 43#define NOTE_A3 220 44#define NOTE_AS3 233 45#define NOTE_B3 247 46#define NOTE_C4 262 47#define NOTE_CS4 277 48#define NOTE_D4 294 49#define NOTE_DS4 311 50#define NOTE_E4 330 51#define NOTE_F4 349 52#define NOTE_FS4 370 53#define NOTE_G4 392 54#define NOTE_GS4 415 55#define NOTE_A4 440 56#define NOTE_AS4 466 57#define NOTE_B4 494 58#define NOTE_C5 523 59#define NOTE_CS5 554 60#define NOTE_D5 587 61#define NOTE_DS5 622 62#define NOTE_E5 659 63#define NOTE_F5 698 64#define NOTE_FS5 740 65#define NOTE_G5 784 66#define NOTE_GS5 831 67#define NOTE_A5 880 68#define NOTE_AS5 932 69#define NOTE_B5 988 70#define NOTE_C6 1047 71#define NOTE_CS6 1109 72#define NOTE_D6 1175 73#define NOTE_DS6 1245 74#define NOTE_E6 1319 75#define NOTE_F6 1397 76#define NOTE_FS6 1480 77#define NOTE_G6 1568 78#define NOTE_GS6 1661 79#define NOTE_A6 1760 80#define NOTE_AS6 1865 81#define NOTE_B6 1976 82#define NOTE_C7 2093 83#define NOTE_CS7 2217 84#define NOTE_D7 2349 85#define NOTE_DS7 2489 86#define NOTE_E7 2637 87#define NOTE_F7 2794 88#define NOTE_FS7 2960 89#define NOTE_G7 3136 90#define NOTE_GS7 3322 91#define NOTE_A7 3520 92#define NOTE_AS7 3729 93#define NOTE_B7 3951 94#define NOTE_C8 4186 95#define NOTE_CS8 4435 96#define NOTE_D8 4699 97#define NOTE_DS8 4978 98// Notes of the Tetris theme (Korobeiniki) 99int melody[] = { 100 NOTE_E5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_A4, 101 NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, 102 NOTE_C5, NOTE_A4, NOTE_A4, NOTE_D5, NOTE_F5, NOTE_A5, NOTE_G5, NOTE_F5, 103 NOTE_E5, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_C5, 104 NOTE_D5, NOTE_E5, NOTE_C5, NOTE_A4, NOTE_A4 105}; 106// Note durations: 4 = quarter note, 8 = eighth note, etc. 107int noteDurations[] = { 108 4, 8, 8, 4, 8, 8, 4, 4, 109 4, 4, 4, 8, 8, 4, 8, 8, 110 4, 4, 4, 4, 4, 2, 4, 8, 111 8, 4, 8, 8, 4, 4, 4, 8, 112 8, 4, 8, 8, 4, 4 113}; 114void playTetrisTheme() { 115 for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) { 116 int noteDuration = 1000 / noteDurations[thisNote]; 117 tone(buzzerPin, melody[thisNote], noteDuration); 118 delay(noteDuration * 1.50); // Adding a longer pause for slower tempo 119 noTone(buzzerPin); 120 } 121} 122void setup() { 123 lcd.init(); // initialize the lcd 124 lcd.backlight(); // Turn on the LCD screen backlight 125 lcd.setCursor(0, 0); 126 lcd.print("Temp: "); 127 pinMode(buzzerPin, OUTPUT); 128 delay(2000); // Wait for the sensor to stabilize 129 // Initialize the initial temperature 130 initialTemperature = readTemperature(); 131 // Initialize serial communication at 9600 baud 132 Serial.begin(9600); 133} 134float readTemperature() { 135 int tempReading = analogRead(tempPin); 136 float voltage = tempReading * (5.0 / 1023.0); 137 return voltage * 100.0; // LM35 outputs 10mV per degree Celsius 138} 139float initialTemperature = 0.0; 140int messageCount = 0; 141void loop() { 142 static bool musicPlayed = false; 143 144 float currentTemperature = readTemperature(); 145 // Display the temperature on the LCD 146 lcd.setCursor(6, 0); 147 lcd.print(currentTemperature); 148 lcd.print(" C "); // Add some spaces to ensure old values are cleared 149 // Check for 20% reduction 150 if (!musicPlayed && currentTemperature <= initialTemperature * 0.8) { 151 playTetrisTheme(); // Play the Tetris theme 152 musicPlayed = true; // Ensure the music is played only once 153 } 154 // Check for serial messages 155 if (Serial.available() > 0) { 156 Serial.read(); // Read the incoming byte (message) 157 messageCount++; // Increment the message count 158 } 159 // Check if message count exceeds 20 160 if (messageCount > 20) { 161 playTetrisTheme(); // Play the Tetris theme 162 messageCount = 0; // Reset the message count to prevent retriggering 163 } 164 delay(1000); // Update the temperature reading every second 165}
Movement for Waffle
python
1import cv2 2import serial 3import time 4 5# Set up serial communication with the Arduino 6# Replace '/dev/tty.usbmodem1431201' with the appropriate port name for your system 7arduino = serial.Serial(port='/dev/tty.usbmodem1431201', baudrate=9600, timeout=1) 8 9# Allow some time for the Arduino to initialize 10time.sleep(2) 11 12# Start video capture from the default webcam (0 is the default webcam index) 13cap = cv2.VideoCapture(0) 14 15# Check if the camera opened successfully 16if not cap.isOpened(): 17 print("Error: Could not open camera.") 18 exit() 19 20# Read the first frame 21ret, prev_frame = cap.read() 22if not ret: 23 print("Error: Could not read frame from camera.") 24 cap.release() 25 cv2.destroyAllWindows() 26 exit() 27 28# Convert the first frame to grayscale 29prev_frame_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY) 30prev_frame_gray = cv2.GaussianBlur(prev_frame_gray, (21, 21), 0) 31 32while True: 33 # Capture frame-by-frame 34 ret, frame = cap.read() 35 if not ret: 36 print("Error: Could not read frame.") 37 break 38 39 # Convert current frame to grayscale 40 frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 41 frame_gray = cv2.GaussianBlur(frame_gray, (21, 21), 0) 42 43 # Compute the absolute difference between the current frame and the previous frame 44 frame_diff = cv2.absdiff(prev_frame_gray, frame_gray) 45 46 # Threshold the difference to get the binary image 47 _, thresh = cv2.threshold(frame_diff, 25, 255, cv2.THRESH_BINARY) 48 thresh = cv2.dilate(thresh, None, iterations=2) 49 50 # Find contours in the binary image 51 contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 52 53 # If contours are found, movement is detected 54 if len(contours) > 0: 55 print("Movement detected!") 56 arduino.write(b'1') # Send a signal to the Arduino 57 else: 58 print("No movement") 59 60 # Display the original frame and the threshold frame (optional) 61 cv2.imshow('Frame', frame) 62 cv2.imshow('Threshold', thresh) 63 64 # Update the previous frame 65 prev_frame_gray = frame_gray.copy() 66 67 # Press 'q' to quit 68 if cv2.waitKey(1) == ord('q'): 69 break 70 71# Release capture and close all windows 72cap.release() 73cv2.destroyAllWindows() 74 75# Close the serial connection 76arduino.close()
Comments
Only logged in users can leave comments