-
-
Save JeffersGlass/29924af2805b960019974ea8772ba6b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IRLib.h> | |
//#include <Wire.h> | |
//#include <math.h> | |
boolean DEBUG = false; | |
//IR LED connected to 5V via 150ohm Resistor | |
//Cathode of IR led connected to collector of 2n2222 | |
//Emitter of 2n2222 connected to ground | |
//Base of 2n2222 connected to pin 3 via 470 ohm resistor | |
const long CANDLES_ON = 0xFF807F; // NEC(1) | |
const long CANDLES_OFF = 0xFF00FF; //NEC(1) | |
const int MESSAGE_NUM = 5; | |
const int BUTTON_PIN_ON = 11; | |
const int DEBOUNCE_TIME = 50; | |
int lastState; | |
int lastButtonState; | |
long lastButtonChangeTime = 0; | |
long heartbeatCounter = 0; | |
int pin1; | |
int pin2; | |
int lastPin1; | |
int lastPin2; | |
IRsend My_Sender; | |
void setup() | |
{ | |
lastState = 0; | |
lastButtonState = HIGH; | |
//Wire.begin(8); // join i2c bus with address #8 | |
//Wire.onReceive(receiveEvent); // register event | |
if (DEBUG) Serial.begin(38400); | |
delay(100); | |
pinMode(BUTTON_PIN_ON, INPUT_PULLUP); | |
pinMode(A4, INPUT); | |
pinMode(A5, INPUT); | |
pin1 = LOW; | |
pin2 = LOW; | |
lastPin1 = LOW; | |
lastPin2 = LOW; | |
} | |
void loop() { | |
delay(5); | |
int onButtonState = digitalRead(BUTTON_PIN_ON); | |
if (onButtonState != lastButtonState && millis() > lastButtonChangeTime + DEBOUNCE_TIME){ | |
if (onButtonState == LOW) processInput(2); | |
else processInput(0); | |
lastButtonState = onButtonState; | |
lastButtonChangeTime = millis(); | |
} | |
else{ | |
doEvent(); | |
} | |
} | |
void doEvent(){ | |
int pin1 = digitalRead(A4); | |
int pin2 = digitalRead(A5); | |
if (pin2 == LOW && (pin1 != lastPin1 || pin2 != lastPin2)){ | |
if (pin1 == LOW) processInput(0); | |
else processInput(2); | |
} | |
else if (pin2 == HIGH){ | |
if (pin1 == LOW) processInput(10); | |
else processInput(12); | |
} | |
lastPin1 = pin1; | |
lastPin2 = pin2; | |
/*while (0 < Wire.available()) { | |
byte x = Wire.read(); | |
if (DEBUG){ Serial.print(heartbeatCounter++); Serial.print(":"); Serial.print("\t\t"); Serial.println(x);} | |
if ((x != lastState && x < 10) || x >= 10){ | |
processInput(x); | |
lastState = x; | |
} | |
}*/ | |
} | |
void processInput(int x) { | |
if (x == 2 || x == 12){ | |
if (DEBUG) Serial.println("Send on"); | |
digitalWrite(13, HIGH); | |
int timesToSend = MESSAGE_NUM; | |
//if (x >= 10) timesToSend = 3; | |
for (int i = 0; i < timesToSend; i++){ | |
My_Sender.send(NEC,CANDLES_ON, 38); | |
delay(10); | |
} | |
} | |
else if (x == 0 || x == 10){ | |
if (DEBUG) Serial.println("Send off"); | |
digitalWrite(13, LOW); | |
int timesToSend = MESSAGE_NUM; | |
if (x >= 10) timesToSend = 3; | |
for (int i = 0; i < timesToSend; i++){ | |
My_Sender.send(NEC,CANDLES_OFF, 38); | |
delay(10); | |
} | |
} | |
if (x >=10) delay(150); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment