Szerelo

Dual Hot Plate energy ragulator

Jul 4th, 2020
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.07 KB | None | 0 0
  1. // Hot Plate - 2 Hot Plate
  2.  
  3.  
  4. #include "Timer.h"
  5. Timer t;
  6.  
  7. #define DIR_OUT(pin) pinMode(pin, OUTPUT)   // Output Direction setting
  8. #define DIR_IN(pin)   pinMode(pin, INPUT_PULLUP)      // Input direction setting
  9. #define O_HIGH(pin) digitalWrite(pin, HIGH)   // Output settind HIGH status
  10. #define O_LOW(pin) digitalWrite(pin, LOW)     // Output settind LOW status
  11. #define IN(pin) digitalRead(pin)      // Read Input
  12.  
  13. #define DOUT 6  // shift regiszter data out
  14. #define CLK 4   // shift regiszter clock
  15. #define NOT_LE 5  // shift regiszter lach enable (neg input)
  16. #define BL A0 // B side Lower button (-)
  17. #define BF A1 // B side Higher button (+)
  18. #define SS A2 // Start/Stop button
  19. #define AL A3 // A side Lower button (-)
  20. #define AF A4 // A side Higher button (+)
  21. #define AZ 2  // A side dual LED Green
  22. #define AP 3  // A side dual LED Red
  23. #define BZ 11 // B side dual LED Green
  24. #define BP 12 // B side dual LED Red
  25. #define TriacA 9
  26. #define TriacB 10
  27.  
  28. #define blockSize 14       //Samples
  29. #define chunk 2            //Errors
  30. #define ThValue 10000      //Thermistor value
  31. #define ThTemp 25   //Thermistor value on temperature
  32. #define BCoefficient 3950  // Thermistor beta coefficient (3000-4000)
  33. #define Resistor 10000     //Serial resistor value
  34.  
  35. int mBlock[blockSize];
  36. float tmp = 0, tempA, tempB;  // temporary variable, A side temparature, B side temparature
  37. float steinhart;    // the output of the temperature calculation
  38.  
  39. unsigned int counter=0;
  40. // these values depend on the connection of the shift register and the display
  41. //                        0   1   2    3    4    5    6    7    8    9
  42. const uint8_t NUM_A[] = {17,125, 35 , 41,  77, 137, 129,  61,   1,   9};    // numbers on A, these values are hardware dependent
  43. const uint8_t NUM_B[] = {17,215, 50 ,146, 212, 152,  24, 211,  16, 144};    // numbers on B, these values are hardware dependent
  44. const uint8_t SEG1_A[] = {B11111011, B11110111, B11011111, B10111111, B01111111, B11111101};   // only 1 segment on A
  45. const uint8_t SEG1_B[] = {B10111111, B01111111, B11111101, B11111011, B11110111, B11011111};   // only 1 segment on B
  46.  
  47. byte powerMinA=3;   // the value after the first press
  48. byte powerMinB=3;
  49. byte powerMaxA=9;
  50. byte powerMaxB=9;
  51. bool LOCK=false;    // Locked status
  52. bool ON=false;      // ON status
  53. bool swTimer=false; // on/off status
  54. bool lockTimer=false; // lock on/off status
  55. bool ON_A=false;    // ON status
  56. bool ON_B=false;
  57. bool powerA=false;  // power status, triac on/off
  58. bool powerB=false;
  59. bool prevSS=false;  // Start/Stop button previous status
  60. bool buttonPressA=false;  // A side whether the button has already been pressed
  61. bool buttonPressB=false;  // A side whether the button has already been pressed
  62. byte onoffTimer=0;  //
  63. byte lockonoffTimer=0;  //
  64. byte valueA=0;
  65. byte valueB=0;
  66. byte powerCounterA=0; // energy ragulator counter
  67. byte powerCounterB=0;
  68.  
  69. void setup() {
  70.   DIR_OUT(DOUT);    // shift register Data, Output
  71.   DIR_OUT(CLK);     // shift register Clock, Output
  72.   DIR_OUT(NOT_LE);  // shift registers latch enable, Output
  73.   O_HIGH(NOT_LE);   // negative output, normal position is high
  74.   DIR_IN(AL);       // - button, In
  75.   DIR_IN(AF);       // + button, In
  76.   DIR_IN(SS);       // Start/Stop button, In
  77.   DIR_IN(BL);       // - button, In
  78.   DIR_IN(BF);       // + button, In
  79.   DIR_OUT(AZ);      // LEDs, OUT
  80.   DIR_OUT(AP);
  81.   DIR_OUT(BZ);
  82.   DIR_OUT(BP);
  83.   DIR_OUT(TriacA);  // Triacs, Out
  84.   DIR_OUT(TriacB);
  85.   Serial.begin(115200);
  86.   Serial.println("Start ...");
  87.   int tickEvent = t.every(250, tick, 500);    // 250ms clock timer
  88.   shift_out_16(254,239);  // OFF status, only point signal on displays
  89. }
  90.  
  91. void loop() {
  92.   digitalWrite(TriacA,powerA);    // A side on or off
  93.   digitalWrite(TriacB,powerB);    // B side on or off
  94.   t.update();     // call timer event
  95.   swTimer=false;  // on/off timer status
  96.   lockTimer=false;  //
  97.   if (digitalRead(SS)==HIGH and prevSS==LOW) {  // Turn on or off
  98.     Serial.println("SS up");
  99.     if (ON==false) {  // prev status is on, turn off
  100.       onoffTimer=0;   // value for turn off animation on display
  101.       shift_out_16(254,239);  // OFF status, only point signal
  102.     } else {  // prev status is off, turn on
  103.       onoffTimer=5;   // value for turn on animation on display
  104.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);  // ON status, writes values
  105.     }
  106.   } // Turn on or off end
  107.   if (digitalRead(SS)==LOW and LOCK==false) {   // Locked status off and push ss button
  108.     prevSS=LOW;    // the first press of the button
  109.     swTimer=true;  // on/off animation start
  110.     buttonPressA=false;
  111.     buttonPressB  =false;
  112.   } else {
  113.     prevSS=HIGH;  // continuous button press
  114.   }
  115.   if (digitalRead(SS)==LOW and LOCK==true) {  // button press during locked status
  116.     shift_out_16(239,254);  // closed status indication
  117.     delay(1000);
  118.     shift_out_16(254,239);  // normal off status indication (only one point/display)
  119.   }
  120.   if (ON==true) {   // The plate is On
  121.     if (digitalRead(AL)==LOW and buttonPressA==false) {   // A side first buttonpress
  122.       buttonPressA=true;    // the button has been pressed
  123.       valueA=powerMinA;
  124.       ON_A=true;    // A side turn on status
  125.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);  // display the values on the displays
  126.       toneBeep();
  127.       delay(500);
  128.     }
  129.     if (digitalRead(BL)==LOW and buttonPressB==false) {   // A side first buttonpress
  130.       buttonPressB=true;    // the button has been pressed
  131.       valueB=powerMinB;
  132.       ON_B=true;    // A side turn on status
  133.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);  // display the values on the displays
  134.       toneBeep();
  135.       delay(500);
  136.     }
  137.     if (digitalRead(AF)==LOW and buttonPressA==false) {   // A side first buttonpress
  138.       buttonPressA=true;    // the button has been pressed
  139.       valueA=powerMaxA;
  140.       ON_A=true;    // A side turn on status
  141.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);  // display the values on the displays
  142.       toneBeep();
  143.       delay(500);
  144.     }
  145.     if (digitalRead(BF)==LOW and buttonPressB==false) {   // A side first buttonpress
  146.       buttonPressB=true;    // the button has been pressed
  147.       valueB=powerMaxB;
  148.       ON_B=true;    // A side turn on status
  149.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);  // display the values on the displays
  150.       toneBeep();
  151.       delay(500);
  152.     }
  153.     if (digitalRead(AL)==LOW and valueA>0) {  // A side power down (-), if not minimum
  154.       valueA--;
  155.       if (valueA==0) {    // check minimum value
  156.         ON_A=false;       // minimum value, power off
  157.         powerA=false;     // turn off the output
  158.       }
  159.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);  // display the values on the displays
  160.       toneBeep();
  161.       delay(500);
  162.     }
  163.     if (digitalRead(AF)==LOW and valueA<9) { // A side power up (+), if not maximum
  164.       valueA++;
  165.       ON_A=true;    // A side turn on status
  166.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);
  167.       toneBeep();
  168.       delay(500);
  169.     }
  170.     if (digitalRead(BL)==LOW and valueB>0) {
  171.       valueB--;
  172.       if (valueB==0) {
  173.         ON_B=false;
  174.         powerB=false;
  175.       }
  176.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);
  177.       toneBeep();
  178.       delay(500);
  179.     }
  180.     if (digitalRead(BF)==LOW and valueB<9) {
  181.       valueB++;
  182.       ON_B=true;
  183.       shift_out_16(NUM_A[valueA],NUM_B[valueB]);
  184.       toneBeep();
  185.       delay(500);
  186.     }
  187.   } else { // The plate is Off
  188.     powerA=false;   // A side output Off
  189.     powerB=false;   // B side output Off
  190.     ON_A=false;     // A side status Off
  191.     ON_B=false;     // B side status Off
  192.     if (digitalRead(AF)==LOW and digitalRead(BF)==LOW and LOCK==false) {    // if both decrement buttons (-) are pressed and in the locked status
  193.       for (byte i=6;i>=1;i--) {   // unlock closed status, display animation start
  194.         Serial.print("AF-BF ");
  195.         Serial.println(i);
  196.         shift_out_16(NUM_A[i-1],NUM_B[i-1]);
  197.         if (digitalRead(AF)==LOW and digitalRead(BF)==LOW) { // release the button before completing the procedure
  198.           delay(500);
  199.           LOCK=true;  // stay locked
  200.         } else {  // the buttons are pressed continuously
  201.           i=1;
  202.           Serial.println("NO LOCK");
  203.           LOCK=false;   // locked status unlocked, ready to use
  204.           shift_out_16(254,239);    // normal stand by display
  205.           delay(1000);
  206.         }
  207.       }
  208.       Serial.print("LOCK status: ");
  209.       Serial.println(LOCK);
  210.       if (LOCK==true) {   // if locked
  211.         shift_out_16(239,254); // line line
  212.         delay(1000);
  213.         shift_out_16(254,239);  // point, point
  214.       }
  215.     }   // if both decrement buttons (-) are pressed and in the locked status END
  216.     if (digitalRead(AL)==LOW and digitalRead(BL)==LOW and LOCK==true) {   // if both boost buttons (+) are pressed and not in the locked status
  217.       for (byte i=6;i>=1;i--) {  // lock closed status, display animation start
  218.         Serial.print("AL-BL ");
  219.         Serial.println(i);
  220.         shift_out_16(NUM_A[i-1],NUM_B[i-1]);
  221.         if (digitalRead(AL)==LOW and digitalRead(BL)==LOW) {  // release the button before completing the procedure
  222.           delay(500);
  223.           LOCK=false;
  224.         } else {
  225.           i=1;
  226.           Serial.println("NO LOCK");
  227.           LOCK=true;
  228.           shift_out_16(254,239);
  229.           delay(1000);
  230.         }
  231.       }
  232.       Serial.print("LOCK status: ");
  233.       Serial.println(LOCK);
  234.       if (LOCK==false) {
  235.         shift_out_16(187,187);
  236.         delay(1000);
  237.         shift_out_16(254,239);
  238.       }
  239.     }
  240.   }
  241. }
  242.  
  243. void shift_out_16(unsigned int A_dig,unsigned int B_dig) {
  244.   int n;
  245.   O_LOW(CLK);
  246.   O_HIGH(NOT_LE);
  247.   for (n=0; n<8; n++) {
  248.     if (B_dig & 0x01) {
  249.       O_HIGH(DOUT);
  250.       } else  {
  251.         O_LOW(DOUT);
  252.       }
  253.     B_dig=B_dig >> 1;
  254.     delay(1);
  255.     O_HIGH(CLK);
  256.     delay(1);
  257.     O_LOW(CLK);
  258.   }
  259.   for (n=0; n<8; n++) {
  260.     if (A_dig & 0x01) {
  261.       O_HIGH(DOUT);
  262.       } else  {
  263.         O_LOW(DOUT);
  264.       }
  265.     A_dig=A_dig >> 1;
  266.     delay(1);
  267.     O_HIGH(CLK);
  268.     delay(1);
  269.     O_LOW(CLK);
  270.   }
  271.   O_LOW(NOT_LE);
  272.   O_HIGH(NOT_LE);
  273. }
  274.  
  275. void tick() {
  276.   chkTemp();
  277.   if (swTimer==true) {
  278.     if (ON==false) {
  279.       shift_out_16(SEG1_A[onoffTimer],SEG1_B[onoffTimer]);
  280.       onoffTimer++;
  281.       if (onoffTimer==6) {
  282.         ON=true;
  283.         swTimer=false;
  284.         valueA=0;
  285.         valueB=0;
  286.         delay(250);
  287.         //shift_out_16(NUM_A[valueA],NUM_B[valueB]);  // ON status, writes values
  288.         shift_out_16(69,215);  // HI
  289.         toneON();
  290.         while(digitalRead(SS)==LOW);    // Waiting for push up ss button
  291.       }
  292.     } else {
  293.       shift_out_16(SEG1_A[onoffTimer],SEG1_B[onoffTimer]);
  294.       onoffTimer--;
  295.       if (onoffTimer==255) {
  296.         ON=false;
  297.         ON_A==false;
  298.         ON_B==false;
  299.         swTimer=false;
  300.         delay(250);
  301.         shift_out_16(254,239);  // OFF status, only point signal
  302.         powerA=false;
  303.         powerB=false;
  304.         ON_A=false;
  305.         ON_B=false;
  306.         toneOFF();
  307.         while(digitalRead(SS)==LOW);
  308.       }
  309.     }
  310.   }
  311.   if (ON==true and ON_A==true) {
  312.     powerCounterA++;
  313.     if (powerCounterA==valueA) {
  314.       powerA=false;     // turn off the A plate
  315.     }
  316.     if (powerCounterA==9) {
  317.       powerCounterA=0;
  318.       powerA=true;    // turn on the A plate
  319.     }
  320.   }
  321.   if (ON==true and ON_B==true) {
  322.     powerCounterB++;
  323.     if (powerCounterB==valueB) {
  324.       powerB=false;     // turn off the B plate
  325.     }
  326.     if (powerCounterB==9) {
  327.       powerCounterB=0;
  328.       powerB=true;    // turn on the A plate
  329.     }
  330.   }
  331.  
  332. }
  333.  
  334. void toneON() {
  335.  
  336. }
  337.  
  338. void toneOFF () {
  339.  
  340. }
  341.  
  342. void toneBeep () {
  343.  
  344. }
  345.  
  346. void sort() {   // Bubble sorting
  347.   for ( int i = 0; i < blockSize; i++) {
  348.     for ( int j = 0; j < blockSize-1; j++) {
  349.       if (mBlock[j]>mBlock[j+1]) {
  350.         tmp = mBlock[j+1];
  351.         mBlock[j+1] = mBlock[j];
  352.         mBlock[j] = tmp;
  353.         }
  354.       }
  355.     }
  356.   }
  357.  
  358.  
  359. void tempRead(uint8_t readPin) {
  360.   for (int i=0; i < blockSize; i++){
  361.     mBlock[i] = analogRead(readPin);     //analog value reading
  362.     delay(5);                     //waiting for transients
  363.     }
  364.   sort();
  365.   tmp=0;
  366.   for (int i=chunk; i < blockSize-chunk; i++){
  367.     tmp= tmp + mBlock[i];
  368.     }
  369.   tmp=tmp / (blockSize - 2 * chunk);    //Scanned value after statistical averaging
  370.   if (tmp <= 1) {
  371.     Serial.println("NTC fault! NTC value too low. Short circuit.");
  372.     tmp = 2;
  373.     }
  374.   tmp = 1023 / tmp - 1;           //NTC resistance calculate
  375.   if (tmp == 0) {
  376.     Serial.println("NTC fault! NTC value too high. Broken NTC or wire.");
  377.     tmp=99999999;
  378.     }
  379.   tmp = Resistor / tmp;
  380.   steinhart = tmp / ThValue;     // (R/Ro)
  381.   steinhart = log(steinhart);    // ln(R/Ro)
  382.   steinhart /= BCoefficient;     // 1/B * ln(R/Ro)
  383.   steinhart += 1.0 / (ThTemp + 273.15); // + (1/To)
  384.   steinhart = 1.0 / steinhart;                 // Invert
  385.   steinhart -= 273.15;                         // Calculate Celsius degree
  386.   tmp = steinhart;
  387.   }
  388.  
  389. void  chkTemp() { // Check temperature for LED's
  390.     tempRead(20);   // Nano A6 pin
  391.     tempA=tmp;
  392.     tempRead(21);   // Nano A7 pin
  393.     tempB=tmp;
  394.     Serial.print("Temperature (C) A: ");
  395.     Serial.print(tempA);
  396.     Serial.print(" B:");
  397.     Serial.println(tempB);
  398.     if (tempA<35) {       // Under 35 Celsius, none of the LEDs are on
  399.       digitalWrite(AP,LOW);
  400.       digitalWrite(AZ,LOW);
  401.     }
  402.     if (tempA>=35 and tempA<40) {   // Between 35 C and 40 C, the green LED is on
  403.       digitalWrite(AP,LOW);
  404.       digitalWrite(AZ,HIGH);
  405.     }
  406.     if (tempA>=40 and tempA<50) {  // Between 40 C and 50 C, the green and red LEDs are on
  407.       digitalWrite(AP,HIGH);
  408.       digitalWrite(AZ,HIGH);
  409.     }
  410.     if (tempA>=50) {          // Above 50 C, the red LED is on
  411.       digitalWrite(AP,HIGH);
  412.       digitalWrite(AZ,LOW);
  413.     }
  414.     if (tempB<35) {
  415.       digitalWrite(BP,LOW);
  416.       digitalWrite(BZ,LOW);
  417.     }
  418.     if (tempB>=35 and tempB<40) {
  419.       digitalWrite(BP,LOW);
  420.       digitalWrite(BZ,HIGH);
  421.     }
  422.     if (tempB>=40 and tempB<50) {
  423.       digitalWrite(BP,HIGH);
  424.       digitalWrite(BZ,HIGH);
  425.     }
  426.     if (tempB>=50) {
  427.       digitalWrite(BP,HIGH);
  428.       digitalWrite(BZ,LOW);
  429.     }
  430.   }
Add Comment
Please, Sign In to add comment