Advertisement
Szerelo

Relay tester with adjustable times

May 9th, 2021
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.05 KB | None | 0 0
  1. // Relé teszter
  2.  
  3. #include <LiquidCrystal_I2C.h>
  4. #include <Rotary.h>
  5. #include <EEPROM.h>
  6.  
  7. #define gomb A0
  8. #define relay1 3
  9. #define relay2 2
  10. #define led 13
  11.  
  12. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))  // |=  Bitenkénti megengedő VAGY és értékadás
  13. #define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit))
  14.  
  15. float Tapfeszultseg = 0;
  16.  
  17. unsigned long currentMillis=0;
  18. unsigned long previousMillis=0;
  19. unsigned int darab=0;
  20. unsigned int ertek=0;
  21. unsigned int beTime=0;
  22. unsigned int kiTime=0;
  23. unsigned int beCounter=0;
  24. unsigned int kiCounter=0;
  25. bool be=false;   // Relé be van-e kapcsolva
  26. bool fut=false;
  27. bool valt=true;   // Volt-e adat változás a kiiráshoz
  28. int counter=20;
  29. byte menu=0;
  30.  
  31. unsigned long lastDebounceTime = 0;
  32. unsigned long debounceDelay = 50;
  33. unsigned long longPressDelay = 1000;
  34. unsigned long buttonCounter;
  35.  
  36. bool readingGomb;
  37. bool buttonShort=false;
  38. bool buttonLong=false;
  39. bool buttonShortPress=false;
  40. bool buttonLongPress=false;
  41. bool reading=true;
  42. bool setting=false;
  43.  
  44. //LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
  45. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  46.  
  47. Rotary r = Rotary(A1, A2);            // define rottary encoder and pins
  48.  
  49. void setup() {
  50.   ADMUX = (0 << ADLAR) | (1 << REFS0) | (14 << MUX0) ;
  51.   sbi(ADCSRA, ADSC);
  52.   Serial.begin(115200);
  53.   Serial.println("Start ...");
  54.   lcd.init();                      // initialize the lcd
  55.   lcd.backlight();
  56.   //lcd.begin(16,2);
  57.   pinMode(gomb, INPUT_PULLUP);
  58.   pinMode(led, OUTPUT);
  59.   pinMode(relay1, OUTPUT);
  60.   pinMode(relay2, OUTPUT);
  61.   digitalWrite(relay1, LOW);
  62.   digitalWrite(relay2, LOW);
  63.   r.begin(true);
  64.   beTime=EEPROM.read(0);
  65.   kiTime=EEPROM.read(1);
  66.   //beTime=20;
  67.   //kiTime=40;
  68.   darab=EEPROM.read(2)+EEPROM.read(3)*256;
  69.   //darab=0;
  70.   while(bit_is_set(ADCSRA,ADIF)){
  71.      Tapfeszultseg=(1024*1.1)/(ADCL+256*ADCH );
  72.      sbi(ADCSRA, ADSC);
  73.   }
  74.   Serial.println(Tapfeszultseg);
  75.   delay(500);
  76.   while(bit_is_set(ADCSRA,ADIF)){
  77.      Tapfeszultseg=(1024*1.1)/(ADCL+256*ADCH );
  78.      sbi(ADCSRA, ADSC);
  79.   }
  80.   Serial.println(Tapfeszultseg);
  81.   delay(500);
  82.   lcd.clear();
  83.   kiir();
  84.   Serial.println("..");
  85. }
  86.  
  87. void loop() {
  88.   while(bit_is_set(ADCSRA,ADIF)){
  89.      Tapfeszultseg=(1024*1.1)/(ADCL+256*ADCH );
  90.      sbi(ADCSRA, ADSC);
  91.   }
  92.   unsigned char result = r.process();
  93.   if (counter>0) {
  94.     Serial.println(Tapfeszultseg);
  95.     counter--;
  96.   }
  97.   if (Tapfeszultseg<4) {
  98.     lcd.noBacklight();
  99.     digitalWrite(relay1, LOW);
  100.     digitalWrite(relay2, LOW);
  101.     digitalWrite(led, LOW);
  102.     ment();
  103.     digitalWrite(led, HIGH);
  104.   }
  105.  
  106.  
  107.   gombnyomas1();
  108.   if (setting==false) {   // normál
  109.     if (buttonShort==true and readingGomb==HIGH){
  110.       reading=false;
  111.       buttonShort=false;
  112.       Serial.println("Short Press");
  113.       if (fut==false) {
  114.         fut=true;
  115.         beCounter=beTime;
  116.         digitalWrite(relay1,HIGH);
  117.       } else {
  118.         fut=false;
  119.         be=false;
  120.         digitalWrite(led,LOW);
  121.         digitalWrite(relay2,LOW);
  122.         delay(500);
  123.         digitalWrite(relay1,LOW);
  124.         ment();
  125.       }
  126.       kiir();
  127.     }
  128.   } else {  // beállítás
  129.     if (buttonShort==true and readingGomb==HIGH){
  130.       reading=false;
  131.       buttonShort=false;
  132.       Serial.println("Short Press");
  133.       menu++;
  134.       if (menu==1) {
  135.         ertek=beTime;
  136.       }
  137.       if (menu==2) {
  138.         beTime=ertek;
  139.         ertek=kiTime;
  140.       }
  141.       if (menu==3) {
  142.         kiTime=ertek;
  143.         menu=0;
  144.         setting=0;
  145.         ment();
  146.         lcd.clear();
  147.       }
  148.       kiir();
  149.     }
  150.     if (result == DIR_CCW) {
  151.       ertek++;    
  152.       if (ertek>100) ertek=100;
  153.       kiir();
  154.     }
  155.     if (result == DIR_CW) {
  156.       ertek--;    
  157.       if (ertek<10) ertek=10;
  158.       kiir();
  159.     }
  160.   }
  161.   if (buttonLong==true){
  162.     reading=false;
  163.     buttonLong=false;
  164.     Serial.println("Long Press");
  165.     fut=false;
  166.     setting=true;
  167.     beCounter=0;
  168.     kiCounter=0;
  169.     ertek=beTime;
  170.     menu=1;
  171.     kiir();
  172.   }
  173.  
  174.   currentMillis = millis();
  175.   if (currentMillis - previousMillis >= 100) {
  176.     previousMillis = currentMillis;
  177.     tikk();
  178.   }
  179.  
  180. }
  181.  
  182. void tikk() {
  183.   if (kiCounter>0 and fut==true) {
  184.     kiCounter--;
  185.     valt=true;
  186.     kiir();
  187.     if (kiCounter==0) {
  188.       beCounter=beTime;
  189.       be=false;
  190.       digitalWrite(led,LOW);
  191.       digitalWrite(relay2,LOW);
  192.     }
  193.   }
  194.   if (beCounter>0 and fut==true) {
  195.     beCounter--;
  196.     valt=true;
  197.     kiir();
  198.     if (beCounter==0) {
  199.       kiCounter=kiTime;
  200.       be=true;
  201.       digitalWrite(led,HIGH);
  202.       digitalWrite(relay2,HIGH);
  203.       darab++;
  204.     }
  205.   }
  206. }
  207.  
  208.  
  209. void kiir() {
  210.   valt=false;
  211.   if (menu==0) {
  212.     lcd.setCursor(0,0);
  213.     if (fut==false) {
  214.       lcd.print("Waiting ");
  215.     } else {
  216.       lcd.print("Running ");
  217.     }
  218.     if (be==true) {
  219.       lcd.print("*");
  220.     } else {
  221.       lcd.print(" ");
  222.     }
  223.     lcd.setCursor(10,0);
  224.     if (darab<10) lcd.print(" ");
  225.     if (darab<100) lcd.print(" ");
  226.     if (darab<1000) lcd.print(" ");
  227.     if (darab<10000) lcd.print(" ");
  228.     lcd.print(darab);
  229.     lcd.setCursor(0,1);
  230.     lcd.print("Be:");
  231.     if (beCounter<10) lcd.print(" ");
  232.     if (beCounter<100) lcd.print(" ");
  233.     if (beCounter<1000) lcd.print(" ");
  234.     lcd.print(beCounter);
  235.     lcd.print("   ");
  236.     lcd.setCursor(8,1);
  237.     lcd.print("Ki:");
  238.     if (kiCounter<10) lcd.print(" ");
  239.     if (kiCounter<100) lcd.print(" ");
  240.     if (kiCounter<1000) lcd.print(" ");
  241.     lcd.print(kiCounter);
  242.     lcd.print("   ");
  243.   }
  244.   if (menu==1) {
  245.     lcd.setCursor(0,0);
  246.     lcd.print("   Setting    ");
  247.     lcd.setCursor(0,1);
  248.     lcd.print("Be:");
  249.     if (ertek<10) lcd.print(" ");
  250.     if (ertek<100) lcd.print(" ");
  251.     if (ertek<1000) lcd.print("*");
  252.     lcd.print(ertek);
  253.     lcd.print("   ");
  254.     lcd.setCursor(7,1);
  255.     lcd.print("*Ki:");
  256.     if (kiCounter<10) lcd.print(" ");
  257.     if (kiCounter<100) lcd.print(" ");
  258.     if (kiCounter<1000) lcd.print(" ");
  259.     lcd.print(kiCounter);
  260.     lcd.print("   ");
  261.   }
  262.   if (menu==2) {
  263.     lcd.setCursor(0,0);
  264.     lcd.print("   Setting    ");
  265.     lcd.setCursor(0,1);
  266.     lcd.print("Be:");
  267.     if (beCounter<10) lcd.print(" ");
  268.     if (beCounter<100) lcd.print(" ");
  269.     if (beCounter<1000) lcd.print(" ");
  270.     lcd.print(beCounter);
  271.     lcd.print("   ");
  272.     lcd.setCursor(8,1);
  273.     lcd.print("Ki:");
  274.     if (ertek<10) lcd.print(" ");
  275.     if (ertek<100) lcd.print(" ");
  276.     if (ertek<1000) lcd.print("*");
  277.     lcd.print(ertek);
  278.     lcd.print("*");
  279.   }
  280. }
  281.  
  282. void gombnyomas() {
  283.   readingGomb = digitalRead(gomb);
  284.     if (readingGomb==LOW ) {   // meg van nyomva a gomb
  285.       if ((millis() - lastDebounceTime) > debounceDelay) {  // Letelt-e a prell idő
  286.         buttonShort=true;
  287.       }
  288.       if ((millis() - lastDebounceTime) > longPressDelay) {  //
  289.         buttonLong=true;
  290.         buttonShort=false;
  291.       }
  292.     } else {  // nincs megnyomva a gomb
  293.       lastDebounceTime=millis();
  294.       if (buttonShort==true) {
  295.         buttonShortPress=true;
  296.         buttonShort=false;
  297.       }
  298.       if (buttonLong==true) {
  299.         buttonLongPress=true;
  300.         buttonLong=false;
  301.       }
  302.     }
  303.   if (buttonShortPress==true) {
  304.     buttonShortPress=false;
  305.     Serial.println("Short Press");
  306.   }
  307.   if (buttonLongPress==true) {
  308.     reading=true;
  309.     lastDebounceTime=millis();
  310.     buttonLongPress=false;
  311.     Serial.println("Long Press");
  312.   }
  313. }
  314.  
  315. void gombnyomas1() {
  316.   readingGomb = digitalRead(gomb);
  317.   if (readingGomb==LOW and reading==true) {
  318.     buttonCounter++;
  319.     if ((millis() - lastDebounceTime) > debounceDelay) {  // Letelt-e a prell idő
  320.       buttonShort=true;
  321.     }
  322.     if ((millis() - lastDebounceTime) > longPressDelay) {  //
  323.       buttonLong=true;
  324.       buttonShort=false;
  325.     }
  326.   }// else {
  327.   if (readingGomb==HIGH) {
  328.     lastDebounceTime=millis();
  329.     buttonCounter=0;
  330.     reading=true;
  331.   }
  332. }
  333.  
  334. void ment() {
  335.   EEPROM.write(0,beTime);
  336.   EEPROM.write(1,kiTime);
  337.   int tmp=darab/256;
  338.   EEPROM.write(3,tmp);
  339.   EEPROM.write(2,darab-256*tmp);
  340. }
  341.  
  342. // END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement