#include <Arduino.h>
#include <IRremote.h>
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp>
#define DISABLE_CODE_FOR_RECEIVER // Disables restarting receiver after each send. Saves 450 bytes program memory and 269 bytes RAM if receiving functions are not used.
//#define EXCLUDE_EXOTIC_PROTOCOLS // Saves around 240 bytes program memory if IrSender.write is used
//#define SEND_PWM_BY_TIMER // Disable carrier PWM generation in software and use (restricted) hardware PWM.
//#define USE_NO_SEND_PWM // Use no carrier PWM, just simulate an active low receiver signal. Overrides SEND_PWM_BY_TIMER definition
//#define NO_LED_FEEDBACK_CODE // Saves 566 bytes program memory
//#define USE_OPEN_DRAIN_OUTPUT_FOR_SEND_PIN // Use or simulate open drain output mode at send pin. Attention, active state of open drain is LOW, so connect the send LED between positive supply and send pin!
#define DELAY_AFTER_SEND 2000
#define DELAY_AFTER_LOOP 5000
#define ONOFF 34
#define nut2 23
#define nut3 11
#define nut4 12
#define IR_SEND_PIN 13
// #if defined(IR_SEND_PIN)
// IrSender.begin(); // Start with IR_SEND_PIN as send pin and enable feedback LED at default feedback LED pin
int button1 = 4;
int button2 = 5;
int button3 = 6;
int button4 = 7;
int timer;
int IRSenderPin = 13;
// IrSender irsend;
bool button1State = false;
bool button2State = false;
bool button3State = false;
bool button4State = false;
unsigned long tStart;
unsigned long tNow;
unsigned long debounceDelay = 50; // Thời gian chống nhiễu
unsigned long pressDuration = 1000;
bool lastButton1State = HIGH; // Trạng thái trước đó của nút 1 -- Nhằm chống hiện tượng bouncing của button khi ấn 1 lần nhấn nhả bị lặp tín hiệu
bool lastButton2State = HIGH;
bool lastButton3State = HIGH;
bool lastButton4State = HIGH;
uint16_t sAddress = 0xF484;
uint8_t sCommand = 0x34;
uint16_t s16BitCommand = 0xB;
uint8_t sRepeats = 0;
uint64_t hexCode[4] = {0x212FD02F, 0x212F708F, 0x212F30CF, 0x212F32CD} ;
uint16_t ONOFFrawData[203] = {8944, 4568, 512, 602, 530, 602, 528, 1752, 510, 600, 530, 602, 530, 602, 530, 600, 530, 1752, 510, 600, 530, 602, 530, 1752, 532, 578, 530, 1750, 510, 1752, 508, 1752, 508, 1752, 510, 1750, 508, 1752, 510, 602, 528, 1752, 508, 602, 530, 602, 530, 602, 528, 602, 530, 602, 528, 602, 528, 1752, 508, 604, 526, 1752, 508, 1750, 508, 1752, 508, 1752, 508, 41076, 8940, 4570, 506, 606, 524, 606, 526, 1752, 508, 606, 526, 608, 524, 606, 526, 606, 524, 1754, 506, 606, 524, 608, 524, 1754, 506, 608, 524, 1754, 508, 1752, 506, 1754, 506, 1754, 508, 1752, 508, 1752, 508, 606, 526, 1752, 506, 606, 526, 606, 524, 606, 524, 606, 524, 606, 524, 606, 526, 1752, 508, 606, 524, 1754, 508, 1752, 508, 1752, 508, 1752, 508, 41074, 8940, 4570, 508, 604, 528, 604, 526, 1752, 508, 604, 528, 604, 526, 604, 526, 604, 526, 1752, 506, 604, 526, 606, 526, 1752, 508, 604, 526, 1752, 508, 1752, 508, 1752, 506, 1754, 506, 1752, 506, 1752, 506, 606, 524, 1752, 508, 606, 526, 604, 526, 606, 526, 606, 524, 606, 524, 606, 526, 1754, 506, 606, 524, 1754, 506, 1754, 506, 1754, 506, 1756, 506}; // EPSON (Repeat) 212FD02F
uint16_t ESCrawData[203] = {8940, 4554, 524, 606, 526, 604, 526, 1756, 504, 606, 526, 606, 526, 604, 526, 606, 526, 1756, 504, 606, 526, 606, 526, 1756, 504, 606, 526, 1756, 504, 1756, 504, 1756, 502, 1758, 506, 602, 526, 1756, 504, 1756, 504, 1756, 502, 606, 526, 606, 524, 606, 526, 604, 528, 1754, 506, 606, 526, 606, 526, 606, 526, 1736, 524, 1734, 526, 1734, 524, 1736, 524, 41052, 8956, 4552, 524, 606, 524, 608, 524, 1734, 524, 606, 524, 606, 526, 606, 524, 608, 524, 1754, 506, 606, 524, 606, 524, 1736, 524, 606, 526, 1736, 522, 1754, 506, 1754, 504, 1754, 506, 606, 526, 1754, 504, 1756, 504, 1754, 506, 606, 526, 606, 524, 606, 526, 606, 526, 1754, 506, 606, 526, 606, 526, 606, 526, 1756, 504, 1734, 526, 1754, 508, 1754, 504, 41054, 8956, 4550, 526, 606, 526, 606, 524, 1736, 524, 606, 526, 606, 524, 606, 526, 606, 524, 1734, 524, 606, 526, 606, 526, 1734, 526, 606, 524, 1734, 526, 1734, 524, 1736, 526, 1734, 526, 606, 526, 1734, 526, 1734, 526, 1734, 526, 606, 524, 606, 524, 606, 526, 606, 526, 1734, 526, 606, 526, 606, 526, 606, 526, 1734, 524, 1736, 524, 1736, 524, 1736, 524}; // EPSON (Repeat) 212F708F
uint16_t MENUrawData[203] = {8960, 4572, 506, 604, 526, 606, 526, 1756, 504, 604, 526, 606, 526, 606, 526, 604, 526, 1756, 504, 606, 526, 604, 526, 1756, 504, 606, 526, 1758, 504, 1756, 504, 1756, 504, 1756, 504, 606, 528, 604, 526, 1756, 504, 1756, 504, 604, 526, 606, 526, 604, 526, 604, 526, 1756, 504, 1756, 504, 606, 526, 604, 528, 1754, 504, 1756, 504, 1756, 504, 1756, 504, 41050, 8960, 4550, 526, 604, 526, 606, 526, 1754, 504, 606, 526, 606, 526, 604, 526, 606, 526, 1754, 504, 606, 526, 606, 524, 1754, 506, 606, 524, 1756, 506, 1732, 526, 1734, 526, 1734, 526, 606, 526, 606, 526, 1736, 524, 1734, 526, 606, 526, 606, 524, 606, 526, 606, 526, 1734, 524, 1734, 524, 606, 526, 606, 526, 1734, 526, 1734, 526, 1734, 526, 1736, 524, 41064, 8934, 4570, 504, 606, 526, 604, 526, 1754, 504, 606, 524, 606, 524, 606, 526, 606, 526, 1754, 504, 606, 526, 604, 526, 1754, 504, 606, 526, 1754, 506, 1754, 506, 1754, 506, 1754, 506, 606, 524, 606, 526, 1754, 506, 1754, 504, 606, 526, 606, 526, 606, 524, 606, 526, 1754, 504, 1754, 506, 606, 526, 606, 526, 1754, 504, 1754, 506, 1754, 506, 1754, 506}; // EPSON (Repeat) 212F30CF
uint16_t ENTERrawData[203] = {8960, 4568, 508, 604, 526, 606, 526, 1754, 506, 604, 526, 606, 526, 604, 528, 604, 526, 1756, 504, 604, 526, 606, 526, 1756, 504, 604, 526, 1754, 506, 1758, 500, 1756, 504, 1754, 504, 606, 524, 606, 526, 1754, 504, 1756, 504, 606, 524, 606, 524, 1754, 504, 606, 524, 1754, 506, 1736, 524, 608, 524, 606, 524, 1754, 506, 1754, 504, 608, 524, 1754, 506, 41060, 8932, 4568, 506, 608, 524, 608, 522, 1754, 506, 606, 524, 606, 524, 606, 524, 606, 524, 1754, 506, 606, 524, 606, 526, 1752, 506, 606, 526, 1752, 506, 1754, 504, 1754, 504, 1754, 506, 606, 526, 606, 524, 1754, 506, 1754, 506, 606, 524, 608, 524, 1754, 506, 606, 524, 1754, 506, 1754, 504, 608, 524, 606, 524, 1754, 506, 1754, 506, 606, 524, 1754, 504, 41046, 8956, 4548, 526, 604, 526, 604, 526, 1734, 526, 604, 526, 604, 526, 604, 526, 604, 526, 1734, 526, 606, 526, 604, 526, 1734, 526, 604, 526, 1732, 526, 1732, 526, 1732, 526, 1734, 526, 604, 526, 604, 528, 1732, 526, 1734, 526, 604, 526, 606, 526, 1734, 526, 604, 528, 1734, 526, 1734, 526, 604, 526, 604, 526, 1734, 524, 1734, 526, 604, 526, 1736, 524}; // EPSON (Repeat) 212F32CD
void wakeup(){
timer=0;
}
void irSendNECblk( uint32_t code, uint8_t irLedPin, uint8_t freqKhz, bool inverted = false, bool invertedBitOrder = false ) {
uint16_t NecBurstUnit = ( freqKhz * 562L) / 1000L ; // IR carrier waves for 1 NEC mark or 1 NEC space( bit 0).
uint8_t carrierPeriodUs = (int16_t)1000 / freqKhz ;
uint8_t * codeSplit ;
codeSplit = ( uint8_t* ) &code ; // treat uint32_t as byte array ;
auto xmit = [irLedPin, inverted, carrierPeriodUs ]( bool isOn, uint16_t waves ) {
// send carrier burst. on = IR mark, otherwise IR space (filler)
uint32_t burstStartUs = micros() ;
for ( uint16_t i = 0 ; i < waves ; i ++ ) {
digitalWrite( irLedPin , isOn != inverted ? HIGH : LOW ) ; // Xor. carrier mark or filler
delayMicroseconds( carrierPeriodUs / 3 ) ; // 33% duty cycle
digitalWrite( irLedPin, inverted ? HIGH : LOW ) ; // carrier space
while ( micros() - ( burstStartUs + ( i * carrierPeriodUs ) ) < carrierPeriodUs ) ; // idle until end of wave(i)
} ;
} ;
pinMode( irLedPin, OUTPUT ) ;
xmit( true , NecBurstUnit * 16 ) ; // header mark 9000 us
xmit( false , NecBurstUnit * 8 ) ; // header space 4500 us
for ( uint8_t i = 0 ; i < 32 ; i ++ ) { // 32 bits
xmit( true , NecBurstUnit ) ; // NEC mark
uint8_t codeByte = !invertedBitOrder ? 3 - i / 8 /*MSB*/ : i/8 /*LSB*/ ;
uint8_t codeBit = !invertedBitOrder ? 7 - i % 8 /*MSB*/ : i % 8 /*LSB*/ ;
xmit( false, bitRead( *(codeSplit + codeByte) , codeBit ) == 1 ? NecBurstUnit * 3 : NecBurstUnit ) ; // NEC space(0) 562us or NEC space(1) ~1675us
}
xmit( true , NecBurstUnit ) ; // terminator
} // end of irSendNECblk()
void senddata_irremote (uint32_t data, uint16_t rawData[]){
Serial.println("da an nut");
Serial.println( data, HEX ) ;
// irSendNECblk( data , 13, 38 ) ;
// IrSender.sendNEC2(data, 16);
Serial.flush();
// IrSender.sendNEC(0xF484, 0xB, data);
IrSender.sendNEC(sAddress, sCommand, sRepeats);
IrSender.sendNECRaw(data, sRepeats);
// const uint16_t irSignal[] = { 9000, 4500/*Start bit*/, 560, 560, 560, 560, 560, 1690, 560,
// 560/*0010 0x4 of 16 bit address LSB first*/, 560, 560, 560, 560, 560, 560, 560, 560/*0000*/, 560, 1690, 560, 1690,
// 560, 560, 560, 1690/*1101 0xB*/, 560, 1690, 560, 1690, 560, 1690, 560, 1690/*1111*/, 560, 560, 560, 560, 560, 560,
// 560, 1690/*0001 0x08 of command LSB first*/, 560, 560, 560, 560, 560, 560, 560, 560/*0000 0x00*/, 560, 1690, 560,
// 1690, 560, 1690, 560, 560/*1110 Inverted 8 of command*/, 560, 1690, 560, 1690, 560, 1690, 560,
// 1690/*1111 inverted 0 of command*/, 560 /*stop bit*/};
IrSender.sendRaw(rawData, sizeof(rawData) / sizeof(rawData[0]), NEC_KHZ);
Serial.println( "done" ) ;
delay(DELAY_AFTER_SEND);
// while (1) delay(1000) ;
}
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
IrSender.begin(IRSenderPin);
tStart = millis();
Serial.begin( 9600 ) ;
Serial.println( "Bam nut di Luan" );
}
void loop() {
int button1Status = digitalRead(button1);
int button2Status = digitalRead(button2);
int button3Status = digitalRead(button3);
int button4Status = digitalRead(button4);
tNow = millis();
// run
// Serial.println(String(IR_SEND_PIN));
if (tNow - tStart >= pressDuration){
if (button1Status == LOW && lastButton1State == HIGH){
Serial.println("nut 1");
senddata_irremote(hexCode[0], ONOFFrawData);
tNow = tStart;
// lastButton1State = button1Status;
}
else if (button2Status == LOW && lastButton2State == HIGH){
Serial.println("nut 2");
senddata_irremote(hexCode[1], ESCrawData);
tNow = tStart;
// lastButton2State = button2Status;
}
else if (button3Status == LOW && lastButton3State == HIGH){
Serial.println("nut 3");
senddata_irremote(hexCode[2], MENUrawData);
tNow = tStart;
// lastButton3State = button3Status;
}
else if (button4Status == LOW && lastButton4State == HIGH){
Serial.println("nut 4");
senddata_irremote(hexCode[3], ENTERrawData);
tNow = tStart;
// lastButton4State = button4Status;
}
else{
Serial.println("Nothing to send");
// delay(30);
}
lastButton1State = button1Status;
lastButton2State = button2Status;
lastButton3State = button3Status;
lastButton4State = button4Status;
}
Serial.print("Button 1: ");
Serial.print(button1Status == LOW ? "Pressed" : "Released");
Serial.print("\tButton 2: ");
Serial.print(button2Status == LOW ? "Pressed" : "Released");
Serial.print("\tButton 3: ");
Serial.print(button3Status == LOW ? "Pressed" : "Released");
Serial.print("\tButton 4: ");
Serial.println(button4Status == LOW ? "Pressed" : "Released");
// // delay(100);
// attachInterrupt(0, wakeup, LOW);
// while (timer < 1000) {
// if (button1Status == LOW) {
// Serial.println("nut 1");
// timer = 0;
// delay(50);
// // senddata_irremote(hexCode[0]);
// irsend.sendNEC(0x00FDA855, 32);
// }
// if (button2Status == LOW) {
// Serial.println("nut 2");
// timer = 0;
// delay(50);
// // senddata_irremote(hexCode[1]);
// irsend.sendNEC(0x00FDA86, 32);
// }
// if (button3Status == LOW) {
// Serial.println("nut 3");
// timer = 0;
// delay(50);
// // senddata_irremote(hexCode[2]);
// irsend.sendNEC(0x00FDA857, 32);
// }
// if (button4Status == LOW) {
// Serial.println("nut 4");
// timer = 0;
// delay(50);
// // senddata_irremote(hexCode[3]);
// irsend.sendNEC(0x00FDA858, 32);
// }
// }
}