Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <IRremote.h>
- #include <EEPROM.h>
- #include <Stepper.h>
- //#include<MsTimer2.h>
- #include<Timer1.h>
- #define digit1 5 // Segment Digit 1
- #define digit2 6 // Segment Digit 2
- #define RECV_PIN 7 // IR Receiver
- #define relay 8 // RelayPin
- #define buzzer 13
- #define clock A4 // Segment Clock
- #define data A5 // Segment Data
- #define MotorPin A0 // Triac Pin
- //----------------------------------------------------------------------------------------------//
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- unsigned long key_value = 0;
- //----------------------------------------------------------------------------------------------//
- // Stepper motor
- unsigned long currentMillis;
- unsigned long previousMillis = 0;
- const long SwingInterval = 1000;
- const float STEPS_PER_REV = 16; //default 32
- const float GEAR_RED = 32; //default 64
- //const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
- const float STEPS_PER_OUT_REV = 1024;
- int StepsRequired;
- Stepper steppermotor(STEPS_PER_REV, 9, 11, 10, 12);
- bool swingFlag = false;
- int SwingState = LOW;
- bool flapIn = true;
- bool flapOut = false;
- byte PreviousSwingState; // EEPROM VAlues
- //----------------------------------------------------------------------------------------------//
- // Segment Display
- byte Digit[] = {B00010010, B11011110, B00010101, B10010100, // 0 1 2 3
- B11011000, B10110000, B00110000, B11010110, // 4 5 6 7
- B00010000, B11010000, B01110001, B00111011, // 8 9 F L
- B10110000, B00000000, B11111111, B01111100}; // S Ao Af n
- // 0 1 2 3 4 5 6 7 8 9 F L S Ao Af n
- // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- //byte symbol[] = {B00000001, B11111110};
- int unit, tens;
- volatile char digNum = 1;
- //----------------------------------------------------------------------------------------------//
- // Motor Speed Control
- int fanval[5] = {75,45,10}; // Fan different Speed
- int timeval;
- bool interruptState = false;
- boolean fan;
- byte speedCounter = 0;
- bool relayState = false;
- //----------------------------------------------------------------------------------------------//
- void setup() {
- // Serial.begin(9600);
- irrecv.enableIRIn();
- irrecv.blink13(false);
- pinMode(2, INPUT); // ZCD
- pinMode(digit1, OUTPUT);
- pinMode(digit2, OUTPUT);
- pinMode(relay, OUTPUT);
- pinMode(buzzer, OUTPUT);
- pinMode(clock, OUTPUT);
- pinMode(data, OUTPUT);
- pinMode(MotorPin, OUTPUT);
- fan = false;
- // MsTimer2::set(5, Display_Digits);
- // MsTimer2::start();
- }
- //----------------------------------------------------------------------------------------------//
- void beep() {
- digitalWrite(buzzer, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(100); // wait for a second
- digitalWrite(buzzer, LOW); // turn the LED off by making the voltage LOW
- delay(100);
- }
- //----------------------------------------------------------------------------------------------//
- //void Display_Digits() {
- //
- // switch (digNum)
- // {
- // case 1:
- // digitalWrite(digit2, LOW);
- // shiftOut(data, clock, LSBFIRST, Digit[tens]);
- // digitalWrite(digit1, HIGH);
- // break;
- // case 2:
- // digitalWrite(digit1, LOW);
- // shiftOut(data, clock, LSBFIRST, Digit[unit]);
- // digitalWrite(digit2, HIGH);
- // break;
- //
- // }
- // digNum = digNum + 1;
- // if (digNum > 2)
- // digNum = 1;
- //}
- //----------------------------------------------------------------------------------------------//
- // Stepper
- void swingOut() {
- EEPROM.write(10, 1);
- StepsRequired = - STEPS_PER_OUT_REV / 2;
- steppermotor.setSpeed(700);
- steppermotor.step(StepsRequired);
- }
- void swingIn() {
- EEPROM.write(10, 2);
- StepsRequired = STEPS_PER_OUT_REV / 2;
- steppermotor.setSpeed(700);
- steppermotor.step(StepsRequired);
- }
- //----------------------------------------------------------------------------------------------//
- void dimmer()
- {
- long int dimval;
- dimval = timeval * 75;
- startTimer1(dimval);
- }
- //----------------------------------------------------------------------------------------------//
- ISR(timer1Event)
- {
- pauseTimer1();
- digitalWrite(MotorPin,HIGH);
- delayMicroseconds(100);
- digitalWrite(MotorPin,LOW);
- }
- void loop(){
- if (irrecv.decode(&results)) {
- // Serial.println(results.value, HEX);
- if(results.value == 0XFFFFFFFF) {
- results.value = key_value;
- }
- //////////////////////////////////////////////////////////////////// ON ////////////////////////////////////////////////////////////////////
- if((results.value == 0x8800325) || (results.value == 0x8800347) || (results.value == 0xD5A9E4D6) ||
- (results.value == 0x8800) || (results.value == 0x69324D85) || (results.value == 0x8800303))
- {
- // Serial.println("ON");
- beep();
- digitalWrite(relay, HIGH); // relay ON
- relayState = true;
- PreviousSwingState = EEPROM.read(10); // Start Swing
- if (PreviousSwingState == 1) // Opened
- SwingState = HIGH;
- else if (PreviousSwingState == 2) // Closed
- SwingState = LOW;
- swingFlag = true;
- speedCounter = 3;
- timeval = fanval[2]; // Motor Top Speed
- if(interruptState == false){
- attachInterrupt(0,dimmer,FALLING);
- interruptState = true;
- }
- tens = 0; // "0n" Segment display
- unit = 15;
- }
- //////////////////////////////////////////////////////////////////// OFF ////////////////////////////////////////////////////////////////////
- if((results.value == 0x88C0051) || (results.value == 0x3393ACC8) || (results.value == 0x1035C9DA) ||
- (results.value == 0x993AE700) || (results.value == 0x88C0))
- {
- // Serial.println("OFF");
- beep();
- detachInterrupt(0); // Motor OFF
- interruptState = false;
- digitalWrite(relay ,LOW);
- PreviousSwingState = EEPROM.read(10);
- if(PreviousSwingState == 2) { // if Lid already in close position stop stepper and turn off
- swingFlag = false;
- }
- else if(PreviousSwingState == 1) { // if Lid open position do a close swingIn()
- swingIn();
- swingFlag = false;
- }
- tens = 14; // Display Nothing
- unit = 14;
- }
- //////////////////////////////////////////////////////////////////// relay ON/OFF ALONE [PLASMA] ////////////////////////////////////////////////////////////////////
- if((results.value == 0x88C000C) || (results.value == 0x88C0084) || (results.value == 0x9718DE1F) ||
- (results.value == 0x4392537E) || (results.value == 0xA7CEC72B) || (results.value == 0xFB766165) ||(results.value == 0x9EFF2F3F))
- {
- // Serial.println("PLASMA");
- beep();
- relayState = !relayState;
- digitalWrite(relay, relayState);
- if(relayState){
- tens = 11; // Display "L0"
- unit = 0;
- }
- if(!relayState){
- tens = 11; // Display "L1"
- unit = 1;
- }
- }
- //////////////////////////////////////////////////////////////////// FAN SPEED ROTATE [JET COOL] ////////////////////////////////////////////////////////////////////
- if((results.value == 0x8810089) || (results.value == 0x8810) ||(results.value == 0x3A398D8A) ||
- (results.value == 0x71199AC5) ||(results.value == 0xEE419856) || (results.value == 0x76105F79))
- {
- // Serial.println("JET COOL");
- beep();
- speedCounter++;
- if(speedCounter >= 3)
- speedCounter = 0;
- timeval = fanval[speedCounter]; // Motor Top Speed
- if(interruptState == false){
- attachInterrupt(0,dimmer,FALLING);
- interruptState = true;
- }
- tens = 5; // Display "S1"
- unit = speedCounter;
- }
- //////////////////////////////////////////////////////////////////// [FAN SPEED] ////////////////////////////////////////////////////////////////////
- if((results.value == 0x8808350) || (results.value == 0x880830B) || (results.value == 0x880832D) ||
- (results.value == 0x8178C8D1) || (results.value == 0x1308451C) || (results.value == 0xA11244F4) ||
- (results.value == 0xD0AE2562) || (results.value == 0x2F0AD5BB))
- {
- // Serial.println("FAN SPEED"); // Fan Speed
- // beep();
- // TODO ?
- }
- //////////////////////////////////////////////////////////////////// [TEMP + -] ////////////////////////////////////////////////////////////////////
- if((results.value == 0x8808440) || (results.value == 0x8808541) || (results.value == 0x8808642) ||
- (results.value == 0x8808743))
- {
- // Serial.println("TEMP 1");
- // beep();
- // TODO ?
- }
- if((results.value == 0x8808844) || (results.value == 0x8808945) || (results.value == 0x8808A46) ||
- (results.value == 0x8808B47))
- {
- // Serial.println("TEMP 2");
- // beep();
- // TODO ?
- }
- if((results.value == 0x8808C48) || (results.value == 0x8808D49) || (results.value == 0x8808E4A) ||
- (results.value == 0x8808F4B))
- {
- // Serial.println("TEMP 3");
- // beep();
- // TODO ?
- }
- key_value = results.value;
- irrecv.resume();
- }
- ////////////////////////////////////////////////////////////////////// SWING ////////////////////////////////////////////////////////////////////
- if (swingFlag == true ) {
- currentMillis = millis();
- if (currentMillis - previousMillis >= SwingInterval) {
- previousMillis = currentMillis;
- if (SwingState == LOW) {
- swingOut();
- flapIn = false;
- flapOut = true;
- SwingState = HIGH;
- } else {
- swingIn();
- flapIn = true;
- flapOut = false;
- SwingState = LOW;
- }
- }
- }
- ////////////////////////////////////////////////////////////////////// SEGMENT DISPLAY ////////////////////////////////////////////////////////////////////
- switch (digNum)
- {
- case 1:
- digitalWrite(digit2, LOW);
- shiftOut(data, clock, LSBFIRST, Digit[tens]);
- digitalWrite(digit1, HIGH);
- break;
- case 2:
- digitalWrite(digit1, LOW);
- shiftOut(data, clock, LSBFIRST, Digit[unit]);
- digitalWrite(digit2, HIGH);
- break;
- }
- digNum = digNum + 1;
- if (digNum > 2)
- digNum = 1;
- }
Add Comment
Please, Sign In to add comment