Advertisement
babyyoda_

StepperDisabled

Feb 18th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.17 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. volatile boolean fanON = 0;
  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.   attachInterrupt(0,dimmer,FALLING);
  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.   if(fanON) startTimer1(dimval);
  127.  
  128.   switch (digNum)
  129.   {
  130.     case 1:
  131.       digitalWrite(digit2, LOW);
  132.       shiftOut(data, clock, LSBFIRST, Digit[tens]);
  133.       digitalWrite(digit1, HIGH);
  134.       break;
  135.     case 2:
  136.       digitalWrite(digit1, LOW);
  137.       shiftOut(data, clock, LSBFIRST, Digit[unit]);
  138.       digitalWrite(digit2, HIGH);
  139.       break;
  140.   }
  141.   digNum = digNum + 1;
  142.   if (digNum > 2)
  143.     digNum = 1;
  144.  
  145. }
  146. //----------------------------------------------------------------------------------------------//
  147.  
  148.  
  149. ISR(timer1Event)
  150. {
  151.   pauseTimer1();
  152.   digitalWrite(MotorPin,HIGH);
  153.   delayMicroseconds(100);
  154.   digitalWrite(MotorPin,LOW);
  155. }
  156.  
  157.  
  158.  
  159. void loop(){
  160.  
  161.   if (irrecv.decode(&results)) {
  162. //    Serial.println(results.value, HEX);
  163.  
  164.     if(results.value == 0XFFFFFFFF) {
  165.       results.value = key_value;
  166.     }
  167.     //////////////////////////////////////////////////////////////////// ON ////////////////////////////////////////////////////////////////////
  168.     if((results.value == 0x8800325) || (results.value == 0x8800347) || (results.value == 0xD5A9E4D6) ||
  169.       (results.value == 0x8800) || (results.value == 0x69324D85) || (results.value == 0x8800303))  
  170.       {
  171. //        Serial.println("ON");
  172.         beep();
  173.        
  174.         digitalWrite(relay, HIGH);                        // relay ON
  175.         relayState = true;
  176.        
  177. //        PreviousSwingState = EEPROM.read(10);           // Start Swing
  178. //        if (PreviousSwingState == 1) // Opened
  179. //          SwingState = HIGH;
  180. //        else if (PreviousSwingState == 2) // Closed
  181. //          SwingState = LOW;
  182. //        swingFlag = true;
  183. //
  184.         speedCounter = 3;
  185.         timeval = fanval[2];                            // Motor Top Speed
  186.         if(interruptState == false){
  187.           attachInterrupt(0,dimmer,FALLING);
  188.           interruptState = true;
  189.         }
  190.        
  191.         tens = 0;                                       // "0n" Segment display
  192.         unit = 15;
  193.       }
  194.     //////////////////////////////////////////////////////////////////// OFF ////////////////////////////////////////////////////////////////////
  195.     if((results.value == 0x88C0051) || (results.value == 0x3393ACC8) || (results.value == 0x1035C9DA) ||
  196.       (results.value == 0x993AE700) || (results.value == 0x88C0))
  197.       {
  198. //        Serial.println("OFF");
  199.         beep();
  200.  
  201.         //detachInterrupt(0);                       // Motor OFF
  202.         fanON = 0;
  203.         interruptState = false;
  204.  
  205.         digitalWrite(relay ,LOW);
  206.        
  207. //        PreviousSwingState = EEPROM.read(10);    
  208. //        if(PreviousSwingState == 2) {             // if Lid already in close position stop stepper and turn off
  209. //          swingFlag = false;
  210. //        }
  211. //        else if(PreviousSwingState == 1) {        // if Lid open position do a close swingIn()
  212. //          swingIn();
  213. //          swingFlag = false;
  214. //        }
  215.         tens = 14;                                // Display Nothing
  216.         unit = 14;
  217.       }
  218.     //////////////////////////////////////////////////////////////////// relay ON/OFF ALONE [PLASMA] ////////////////////////////////////////////////////////////////////
  219.     if((results.value == 0x88C000C) || (results.value == 0x88C0084) || (results.value == 0x9718DE1F) ||
  220.       (results.value == 0x4392537E) || (results.value == 0xA7CEC72B) || (results.value == 0xFB766165) ||(results.value == 0x9EFF2F3F))
  221.       {
  222. //        Serial.println("PLASMA");
  223.         beep();
  224.         relayState = !relayState;
  225.         digitalWrite(relay, relayState);      
  226.         if(relayState){
  227.           tens = 11;                                // Display "L0"
  228.           unit = 0;
  229.         }
  230.         if(!relayState){
  231.           tens = 11;                                // Display "L1"
  232.           unit = 1;
  233.         }
  234.       }
  235.     //////////////////////////////////////////////////////////////////// FAN SPEED ROTATE [JET COOL] ////////////////////////////////////////////////////////////////////
  236.     if((results.value == 0x8810089)  || (results.value == 0x8810) ||(results.value == 0x3A398D8A) ||
  237.       (results.value == 0x71199AC5) ||(results.value == 0xEE419856) || (results.value == 0x76105F79))
  238.       {
  239. //        Serial.println("JET COOL");
  240.         beep();
  241.         speedCounter++;
  242.         if(speedCounter >= 3)
  243.           speedCounter = 0;
  244.         timeval = fanval[speedCounter];                            // Motor Top Speed
  245.        
  246.         if(interruptState == false){
  247.           //attachInterrupt(0,dimmer,FALLING);
  248.           fanON = 1;
  249.           interruptState = true;
  250.         }
  251.         tens = 5;                                // Display "S1"
  252.         unit = speedCounter;
  253.        
  254.       }
  255.     ////////////////////////////////////////////////////////////////////  [FAN SPEED] ////////////////////////////////////////////////////////////////////
  256.     if((results.value == 0x8808350) || (results.value == 0x880830B) || (results.value == 0x880832D) ||
  257.       (results.value == 0x8178C8D1) || (results.value == 0x1308451C) || (results.value == 0xA11244F4) ||
  258.       (results.value == 0xD0AE2562) || (results.value == 0x2F0AD5BB))
  259.       {
  260. //        Serial.println("FAN SPEED");      // Fan Speed
  261. //        beep();
  262. //        TODO ?
  263.       }
  264.     ////////////////////////////////////////////////////////////////////  [TEMP + -] ////////////////////////////////////////////////////////////////////
  265.     if((results.value == 0x8808440) || (results.value == 0x8808541) || (results.value == 0x8808642) ||
  266.       (results.value == 0x8808743))
  267.       {
  268. //        Serial.println("TEMP 1");
  269. //        beep();
  270. //        TODO ?
  271.       }
  272.     if((results.value == 0x8808844) || (results.value == 0x8808945) || (results.value == 0x8808A46) ||
  273.       (results.value == 0x8808B47))
  274.       {
  275. //        Serial.println("TEMP 2");
  276. //        beep();
  277. //        TODO ?
  278.       }
  279.     if((results.value == 0x8808C48) || (results.value == 0x8808D49) || (results.value == 0x8808E4A) ||
  280.       (results.value == 0x8808F4B))
  281.       {
  282. //        Serial.println("TEMP 3");
  283. //        beep();
  284. //        TODO ?
  285.       }
  286.       key_value = results.value;
  287.       irrecv.resume();
  288.   }
  289.   ////////////////////////////////////////////////////////////////////// SWING ////////////////////////////////////////////////////////////////////
  290. //  if (swingFlag == true ) {
  291. //    currentMillis = millis();
  292. //    if (currentMillis - previousMillis >= SwingInterval) {
  293. //      previousMillis = currentMillis;
  294. //
  295. //      if (SwingState == LOW) {
  296. //        swingOut();
  297. //        flapIn = false;
  298. //        flapOut = true;
  299. //        SwingState = HIGH;
  300. //
  301. //      } else {
  302. //        swingIn();
  303. //        flapIn = true;
  304. //        flapOut = false;
  305. //        SwingState = LOW;
  306. //      }
  307. //    }
  308. //  }
  309.   ////////////////////////////////////////////////////////////////////// SEGMENT DISPLAY ////////////////////////////////////////////////////////////////////
  310.    
  311.  
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement