babyyoda_

Complete

Feb 18th, 2021 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.92 KB | None | 0 0
  1. #include <IRremote.h>
  2. #include <EEPROM.h>
  3. #include <Stepper.h>
  4. //#include<MsTimer2.h>
  5. #include<Timer1.h>
  6.  
  7. #define digit1 5          // Segment Digit 1
  8. #define digit2 6          // Segment Digit 2
  9. #define RECV_PIN 7        // IR Receiver
  10. #define relay 8           // RelayPin
  11. #define buzzer 13
  12. #define clock A4          // Segment Clock
  13. #define data A5           // Segment Data
  14. #define MotorPin A0       // Triac Pin
  15. //----------------------------------------------------------------------------------------------//
  16.  
  17. IRrecv irrecv(RECV_PIN);
  18. decode_results results;
  19. unsigned long key_value = 0;
  20. //----------------------------------------------------------------------------------------------//
  21. // Stepper motor
  22. unsigned long currentMillis;
  23. unsigned long previousMillis = 0;        
  24. const long SwingInterval = 1000;        
  25. const float STEPS_PER_REV = 16;       //default 32
  26. const float GEAR_RED = 32;            //default 64
  27. //const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
  28. const float STEPS_PER_OUT_REV = 1024;
  29. int StepsRequired;
  30. Stepper steppermotor(STEPS_PER_REV, 9, 11, 10, 12);
  31. bool swingFlag = false;
  32. int SwingState = LOW;
  33. bool flapIn = true;
  34. bool flapOut = false;
  35. byte PreviousSwingState; // EEPROM VAlues
  36. //----------------------------------------------------------------------------------------------//
  37. // Segment Display
  38. byte Digit[] = {B00010010, B11011110, B00010101, B10010100,                         //  0  1  2  3
  39.                 B11011000, B10110000, B00110000, B11010110,                         //  4  5  6  7
  40.                 B00010000, B11010000, B01110001, B00111011,                         //  8  9  F  L
  41.                 B10110000, B00000000, B11111111, B01111100};                        //  S  Ao Af n
  42.                
  43.                
  44. // 0 1 2 3 4 5 6 7 8 9 F  L  S  Ao Af n
  45. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  46. //byte symbol[] = {B00000001, B11111110};
  47. int unit, tens;
  48. volatile char digNum = 1;
  49.  
  50. //----------------------------------------------------------------------------------------------//
  51. // Motor Speed Control
  52. int fanval[5] = {75,45,10};           // Fan different Speed
  53. int timeval;
  54. bool interruptState = false;
  55. boolean  fan;
  56. byte speedCounter = 0;
  57. bool relayState = false;
  58. //----------------------------------------------------------------------------------------------//
  59.  
  60. void setup() {
  61.  
  62. //  Serial.begin(9600);
  63.   irrecv.enableIRIn();
  64.   irrecv.blink13(false);
  65.   pinMode(2, INPUT);           // ZCD
  66.   pinMode(digit1, OUTPUT);
  67.   pinMode(digit2, OUTPUT);
  68.   pinMode(relay, OUTPUT);
  69.   pinMode(buzzer, OUTPUT);  
  70.   pinMode(clock, OUTPUT);
  71.   pinMode(data, OUTPUT);
  72.   pinMode(MotorPin, OUTPUT);
  73.   fan = false;
  74. //  MsTimer2::set(5, Display_Digits);
  75. //  MsTimer2::start();
  76.  
  77. }
  78. //----------------------------------------------------------------------------------------------//
  79. void beep() {
  80.   digitalWrite(buzzer, HIGH);   // turn the LED on (HIGH is the voltage level)
  81.   delay(100);                       // wait for a second
  82.   digitalWrite(buzzer, LOW);    // turn the LED off by making the voltage LOW
  83.   delay(100);
  84. }
  85. //----------------------------------------------------------------------------------------------//
  86. //void Display_Digits() {
  87. //
  88. //  switch (digNum)
  89. //  {
  90. //    case 1:
  91. //      digitalWrite(digit2, LOW);
  92. //      shiftOut(data, clock, LSBFIRST, Digit[tens]);
  93. //      digitalWrite(digit1, HIGH);
  94. //      break;
  95. //    case 2:
  96. //      digitalWrite(digit1, LOW);
  97. //      shiftOut(data, clock, LSBFIRST, Digit[unit]);
  98. //      digitalWrite(digit2, HIGH);
  99. //      break;
  100. //
  101. //  }
  102. //  digNum = digNum + 1;
  103. //  if (digNum > 2)
  104. //    digNum = 1;
  105. //}
  106. //----------------------------------------------------------------------------------------------//
  107. // Stepper
  108. void swingOut() {
  109.   EEPROM.write(10, 1);
  110.   StepsRequired  =  - STEPS_PER_OUT_REV / 2;
  111.   steppermotor.setSpeed(700);
  112.   steppermotor.step(StepsRequired);
  113. }
  114. void swingIn() {
  115.   EEPROM.write(10, 2);
  116.   StepsRequired  =  STEPS_PER_OUT_REV / 2;
  117.   steppermotor.setSpeed(700);
  118.   steppermotor.step(StepsRequired);
  119. }
  120. //----------------------------------------------------------------------------------------------//
  121.  
  122. void dimmer()
  123. {
  124.   long int dimval;
  125.   dimval = timeval * 75;
  126.   startTimer1(dimval);
  127. }
  128. //----------------------------------------------------------------------------------------------//
  129.  
  130.  
  131. ISR(timer1Event)
  132. {
  133.   pauseTimer1();
  134.   digitalWrite(MotorPin,HIGH);
  135.   delayMicroseconds(100);
  136.   digitalWrite(MotorPin,LOW);
  137. }
  138.  
  139.  
  140.  
  141. void loop(){
  142.  
  143.   if (irrecv.decode(&results)) {
  144. //    Serial.println(results.value, HEX);
  145.  
  146.     if(results.value == 0XFFFFFFFF) {
  147.       results.value = key_value;
  148.     }
  149.     //////////////////////////////////////////////////////////////////// ON ////////////////////////////////////////////////////////////////////
  150.     if((results.value == 0x8800325) || (results.value == 0x8800347) || (results.value == 0xD5A9E4D6) ||
  151.       (results.value == 0x8800) || (results.value == 0x69324D85) || (results.value == 0x8800303))  
  152.       {
  153. //        Serial.println("ON");
  154.         beep();
  155.        
  156.         digitalWrite(relay, HIGH);                        // relay ON
  157.         relayState = true;
  158.        
  159.         PreviousSwingState = EEPROM.read(10);           // Start Swing
  160.         if (PreviousSwingState == 1) // Opened
  161.           SwingState = HIGH;
  162.         else if (PreviousSwingState == 2) // Closed
  163.           SwingState = LOW;
  164.         swingFlag = true;
  165.  
  166.         speedCounter = 3;
  167.         timeval = fanval[2];                            // Motor Top Speed
  168.         if(interruptState == false){
  169.           attachInterrupt(0,dimmer,FALLING);
  170.           interruptState = true;
  171.         }
  172.        
  173.         tens = 0;                                       // "0n" Segment display
  174.         unit = 15;
  175.       }
  176.     //////////////////////////////////////////////////////////////////// OFF ////////////////////////////////////////////////////////////////////
  177.     if((results.value == 0x88C0051) || (results.value == 0x3393ACC8) || (results.value == 0x1035C9DA) ||
  178.       (results.value == 0x993AE700) || (results.value == 0x88C0))
  179.       {
  180. //        Serial.println("OFF");
  181.         beep();
  182.  
  183.         detachInterrupt(0);                       // Motor OFF
  184.         interruptState = false;
  185.  
  186.         digitalWrite(relay ,LOW);
  187.        
  188.         PreviousSwingState = EEPROM.read(10);    
  189.         if(PreviousSwingState == 2) {             // if Lid already in close position stop stepper and turn off
  190.           swingFlag = false;
  191.         }
  192.         else if(PreviousSwingState == 1) {        // if Lid open position do a close swingIn()
  193.           swingIn();
  194.           swingFlag = false;
  195.         }
  196.         tens = 14;                                // Display Nothing
  197.         unit = 14;
  198.       }
  199.     //////////////////////////////////////////////////////////////////// relay ON/OFF ALONE [PLASMA] ////////////////////////////////////////////////////////////////////
  200.     if((results.value == 0x88C000C) || (results.value == 0x88C0084) || (results.value == 0x9718DE1F) ||
  201.       (results.value == 0x4392537E) || (results.value == 0xA7CEC72B) || (results.value == 0xFB766165) ||(results.value == 0x9EFF2F3F))
  202.       {
  203. //        Serial.println("PLASMA");
  204.         beep();
  205.         relayState = !relayState;
  206.         digitalWrite(relay, relayState);      
  207.         if(relayState){
  208.           tens = 11;                                // Display "L0"
  209.           unit = 0;
  210.         }
  211.         if(!relayState){
  212.           tens = 11;                                // Display "L1"
  213.           unit = 1;
  214.         }
  215.       }
  216.     //////////////////////////////////////////////////////////////////// FAN SPEED ROTATE [JET COOL] ////////////////////////////////////////////////////////////////////
  217.     if((results.value == 0x8810089)  || (results.value == 0x8810) ||(results.value == 0x3A398D8A) ||
  218.       (results.value == 0x71199AC5) ||(results.value == 0xEE419856) || (results.value == 0x76105F79))
  219.       {
  220. //        Serial.println("JET COOL");
  221.         beep();
  222.         speedCounter++;
  223.         if(speedCounter >= 3)
  224.           speedCounter = 0;
  225.         timeval = fanval[speedCounter];                            // Motor Top Speed
  226.        
  227.         if(interruptState == false){
  228.           attachInterrupt(0,dimmer,FALLING);
  229.           interruptState = true;
  230.         }
  231.         tens = 5;                                // Display "S1"
  232.         unit = speedCounter;
  233.        
  234.       }
  235.     ////////////////////////////////////////////////////////////////////  [FAN SPEED] ////////////////////////////////////////////////////////////////////
  236.     if((results.value == 0x8808350) || (results.value == 0x880830B) || (results.value == 0x880832D) ||
  237.       (results.value == 0x8178C8D1) || (results.value == 0x1308451C) || (results.value == 0xA11244F4) ||
  238.       (results.value == 0xD0AE2562) || (results.value == 0x2F0AD5BB))
  239.       {
  240. //        Serial.println("FAN SPEED");      // Fan Speed
  241. //        beep();
  242. //        TODO ?
  243.       }
  244.     ////////////////////////////////////////////////////////////////////  [TEMP + -] ////////////////////////////////////////////////////////////////////
  245.     if((results.value == 0x8808440) || (results.value == 0x8808541) || (results.value == 0x8808642) ||
  246.       (results.value == 0x8808743))
  247.       {
  248. //        Serial.println("TEMP 1");
  249. //        beep();
  250. //        TODO ?
  251.       }
  252.     if((results.value == 0x8808844) || (results.value == 0x8808945) || (results.value == 0x8808A46) ||
  253.       (results.value == 0x8808B47))
  254.       {
  255. //        Serial.println("TEMP 2");
  256. //        beep();
  257. //        TODO ?
  258.       }
  259.     if((results.value == 0x8808C48) || (results.value == 0x8808D49) || (results.value == 0x8808E4A) ||
  260.       (results.value == 0x8808F4B))
  261.       {
  262. //        Serial.println("TEMP 3");
  263. //        beep();
  264. //        TODO ?
  265.       }
  266.       key_value = results.value;
  267.       irrecv.resume();
  268.   }
  269.   ////////////////////////////////////////////////////////////////////// SWING ////////////////////////////////////////////////////////////////////
  270.   if (swingFlag == true ) {
  271.     currentMillis = millis();
  272.     if (currentMillis - previousMillis >= SwingInterval) {
  273.       previousMillis = currentMillis;
  274.  
  275.       if (SwingState == LOW) {
  276.         swingOut();
  277.         flapIn = false;
  278.         flapOut = true;
  279.         SwingState = HIGH;
  280.  
  281.       } else {
  282.         swingIn();
  283.         flapIn = true;
  284.         flapOut = false;
  285.         SwingState = LOW;
  286.       }
  287.     }
  288.   }
  289.   ////////////////////////////////////////////////////////////////////// SEGMENT DISPLAY ////////////////////////////////////////////////////////////////////
  290.    
  291.   switch (digNum)
  292.   {
  293.     case 1:
  294.       digitalWrite(digit2, LOW);
  295.       shiftOut(data, clock, LSBFIRST, Digit[tens]);
  296.       digitalWrite(digit1, HIGH);
  297.       break;
  298.     case 2:
  299.       digitalWrite(digit1, LOW);
  300.       shiftOut(data, clock, LSBFIRST, Digit[unit]);
  301.       digitalWrite(digit2, HIGH);
  302.       break;
  303.   }
  304.   digNum = digNum + 1;
  305.   if (digNum > 2)
  306.     digNum = 1;
  307. }
  308.  
Add Comment
Please, Sign In to add comment