// C++ code
//
/*
LiquidCrystal Library - Hello World
Demonstrates the use of a 16x2 LCD display.
The LiquidCrystal library works with all LCD
displays that are compatible with the Hitachi
HD44780 driver. There are many of them out
there, and you can usually tell them by the
16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008 by David
A. Mellis
library modified 5 Jul 2009 by Limor Fried
(http://www.ladyada.net)
example added 9 Jul 2009 by Tom Igoe
modified 22 Nov 2010 by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
#include <LiquidCrystal.h>
int seconds = 0;
// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#include <IRremote.h>
// Pin definitions
#define PIN_RECEIVER 2 // Signal Pin of IR receiver
#define RELAY_PIN 3
#define TRIG_PIN 4
#define ECHO_PIN 5
// Constants
#define TIMEOUT_DURATION 10000 // 10 seconds timeout
#define CAN_DISTANCE_THRESHOLD 10 // Distance in cm to detect a can
// Variables
IRrecv receiver(PIN_RECEIVER);
decode_results results;
bool motorState = false;
unsigned long lastCanDetectedTime = 0;
int crushedCanCount = 0;
void setup() {
// Initialize serial for debugging
Serial.begin(9600);
receiver.enableIRIn(); // Start the receiver
// Initialize relay pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
// Initialize ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Can Crusher");
lcd.setCursor(0, 1);
lcd.print("Count: 0");
}
void loop() {
// Check for IR remote input
if (receiver.decode()) {
Serial.print("IR Value:");
Serial.println(receiver.decodedIRData.command);
if (receiver.decodedIRData.command == 162) { // Example HEX code for a specific button
motorState = !motorState; // Toggle motor state
digitalWrite(RELAY_PIN, motorState ? HIGH : LOW); // Turn relay on/off
lastCanDetectedTime = millis(); // Reset timeout timer
}
receiver.resume(); // Receive the next value
}
// Check ultrasonic sensor for can detection
if (motorState) {
long duration, distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2; // Calculate distance in cm
if (distance < CAN_DISTANCE_THRESHOLD) {
crushedCanCount++; // Increment crushed can count
lastCanDetectedTime = millis(); // Reset timeout timer
// Update LCD display
lcd.setCursor(7, 1);
lcd.print(crushedCanCount);
}
// Check for timeout
if (millis() - lastCanDetectedTime > TIMEOUT_DURATION) {
motorState = false; // Turn off motor
digitalWrite(RELAY_PIN, LOW); // Turn off relay
}
}
// Small delay to avoid overloading the loop
delay(100);
}