Advertisement
babyyoda_

WMD_R2_Timer1_Serial(NO_IR)

Apr 25th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <EEPROM.h>
  3. #include <Stepper.h>
  4. #include<Timer1.h>
  5.  
  6. #define digit1 5          // Segment Digit 1
  7. #define digit2 6          // Segment Digit 2
  8. #define RECV_PIN 7        // IR Receiver
  9. #define relay 8           // RelayPin
  10. #define buzzer 13
  11. #define clock A4          // Segment Clock
  12. #define data A5           // Segment Data
  13. #define MotorPin A0       // Triac Pin
  14. //----------------------------------------------------------------------------------------------//
  15.  
  16. // Stepper motor
  17. unsigned long currentMillis;
  18. unsigned long previousMillis = 0;
  19. const long SwingInterval = 1000;
  20. const float STEPS_PER_REV = 16;       //default 32
  21. const float GEAR_RED = 32;            //default 64
  22. //const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
  23. const float STEPS_PER_OUT_REV = 1024;
  24. int StepsRequired;
  25. Stepper steppermotor(STEPS_PER_REV, 9, 11, 10, 12);
  26. int swingState = LOW;                // swing close by default
  27. byte PreviousSwingState; // EEPROM VAlues
  28. //----------------------------------------------------------------------------------------------//
  29. // Segment Display
  30. byte Digit[] = {B00010010, B11011110, B00010101, B10010100,                         //  0  1  2  3
  31.                 B11011000, B10110000, B00110000, B11010110,                         //  4  5  6  7
  32.                 B00010000, B11010000, B01110001, B00111011,                         //  8  9  F  L
  33.                 B10110000, B00000000, B11111111, B01111100
  34.                };                        //  S  Ao Af n
  35.  
  36.  
  37. // 0 1 2 3 4 5 6 7 8 9 F  L  S  Ao Af n
  38. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  39. //byte symbol[] = {B00000001, B11111110};
  40. int unit, tens;
  41. volatile char digNum = 1;
  42.  
  43. //----------------------------------------------------------------------------------------------//
  44. // Motor Speed Control
  45. //int fanval[5] = {75, 45, 10};         // Fan different Speed
  46. int fanval[5] = {75, 45, 5};         // Fan different Speed
  47. int timeval;
  48. bool interruptState = false;
  49. boolean  fan;
  50. byte speedCounter = 0;
  51. bool relayState = false;
  52. //----------------------------------------------------------------------------------------------//
  53. volatile boolean fanON = 0;
  54. bool machineState = false;
  55. char Serialdata;
  56.  
  57. void setup() {
  58.  
  59.   //  Serial.begin(9600);
  60.   pinMode(2, INPUT);           // ZCD
  61.   pinMode(digit1, OUTPUT);
  62.   pinMode(digit2, OUTPUT);
  63.   pinMode(relay, OUTPUT);
  64.   pinMode(buzzer, OUTPUT);
  65.   pinMode(clock, OUTPUT);
  66.   pinMode(data, OUTPUT);
  67.   pinMode(MotorPin, OUTPUT);
  68.   fan = false;
  69.   attachInterrupt(0, dimmer, FALLING);
  70.   tens = 13;                                       // ALL on
  71.   unit = 13;
  72.   delay(500);
  73.   tens = 14;                                       // All off
  74.   unit = 14;
  75.   //  EEPROM.write(10, 2);                            // Enable for force close swing
  76. }
  77. //----------------------------------------------------------------------------------------------//
  78. void beep() {
  79.   digitalWrite(buzzer, HIGH);   // turn the LED on (HIGH is the voltage level)
  80.   delay(100);                       // wait for a second
  81.   digitalWrite(buzzer, LOW);    // turn the LED off by making the voltage LOW
  82.   delay(100);
  83. }
  84. //----------------------------------------------------------------------------------------------//
  85. // Stepper
  86. void swingOut() {         // Open
  87.   EEPROM.write(10, 1);
  88.   StepsRequired  =  - STEPS_PER_OUT_REV / 2;
  89.   steppermotor.setSpeed(700);
  90.   steppermotor.step(StepsRequired);
  91. }
  92. void swingIn() {          // Close
  93.   EEPROM.write(10, 0xFF);
  94.   StepsRequired  =  STEPS_PER_OUT_REV / 2;
  95.   steppermotor.setSpeed(700);
  96.   steppermotor.step(StepsRequired);
  97. }
  98. //----------------------------------------------------------------------------------------------//
  99.  
  100. void dimmer()
  101. {
  102.   long int dimval;
  103.   dimval = timeval * 75;
  104.   if (fanON) startTimer1(dimval);
  105.  
  106.   switch (digNum)
  107.   {
  108.     case 1:
  109.       digitalWrite(digit2, LOW);
  110.       shiftOut(data, clock, LSBFIRST, Digit[tens]);
  111.       digitalWrite(digit1, HIGH);
  112.       break;
  113.     case 2:
  114.       digitalWrite(digit1, LOW);
  115.       shiftOut(data, clock, LSBFIRST, Digit[unit]);
  116.       digitalWrite(digit2, HIGH);
  117.       break;
  118.   }
  119.   digNum = digNum + 1;
  120.   if (digNum > 2)
  121.     digNum = 1;
  122.  
  123. }
  124. //----------------------------------------------------------------------------------------------//
  125.  
  126.  
  127. ISR(timer1Event)
  128. {
  129.   pauseTimer1();
  130.   digitalWrite(MotorPin, HIGH);
  131.   delayMicroseconds(100);
  132.   digitalWrite(MotorPin, LOW);
  133. }
  134.  
  135.  
  136.  
  137. void loop() {
  138.  
  139.   if (Serial.available() > 0) {
  140.     Serialdata = Serial.read();
  141.  
  142.     //////////////////////////////////////////////////////////////////// ON ////////////////////////////////////////////////////////////////////
  143.     if (Serialdata == '1')
  144.     {
  145.       if (machineState == false) {                        // Turn ON only if it's OFF
  146.         //        Serial.println("ON");
  147.         tens = 0;                                       // "0n" Segment display
  148.         unit = 15;
  149.  
  150.         beep();
  151.  
  152.         digitalWrite(relay, HIGH);                        // relay ON
  153.         relayState = true;
  154.  
  155.         PreviousSwingState = EEPROM.read(10);         // Add variable for address
  156.         if (swingState == LOW && PreviousSwingState == 0xFF) {                    // Open Swing if closed
  157.           swingOut();
  158.           swingState = HIGH;
  159.         }
  160.  
  161.         speedCounter = 3;
  162.         timeval = fanval[2];                            // Motor Top Speed
  163.         if (interruptState == false) {
  164.           fanON = 1;
  165.           interruptState = true;
  166.         }
  167.  
  168.         tens = 11;                                       // "L1" Segment display
  169.         unit = 1;
  170.  
  171.         machineState = true;
  172.       }
  173.     }
  174.     //////////////////////////////////////////////////////////////////// OFF ////////////////////////////////////////////////////////////////////
  175.     if (Serialdata == '2')
  176.     {
  177.       if (machineState == true) {                 // Turn OFF only if it's ON
  178.         //        Serial.println("OFF");
  179.         beep();
  180.         tens = 0;                                // Display Nothing
  181.         unit = 10;
  182.  
  183.         //detachInterrupt(0);                       // Motor OFF
  184.         fanON = 0;
  185.         interruptState = false;
  186.  
  187.         digitalWrite(relay , LOW);
  188.  
  189.         PreviousSwingState = EEPROM.read(10);
  190.         if (PreviousSwingState == 1) {
  191.           swingIn();
  192.           swingState = LOW;
  193.         }
  194.  
  195.         tens = 14;                                // Display Nothing
  196.         unit = 14;
  197.         machineState = false;
  198.       }
  199.     }
  200.     //////////////////////////////////////////////////////////////////// relay ON/OFF ALONE [PLASMA] ////////////////////////////////////////////////////////////////////
  201.     if (Serialdata == '3')
  202.     {
  203.       //        Serial.println("PLASMA");
  204.       beep();
  205.       relayState = !relayState;
  206.       digitalWrite(relay, relayState);
  207.       if (relayState) {
  208.  
  209.         tens = 11;                                // Display "L1"
  210.         unit = 1;
  211.  
  212.         timeval = fanval[2];                            // Turn ON motor if OFF
  213.         if (interruptState == false) {
  214.           fanON = 1;
  215.           interruptState = true;
  216.         }
  217.  
  218.         PreviousSwingState = EEPROM.read(10);
  219.         if (swingState == LOW && PreviousSwingState == 0xFF) {                    // Open Swing if closed
  220.           swingOut();
  221.           swingState = HIGH;
  222.         }
  223.       }
  224.       if (!relayState) {
  225.         tens = 11;                                // Display "L0"
  226.         unit = 0;
  227.       }
  228.     }
  229.     //////////////////////////////////////////////////////////////////// FAN SPEED ROTATE [JET COOL] ////////////////////////////////////////////////////////////////////
  230.     if (Serialdata == '4')
  231.     {
  232.       //        Serial.println("JET COOL");
  233.       beep();
  234.       speedCounter++;
  235.       if (speedCounter >= 3)
  236.         speedCounter = 0;
  237.       timeval = fanval[speedCounter];                            // Motor Top Speed
  238.  
  239.       if (interruptState == false) {
  240.         //attachInterrupt(0,dimmer,FALLING);
  241.         fanON = 1;
  242.         interruptState = true;
  243.       }
  244.  
  245.       PreviousSwingState = EEPROM.read(10);
  246.       if (swingState == LOW && PreviousSwingState == 0xFF) {
  247.         swingOut();
  248.         swingState = HIGH;
  249.       }
  250.  
  251.       tens = 5;                                // Display "S1"
  252.       unit = speedCounter;
  253.  
  254.     }
  255.     ////////////////////////////////////////////////////////////////////  [FAN SPEED] ////////////////////////////////////////////////////////////////////
  256.     if (Serialdata == '5')
  257.     {
  258.       //        Serial.println("FAN SPEED");      // Fan Speed
  259.       //        beep();
  260.       //        TODO ?
  261.     }
  262.     ////////////////////////////////////////////////////////////////////  [TEMP + -] ////////////////////////////////////////////////////////////////////
  263.     if (Serialdata == '6')
  264.     {
  265.       //        Serial.println("TEMP 1");
  266.       //        beep();
  267.       //        TODO ?
  268.     }
  269.     if (Serialdata == '7')
  270.     {
  271.       //        Serial.println("TEMP 2");
  272.       //        beep();
  273.       //        TODO ?
  274.     }
  275.     if (Serialdata == '8')
  276.     {
  277.       //        Serial.println("TEMP 3");
  278.       //        beep();
  279.       //        TODO ?
  280.     }
  281.   }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement