#include <IRremote.h>
#define IR_RECEIVE_PIN 3
#define BUZZER_PIN 11
#define SERVO_PIN 9
#define IR_CODE_OPEN_GARBAGE 82
// IR Remote button codes:
#define UP_ARROW 96
#define DOWN_ARROW 97
#define LEFT_ARROW 101
#define RIGHT_ARROW 98
#define RIGHT_ARROW_1 1728 // This seems unusual, make sure your remote sends this code
#define MIDDLE_ARROW 104
#define UP_VOLUME 7
#define DOWN_VOLUME 11
int CloseAngle = 120, OpenAngle = 40, OpenHalfAngle = 90;
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(SERVO_PIN);
IrReceiver.begin(IR_RECEIVE_PIN);
pinMode(BUZZER_PIN, OUTPUT);
// openGarbage(); // Uncomment this line for testing servo movement
}
void loop() {
handle_IR(); // Continuously check for IR signals
}
void openGarbage() {
// Move servo from closed to open position
for (int angle = CloseAngle; angle > OpenAngle; angle--) {
myservo.write(angle);
delay(20);
}
// Move servo back to closed position
for (int angle = OpenAngle; angle <= CloseAngle; angle++) {
myservo.write(angle);
delay(20);
}
}
void handle_IR() {
if (IrReceiver.decode()) {
Serial.println("Receiver IR Code");
Serial.println(IrReceiver.decodedIRData.command); // Debugging: Print received IR code
IrReceiver.resume(); // Prepare for next IR signal
if (IrReceiver.decodedIRData.command == IR_CODE_OPEN_GARBAGE) {
openGarbage();
Serial.println("OPEN THE GARBAGE");
}
}
}
void BEEP() {
// Simple beep function for feedback
digitalWrite(BUZZER_PIN, HIGH);
delay(50);
digitalWrite(BUZZER_PIN, LOW);
delay(50);
}
#include <Servo.h>
Servo myservo; // Create a servo object
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch(); // Create an RF switch object
// Define pin connections
#define RF433_PIN 2 // RF receiver is connected to digital pin 2
#define BUZZER_PIN 11 // Buzzer is connected to pin 11
#define SERVO_PIN 9 // Servo motor is connected to pin 9
#define RF_CODE_OPEN_GARBAGE 12345678 // RF code to open the garbage lid
// Define servo angles for different positions
int CloseAngle = 120; // Angle when the garbage lid is closed
int OpenAngle = 40; // Angle when the garbage lid is fully open
int OpenHalfAngle = 90; // Halfway open (not used in this code)
long ReceivedValue; // Variable to store received RF code
void setup() {
Serial.begin(9600); // Initialize serial communication
myservo.attach(SERVO_PIN); // Attach servo to the specified pin
mySwitch.enableReceive(0); // Enable RF433 receiver on interrupt 0 (digital pin 2)
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as an output
// openGarbage(); // Uncomment this line to test servo movement during startup
}
void loop() {
getRF433(); // Continuously check for RF signals
handleRfCode(); // Handle received RF commands
}
/**
* Handles the received RF code and performs actions accordingly.
*/
void handleRfCode() {
switch (ReceivedValue) {
case RF_CODE_OPEN_GARBAGE: // If received code matches the predefined one
Serial.println("OPEN the Garbage"); // Debug message
openGarbage(); // Open the garbage lid
break;
}
}
/**
* Receives RF433 signal and stores the received code.
*/
void getRF433() {
ReceivedValue = 0; // Reset the received value
if (mySwitch.available()) { // Check if a new RF signal is available
if (mySwitch.getReceivedValue()) { // If a valid signal is received
ReceivedValue = mySwitch.getReceivedValue(); // Store the received code
}
mySwitch.resetAvailable(); // Reset RF receiver for the next signal
}
delay(5); // Small delay to prevent excessive CPU usage
if (ReceivedValue != 0) {
Serial.println(ReceivedValue); // Print received code for debugging
}
}
/**
* Opens and then closes the garbage lid using a servo motor.
*/
void openGarbage() {
// Move servo from closed to open position
for (int angle = CloseAngle; angle > OpenAngle; angle--) {
myservo.write(angle);
delay(20); // Small delay for smooth movement
}
// Move servo back to closed position
for (int angle = OpenAngle; angle <= CloseAngle; angle++) {
myservo.write(angle);
delay(20);
}
}
/**
* Generates a short beep sound using the buzzer.
*/
void BEEP() {
digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
delay(50); // Keep it on for 50ms
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
delay(50); // Short pause before the next action
}