0
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
// Khai báo địa chỉ I2C của màn hình LCD
LiquidCrystal_I2C lcd(0x27,16, 2);
// Khai báo chân analog A0 (cảm biến độ ẩm)
const int moisturePin = A0;
// Khai báo chân analog A1 (cảm biến nhiệt độ)
const int tempPin = A1;
// Khai báo chân analog A2 (cảm biến ánh sáng)
const int lightPin = A2;
#include <Servo.h>
Servo myservo1; // Khởi tạo đối tượng Servo
const int servoPin1 = 2; // Chân điều khiển Servo 1
Servo myservo2; // Khởi tạo đối tượng Servo
const int servoPin2 = 3; // Chân điều khiển Servo 2
Servo myservo3; // Khởi tạo đối tượng Servo
const int servoPin3 = 4; // Chân điều khiển Servo 3
Servo myservo4; // Khởi tạo đối tượng Servo
const int servoPin4 = 5; // Chân điều khiển Servo 4
int servoPos = 0; // Biến lưu trữ vị trí của Servo
// Khai báo chân điều khiển relay cho đèn (dựa trên độ ẩm)
const int lightRelayPin = 13;
// Khai báo chân điều khiển relay cho quạt
const int fanRelayPin = 12;
// Khai báo chân điều khiển relay cho đèn (dựa trên nhiệt độ)
const int tempLightRelayPin = 11;
// Ngưỡng độ ẩm quy định để mở relay (độ ẩm cao hơn sẽ mở relay)
const int moistureThreshold = 500;
// Ngưỡng nhiệt độ quy định để bật quạt
const int highTempThreshold = 300; // Nhiệt độ cao hơn sẽ bật quạt
// Ngưỡng nhiệt độ quy định để bật đèn
const int lowTempThreshold = 100; // Nhiệt độ thấp hơn sẽ bật đèn
// Ngưỡng cường độ ánh sáng quy định để thực hiện hành động
const int lightThreshold = 30; // Giá trị cường độ ánh sáng
// Biến lưu trữ trạng thái chế độ (auto hoặc manual)
bool autoMode = true;
// Khai báo đối tượng IRrecv và mã học từ remote
const int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
 // Khởi tạo cổng Serial với tốc độ 9600 bps để ghi log dữ liệu
 Serial.begin(9600);
 
 // Khởi tạo màn hình LCD
 lcd.init();
 lcd.setCursor(0,0);
 lcd.backlight(); // Bật đèn nền LCD
 lcd.display();
 
 // Đặt chân điều khiển relay là output
 pinMode(lightRelayPin, OUTPUT);
 pinMode(fanRelayPin, OUTPUT);
 pinMode(tempLightRelayPin, OUTPUT);
 
 // Kết nối Servo với chân điều khiển
 myservo1.attach(servoPin1);
 myservo2.attach(servoPin2);
 myservo3.attach(servoPin3);
 myservo4.attach(servoPin4);
 // Khởi tạo đối tượng IRrecv
 irrecv.enableIRIn();
}
void loop() {
 // Kiểm tra nếu có dữ liệu từ remote
 if (irrecv.decode(&results)) {
 // Lưu giá trị mã học vào biến code
 unsigned long code = results.value;
 Serial.println(code);
 // Xử lý dữ liệu từ remote
 if (code == 4294967295) { // Mã học của nút "Auto Mode"
 autoMode = !autoMode; // Chuyển đổi chế độ tự động
 // Hiển thị trạng thái chế độ lên LCD
 Serial.println(code);
 lcd.clear();
 lcd.setCursor(0, 0);
 if (autoMode) {
 lcd.print("Auto Mode: ON");
 } else {
 lcd.print("Auto Mode: OFF");
 }
 delay(1000); // Đợi 1 giây để tránh việc nhận các lệnh liên tiếp từ remote
 }
 irrecv.resume(); // Tiếp tục nhận dữ liệu từ remote
 }
}

Ignore the mess that I have made on the top.

I tried the script in a simulator and found out that every button I pressed gave me the same code, which is 4294967295.

Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Apr 6, 2024 at 12:54
2
  • yoir code is very cluttered ... comments are not part of the program, put some space between the code and comments Commented Apr 6, 2024 at 17:25
  • 1
    what happens if you change the 4294967295 to a 4294967294? Commented Apr 6, 2024 at 17:29

1 Answer 1

1

It could be a problem with the simulator (you don't say which simulator you are using), they aren't necessarily an accurate representation of the actual hardware. See How to simulate Arduino? for more info.

However, assuming that the simulator is working correctly...

The value 4294967295 is 0xFFFFFFFF (or MAXINT).

If you receive this MAXINT value then the remote button may be repeating.

Try this sketch (which apparently works well in TinkerCAD):

#define DECODE_NEC
#define IR_RECEIVE_PIN 3
#if !defined(RAW_BUFFER_LENGTH)
// Maximum length of raw duration buffer. Must be even. 100 supports up to 48 bit codings inclusive 1 start and
// 1 stop bit.
#define RAW_BUFFER_LENGTH 100 \
// #define RAW_BUFFER_LENGTH 112 // MagiQuest requires 112 bytes.
#endif
#define EXCLUDE_UNIVERSAL_PROTOCOLS // Saves up to 1000 bytes program space.
#define EXCLUDE_EXOTIC_PROTOCOLS // saves around 650 bytes program space if all other protocols are active
#include <IRremote.hpp>
void setup()
{ 
 Serial.begin(115200);
#if defined(__AVR_ATmega32U4__) || defined(SERIAL_PORT_USBVIRTUAL) || defined(SERIAL_USB) || defined(ARDUINO_attiny3217)
 delay(1000);
#endif
 Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
 Serial.println(F("Enabling IRin..."));
 IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
 Serial.print(F("Ready to receive IR signals of protocols: "));
 printActiveIRProtocols(&Serial);
 Serial.print(F("at pin "));
 Serial.println(IR_RECEIVE_PIN);
 
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8,OUTPUT);
 
}
void loop() {
 
 if (IrReceiver.decode()) {
 //IrReceiver.printIRResultShort(&Serial);
 switch(IrReceiver.decodedIRData.command) { 
 case 0x10: Serial.println("1"); // Button 1
 digitalWrite(5,HIGH);
 break;
 
 case 0x11: Serial.println("2"); // Button 2 
 digitalWrite(5,LOW);
 break; 
 
 case 0x12: Serial.println("3"); // Button 3
 digitalWrite(6,HIGH);
 break;
 
 case 0x14: Serial.println("4"); // Button 4
 digitalWrite(6,LOW);
 break;
 
 case 0x15: Serial.println("5"); // Button 5
 digitalWrite(7,HIGH);
 break; 
 
 case 0x16: Serial.println("6"); // Button 6
 digitalWrite(7,LOW);
 break; 
 
 
 default: Serial.println(IrReceiver.decodedIRData.command); 
 } 
 IrReceiver.resume(); // Receive the next value
 } 
}

If this sketch works, and it verifies that your hardware is OK, then you can adapt this to make your own sketch work.


References

answered Apr 6, 2024 at 13:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.