Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hot Plate - 2 Hot Plate
- #include "Timer.h"
- Timer t;
- #define DIR_OUT(pin) pinMode(pin, OUTPUT) // Output Direction setting
- #define DIR_IN(pin) pinMode(pin, INPUT_PULLUP) // Input direction setting
- #define O_HIGH(pin) digitalWrite(pin, HIGH) // Output settind HIGH status
- #define O_LOW(pin) digitalWrite(pin, LOW) // Output settind LOW status
- #define IN(pin) digitalRead(pin) // Read Input
- #define DOUT 6 // shift regiszter data out
- #define CLK 4 // shift regiszter clock
- #define NOT_LE 5 // shift regiszter lach enable (neg input)
- #define BL A0 // B side Lower button (-)
- #define BF A1 // B side Higher button (+)
- #define SS A2 // Start/Stop button
- #define AL A3 // A side Lower button (-)
- #define AF A4 // A side Higher button (+)
- #define AZ 2 // A side dual LED Green
- #define AP 3 // A side dual LED Red
- #define BZ 11 // B side dual LED Green
- #define BP 12 // B side dual LED Red
- #define TriacA 9
- #define TriacB 10
- #define blockSize 14 //Samples
- #define chunk 2 //Errors
- #define ThValue 10000 //Thermistor value
- #define ThTemp 25 //Thermistor value on temperature
- #define BCoefficient 3950 // Thermistor beta coefficient (3000-4000)
- #define Resistor 10000 //Serial resistor value
- int mBlock[blockSize];
- float tmp = 0, tempA, tempB; // temporary variable, A side temparature, B side temparature
- float steinhart; // the output of the temperature calculation
- unsigned int counter=0;
- // these values depend on the connection of the shift register and the display
- // 0 1 2 3 4 5 6 7 8 9
- const uint8_t NUM_A[] = {17,125, 35 , 41, 77, 137, 129, 61, 1, 9}; // numbers on A, these values are hardware dependent
- const uint8_t NUM_B[] = {17,215, 50 ,146, 212, 152, 24, 211, 16, 144}; // numbers on B, these values are hardware dependent
- const uint8_t SEG1_A[] = {B11111011, B11110111, B11011111, B10111111, B01111111, B11111101}; // only 1 segment on A
- const uint8_t SEG1_B[] = {B10111111, B01111111, B11111101, B11111011, B11110111, B11011111}; // only 1 segment on B
- byte powerMinA=3; // the value after the first press
- byte powerMinB=3;
- byte powerMaxA=9;
- byte powerMaxB=9;
- bool LOCK=false; // Locked status
- bool ON=false; // ON status
- bool swTimer=false; // on/off status
- bool lockTimer=false; // lock on/off status
- bool ON_A=false; // ON status
- bool ON_B=false;
- bool powerA=false; // power status, triac on/off
- bool powerB=false;
- bool prevSS=false; // Start/Stop button previous status
- bool buttonPressA=false; // A side whether the button has already been pressed
- bool buttonPressB=false; // A side whether the button has already been pressed
- byte onoffTimer=0; //
- byte lockonoffTimer=0; //
- byte valueA=0;
- byte valueB=0;
- byte powerCounterA=0; // energy ragulator counter
- byte powerCounterB=0;
- void setup() {
- DIR_OUT(DOUT); // shift register Data, Output
- DIR_OUT(CLK); // shift register Clock, Output
- DIR_OUT(NOT_LE); // shift registers latch enable, Output
- O_HIGH(NOT_LE); // negative output, normal position is high
- DIR_IN(AL); // - button, In
- DIR_IN(AF); // + button, In
- DIR_IN(SS); // Start/Stop button, In
- DIR_IN(BL); // - button, In
- DIR_IN(BF); // + button, In
- DIR_OUT(AZ); // LEDs, OUT
- DIR_OUT(AP);
- DIR_OUT(BZ);
- DIR_OUT(BP);
- DIR_OUT(TriacA); // Triacs, Out
- DIR_OUT(TriacB);
- Serial.begin(115200);
- Serial.println("Start ...");
- int tickEvent = t.every(250, tick, 500); // 250ms clock timer
- shift_out_16(254,239); // OFF status, only point signal on displays
- }
- void loop() {
- digitalWrite(TriacA,powerA); // A side on or off
- digitalWrite(TriacB,powerB); // B side on or off
- t.update(); // call timer event
- swTimer=false; // on/off timer status
- lockTimer=false; //
- if (digitalRead(SS)==HIGH and prevSS==LOW) { // Turn on or off
- Serial.println("SS up");
- if (ON==false) { // prev status is on, turn off
- onoffTimer=0; // value for turn off animation on display
- shift_out_16(254,239); // OFF status, only point signal
- } else { // prev status is off, turn on
- onoffTimer=5; // value for turn on animation on display
- shift_out_16(NUM_A[valueA],NUM_B[valueB]); // ON status, writes values
- }
- } // Turn on or off end
- if (digitalRead(SS)==LOW and LOCK==false) { // Locked status off and push ss button
- prevSS=LOW; // the first press of the button
- swTimer=true; // on/off animation start
- buttonPressA=false;
- buttonPressB =false;
- } else {
- prevSS=HIGH; // continuous button press
- }
- if (digitalRead(SS)==LOW and LOCK==true) { // button press during locked status
- shift_out_16(239,254); // closed status indication
- delay(1000);
- shift_out_16(254,239); // normal off status indication (only one point/display)
- }
- if (ON==true) { // The plate is On
- if (digitalRead(AL)==LOW and buttonPressA==false) { // A side first buttonpress
- buttonPressA=true; // the button has been pressed
- valueA=powerMinA;
- ON_A=true; // A side turn on status
- shift_out_16(NUM_A[valueA],NUM_B[valueB]); // display the values on the displays
- toneBeep();
- delay(500);
- }
- if (digitalRead(BL)==LOW and buttonPressB==false) { // A side first buttonpress
- buttonPressB=true; // the button has been pressed
- valueB=powerMinB;
- ON_B=true; // A side turn on status
- shift_out_16(NUM_A[valueA],NUM_B[valueB]); // display the values on the displays
- toneBeep();
- delay(500);
- }
- if (digitalRead(AF)==LOW and buttonPressA==false) { // A side first buttonpress
- buttonPressA=true; // the button has been pressed
- valueA=powerMaxA;
- ON_A=true; // A side turn on status
- shift_out_16(NUM_A[valueA],NUM_B[valueB]); // display the values on the displays
- toneBeep();
- delay(500);
- }
- if (digitalRead(BF)==LOW and buttonPressB==false) { // A side first buttonpress
- buttonPressB=true; // the button has been pressed
- valueB=powerMaxB;
- ON_B=true; // A side turn on status
- shift_out_16(NUM_A[valueA],NUM_B[valueB]); // display the values on the displays
- toneBeep();
- delay(500);
- }
- if (digitalRead(AL)==LOW and valueA>0) { // A side power down (-), if not minimum
- valueA--;
- if (valueA==0) { // check minimum value
- ON_A=false; // minimum value, power off
- powerA=false; // turn off the output
- }
- shift_out_16(NUM_A[valueA],NUM_B[valueB]); // display the values on the displays
- toneBeep();
- delay(500);
- }
- if (digitalRead(AF)==LOW and valueA<9) { // A side power up (+), if not maximum
- valueA++;
- ON_A=true; // A side turn on status
- shift_out_16(NUM_A[valueA],NUM_B[valueB]);
- toneBeep();
- delay(500);
- }
- if (digitalRead(BL)==LOW and valueB>0) {
- valueB--;
- if (valueB==0) {
- ON_B=false;
- powerB=false;
- }
- shift_out_16(NUM_A[valueA],NUM_B[valueB]);
- toneBeep();
- delay(500);
- }
- if (digitalRead(BF)==LOW and valueB<9) {
- valueB++;
- ON_B=true;
- shift_out_16(NUM_A[valueA],NUM_B[valueB]);
- toneBeep();
- delay(500);
- }
- } else { // The plate is Off
- powerA=false; // A side output Off
- powerB=false; // B side output Off
- ON_A=false; // A side status Off
- ON_B=false; // B side status Off
- if (digitalRead(AF)==LOW and digitalRead(BF)==LOW and LOCK==false) { // if both decrement buttons (-) are pressed and in the locked status
- for (byte i=6;i>=1;i--) { // unlock closed status, display animation start
- Serial.print("AF-BF ");
- Serial.println(i);
- shift_out_16(NUM_A[i-1],NUM_B[i-1]);
- if (digitalRead(AF)==LOW and digitalRead(BF)==LOW) { // release the button before completing the procedure
- delay(500);
- LOCK=true; // stay locked
- } else { // the buttons are pressed continuously
- i=1;
- Serial.println("NO LOCK");
- LOCK=false; // locked status unlocked, ready to use
- shift_out_16(254,239); // normal stand by display
- delay(1000);
- }
- }
- Serial.print("LOCK status: ");
- Serial.println(LOCK);
- if (LOCK==true) { // if locked
- shift_out_16(239,254); // line line
- delay(1000);
- shift_out_16(254,239); // point, point
- }
- } // if both decrement buttons (-) are pressed and in the locked status END
- if (digitalRead(AL)==LOW and digitalRead(BL)==LOW and LOCK==true) { // if both boost buttons (+) are pressed and not in the locked status
- for (byte i=6;i>=1;i--) { // lock closed status, display animation start
- Serial.print("AL-BL ");
- Serial.println(i);
- shift_out_16(NUM_A[i-1],NUM_B[i-1]);
- if (digitalRead(AL)==LOW and digitalRead(BL)==LOW) { // release the button before completing the procedure
- delay(500);
- LOCK=false;
- } else {
- i=1;
- Serial.println("NO LOCK");
- LOCK=true;
- shift_out_16(254,239);
- delay(1000);
- }
- }
- Serial.print("LOCK status: ");
- Serial.println(LOCK);
- if (LOCK==false) {
- shift_out_16(187,187);
- delay(1000);
- shift_out_16(254,239);
- }
- }
- }
- }
- void shift_out_16(unsigned int A_dig,unsigned int B_dig) {
- int n;
- O_LOW(CLK);
- O_HIGH(NOT_LE);
- for (n=0; n<8; n++) {
- if (B_dig & 0x01) {
- O_HIGH(DOUT);
- } else {
- O_LOW(DOUT);
- }
- B_dig=B_dig >> 1;
- delay(1);
- O_HIGH(CLK);
- delay(1);
- O_LOW(CLK);
- }
- for (n=0; n<8; n++) {
- if (A_dig & 0x01) {
- O_HIGH(DOUT);
- } else {
- O_LOW(DOUT);
- }
- A_dig=A_dig >> 1;
- delay(1);
- O_HIGH(CLK);
- delay(1);
- O_LOW(CLK);
- }
- O_LOW(NOT_LE);
- O_HIGH(NOT_LE);
- }
- void tick() {
- chkTemp();
- if (swTimer==true) {
- if (ON==false) {
- shift_out_16(SEG1_A[onoffTimer],SEG1_B[onoffTimer]);
- onoffTimer++;
- if (onoffTimer==6) {
- ON=true;
- swTimer=false;
- valueA=0;
- valueB=0;
- delay(250);
- //shift_out_16(NUM_A[valueA],NUM_B[valueB]); // ON status, writes values
- shift_out_16(69,215); // HI
- toneON();
- while(digitalRead(SS)==LOW); // Waiting for push up ss button
- }
- } else {
- shift_out_16(SEG1_A[onoffTimer],SEG1_B[onoffTimer]);
- onoffTimer--;
- if (onoffTimer==255) {
- ON=false;
- ON_A==false;
- ON_B==false;
- swTimer=false;
- delay(250);
- shift_out_16(254,239); // OFF status, only point signal
- powerA=false;
- powerB=false;
- ON_A=false;
- ON_B=false;
- toneOFF();
- while(digitalRead(SS)==LOW);
- }
- }
- }
- if (ON==true and ON_A==true) {
- powerCounterA++;
- if (powerCounterA==valueA) {
- powerA=false; // turn off the A plate
- }
- if (powerCounterA==9) {
- powerCounterA=0;
- powerA=true; // turn on the A plate
- }
- }
- if (ON==true and ON_B==true) {
- powerCounterB++;
- if (powerCounterB==valueB) {
- powerB=false; // turn off the B plate
- }
- if (powerCounterB==9) {
- powerCounterB=0;
- powerB=true; // turn on the A plate
- }
- }
- }
- void toneON() {
- }
- void toneOFF () {
- }
- void toneBeep () {
- }
- void sort() { // Bubble sorting
- for ( int i = 0; i < blockSize; i++) {
- for ( int j = 0; j < blockSize-1; j++) {
- if (mBlock[j]>mBlock[j+1]) {
- tmp = mBlock[j+1];
- mBlock[j+1] = mBlock[j];
- mBlock[j] = tmp;
- }
- }
- }
- }
- void tempRead(uint8_t readPin) {
- for (int i=0; i < blockSize; i++){
- mBlock[i] = analogRead(readPin); //analog value reading
- delay(5); //waiting for transients
- }
- sort();
- tmp=0;
- for (int i=chunk; i < blockSize-chunk; i++){
- tmp= tmp + mBlock[i];
- }
- tmp=tmp / (blockSize - 2 * chunk); //Scanned value after statistical averaging
- if (tmp <= 1) {
- Serial.println("NTC fault! NTC value too low. Short circuit.");
- tmp = 2;
- }
- tmp = 1023 / tmp - 1; //NTC resistance calculate
- if (tmp == 0) {
- Serial.println("NTC fault! NTC value too high. Broken NTC or wire.");
- tmp=99999999;
- }
- tmp = Resistor / tmp;
- steinhart = tmp / ThValue; // (R/Ro)
- steinhart = log(steinhart); // ln(R/Ro)
- steinhart /= BCoefficient; // 1/B * ln(R/Ro)
- steinhart += 1.0 / (ThTemp + 273.15); // + (1/To)
- steinhart = 1.0 / steinhart; // Invert
- steinhart -= 273.15; // Calculate Celsius degree
- tmp = steinhart;
- }
- void chkTemp() { // Check temperature for LED's
- tempRead(20); // Nano A6 pin
- tempA=tmp;
- tempRead(21); // Nano A7 pin
- tempB=tmp;
- Serial.print("Temperature (C) A: ");
- Serial.print(tempA);
- Serial.print(" B:");
- Serial.println(tempB);
- if (tempA<35) { // Under 35 Celsius, none of the LEDs are on
- digitalWrite(AP,LOW);
- digitalWrite(AZ,LOW);
- }
- if (tempA>=35 and tempA<40) { // Between 35 C and 40 C, the green LED is on
- digitalWrite(AP,LOW);
- digitalWrite(AZ,HIGH);
- }
- if (tempA>=40 and tempA<50) { // Between 40 C and 50 C, the green and red LEDs are on
- digitalWrite(AP,HIGH);
- digitalWrite(AZ,HIGH);
- }
- if (tempA>=50) { // Above 50 C, the red LED is on
- digitalWrite(AP,HIGH);
- digitalWrite(AZ,LOW);
- }
- if (tempB<35) {
- digitalWrite(BP,LOW);
- digitalWrite(BZ,LOW);
- }
- if (tempB>=35 and tempB<40) {
- digitalWrite(BP,LOW);
- digitalWrite(BZ,HIGH);
- }
- if (tempB>=40 and tempB<50) {
- digitalWrite(BP,HIGH);
- digitalWrite(BZ,HIGH);
- }
- if (tempB>=50) {
- digitalWrite(BP,HIGH);
- digitalWrite(BZ,LOW);
- }
- }
Add Comment
Please, Sign In to add comment