Advertisement
Szerelo

Ventilátor időszakhoz kötött vezérlése (nappal/éjszaka)

Jun 7th, 2018
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.53 KB | None | 0 0
  1. //https://arduino-info.wikispaces.com/LCD-Blue-I2C
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include <Timer.h>
  5. #include <EEPROM.h>
  6. #include <Time.h>
  7. #include <DS1302.h>
  8.  
  9. DS1302 rtc(10, 9, 8);
  10.  
  11. Timer t;
  12. Time ti = rtc.time();
  13.  
  14. byte Motorok[23];   //Motorok PWM értékei: Motor1 éjjeli, Motor2 éjjeli, Motor1 nappali, Motor2 nappali, Motor1 turbo, Motor2 turbo, turbo idő,
  15. String sz[24];
  16. int Turbo=0;      //Turbo üzemmód be van-e kapcsolva
  17.  
  18. String tmp="";
  19. byte Ido=0;
  20. byte TPerc=0;
  21. byte TSec=0;
  22. byte menu=0;
  23. byte menuval=0;
  24. byte lighttime=40;
  25. byte lightt=30;
  26. bool light=true;
  27. byte naptar[7];
  28. int kikapcs=65535;
  29. int kikapcs1=65535;
  30.  
  31. #define GombSetting 7  //Beállítás mód
  32. #define GombEjjel A3
  33. #define GombNappal A2
  34. #define GombTurbo A1
  35. #define GombStop A0
  36.  
  37. #define Relay1 4
  38. #define Relay2 3
  39.  
  40. #define Motor1 5
  41. #define Motor2 6
  42.  
  43. // Set the pins on the I2C chip used for LCD connections:
  44. //                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  45. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
  46.  
  47. void setup()
  48. {
  49.   //rtc.writeProtect(false);
  50.   //Time ti(2018, 4, 26, 21, 40, 50, Time::kThursday);
  51.   //rtc.time(ti);
  52.   tone(12,700,200);
  53.   delay(200);
  54.   tone(12,600,200);
  55.   delay(200);
  56.   tone(12,500,200);
  57.   Serial.begin(115200);
  58.   Serial.print("Setup ");
  59.   pinMode(GombSetting, INPUT);
  60.   pinMode(GombEjjel, INPUT);
  61.   pinMode(GombNappal, INPUT);
  62.   pinMode(GombTurbo, INPUT);
  63.   pinMode(GombStop, INPUT);
  64.   digitalWrite(GombSetting, HIGH);
  65.   digitalWrite(GombEjjel, HIGH);
  66.   digitalWrite(GombNappal, HIGH);
  67.   digitalWrite(GombTurbo, HIGH);
  68.   digitalWrite(GombStop, HIGH);
  69.  
  70.   pinMode(Motor1, OUTPUT);
  71.   pinMode(Motor2, OUTPUT);
  72.   pinMode(Relay1, OUTPUT);
  73.   pinMode(Relay2, OUTPUT);
  74.   digitalWrite(Relay1, LOW);
  75.   digitalWrite(Relay2, LOW);
  76.  
  77.   lcd.begin(16,4);   // lcd alaphelyzetbe állítás
  78.   Serial.println("LCD start");
  79.   lcd.clear();
  80.   lcd.setCursor(0,0);
  81.   lcd.print("OK");
  82.   sz[0]="Ejjel Motor1";
  83.   sz[1]="Ejjel Motor2";
  84.   sz[2]="Nappal Motor1";
  85.   sz[3]="Nappal Motor2";
  86.   sz[4]="Turbo Motor1";
  87.   sz[5]="Turbo Motor2";
  88.   sz[6]="Turbo ido perc";
  89.   sz[7]="Bekapcsolas Ora";
  90.   sz[8]="Bekapcsolas Perc";
  91.   sz[9]="Kikapcsolas Ora";
  92.   sz[10]="Kikapcsolas Perc";
  93.   sz[11]="Vasarnap";
  94.   sz[12]="Hetfo";
  95.   sz[13]="Kedd";
  96.   sz[14]="Szerda";
  97.   sz[15]="Csutortok";
  98.   sz[16]="Pentek";
  99.   sz[17]="Szombat";
  100.   sz[18]="Ev (2000+)";
  101.   sz[19]="Honap (1-12)";
  102.   sz[20]="Nap (1-31!)";
  103.   sz[21]="Ora (0-23)";
  104.   sz[22]="Perc (0-59)";
  105.   sz[23]="Nap neve (1-7)";
  106.   sz[24]="Save";
  107.  
  108.   delay(500);
  109. //  lcd.noBacklight();
  110. //  delay(500);
  111. //  lcd.backlight();
  112.   Serial.println(".");
  113.  
  114. int tickEvent = t.every(1000, tikkel, 2000 );
  115.   Serial.print("1 second tikkelés id=");
  116.   Serial.println(tickEvent);
  117.   delay(500);
  118. //Motorok sebesség és turbo idő beolvasása
  119.    for (int i=0; i <= 17; i++){
  120.       Motorok[i]=EEPROM.read(i);
  121.    }
  122.   Motorok[18]=ti.yr-2000;
  123.   Motorok[19]=ti.mon;
  124.   Motorok[20]=ti.date;
  125.   Motorok[21]=ti.hr;
  126.   Motorok[22]=ti.min;
  127.   Motorok[23]=ti.day;
  128.   naptartxt();
  129.   Ido=Motorok[6];
  130.   lcd.backlight();
  131.   for (int i=9; i = 0; i--){
  132.     lcd.setCursor(i,0);
  133.     lcd.print("Hello! ");
  134.     delay(100);
  135.   }
  136.   ejjelitxt();  //Reset utáni kezdő lépernyő
  137. }
  138.  
  139. void loop() {
  140.   t.update();                       //Timer frissítés
  141.   if (digitalRead(GombSetting)==1) {
  142.     //Normál ág
  143.     if (digitalRead(GombEjjel)==0 and light==true) {
  144.       lighttime=lightt;
  145.       Serial.println ("Ejjel");
  146.       ejjelitxt();
  147.       tone(12,700,200);
  148.       delay(500);
  149.     }
  150.     if (digitalRead(GombEjjel)==0) {
  151.       lighttime=lightt;
  152.       lcd.backlight();
  153.       light=true;
  154.       delay(1000);
  155.     }
  156.  
  157.     if (digitalRead(GombNappal)==0 and light==true) {
  158.       lighttime=lightt;
  159.       Serial.println ("Nappal");
  160.       nappalitxt();
  161.       tone(12,700,200);
  162.       delay(500);
  163.     }
  164.     if (digitalRead(GombNappal)==0) {
  165.       lighttime=lightt;
  166.       lcd.backlight();
  167.       light=true;
  168.       delay(1000);
  169.     }
  170.  
  171.     if (digitalRead(GombStop)==0 and light==true) {
  172.       lighttime=lightt;
  173.       Serial.println ("Stop");
  174.       stoptxt();
  175.       tone(12,700,200);
  176.       delay(500);
  177.     }
  178.     if (digitalRead(GombStop)==0) {
  179.       lighttime=lightt;
  180.       lcd.backlight();
  181.       light=true;
  182.       delay(1000);
  183.     }
  184.  
  185.     if (digitalRead(GombTurbo)==0 and Turbo==0 and light==true) {
  186.       lighttime=lightt;
  187.       Serial.println (Motorok[6]);
  188.       Serial.println ("Turbo");
  189.       Turbo=1;
  190.       //Perc=Motorok[6];
  191.       TPerc=Ido;
  192.       TSec=0;
  193.       digitalWrite(Relay1,HIGH);
  194.       digitalWrite(Relay2,HIGH);
  195.       analogWrite(Motor1,Motorok[4]);
  196.       analogWrite(Motor2,Motorok[5]);
  197.       turbotxt();
  198.       tone(12,700,200);
  199.       delay(100);
  200.     }
  201.     if (digitalRead(GombTurbo)==0) {
  202.       lighttime=lightt;
  203.       lcd.backlight();
  204.       light=true;
  205.       delay(1000);
  206.     }
  207.  
  208.   }
  209.   else {
  210. //Beállítás ág
  211.     lcd.backlight();
  212.     Serial.println ("Beállitas");
  213.     analogWrite(Motor1,0);
  214.     analogWrite(Motor2,1);
  215.     digitalWrite(Relay1, LOW);
  216.     digitalWrite(Relay2, LOW);
  217.     delay(100);
  218.     beallittxt();
  219.     delay(1000);
  220.     beallitsz();
  221.     while (digitalRead(GombSetting)==0) {
  222.       if (digitalRead(GombStop)==0 ) {
  223.         Serial.println ("Stop gomb beallitas");
  224.         menu -= 1;
  225.         if (menu==255) {
  226.           menu=0;
  227.         }
  228.         beallitsz();
  229.         tone(12,700,200);
  230.         delay(500);  
  231.       }
  232.       if (digitalRead(GombTurbo)==0 ) {
  233.         Serial.println ("Turbo gomb beallitas");
  234.         menu += 1;
  235.         if (menu==24) {
  236.           menu=23;
  237.         }
  238.         beallitsz();  
  239.         tone(12,700,200);
  240.         delay(500);  
  241.       }
  242.       if (digitalRead(GombEjjel)==0 ) {
  243.         Serial.println ("Ejjel gomb beallitas");
  244.         Motorok[menu] += 1;
  245.         beallitsz();  
  246.         tone(12,700,200);
  247.         delay(200);  
  248.       }
  249.       if (digitalRead(GombNappal)==0 ) {
  250.         Serial.println ("Nappal gomb beallitas");
  251.         menuval=Motorok[menu];
  252.         Motorok[menu] -= 1;
  253.         if (Motorok[menu]==255 and menu>=6 ) {
  254.           Motorok[menu]=0;
  255.         }
  256.         beallitsz();  
  257.         tone(12,700,200);
  258.         delay(200);  
  259.       }
  260.    
  261.     }
  262.     Serial.print("Save");
  263.     for (int i=0; i <= 17; i++){
  264.       EEPROM.write(i,Motorok[i]);
  265.     }
  266.     Ido=Motorok[6];   //Turbo idő
  267.     rtc.writeProtect(false);
  268.     rtc.halt(false);
  269.     ti.yr=Motorok[18]+2000;
  270.     ti.mon=Motorok[19];
  271.     ti.date=Motorok[20];
  272.     ti.hr=Motorok[21];
  273.     ti.min=Motorok[22];
  274.     ti.day=Motorok[23];
  275.     rtc.time(ti);    
  276.     rtc.writeProtect(true);
  277.    
  278.     lcd.clear();
  279.     lcd.setCursor(4,0);
  280.     lcd.print("Valassz");
  281.     lcd.setCursor(2,1);
  282.     lcd.print("uzemmodot");
  283.   }
  284. }
  285.  
  286. void beallitsz(void) {
  287.   lcd.clear();
  288.   lcd.setCursor(0,0);
  289.   lcd.print(sz[menu]);
  290.   lcd.setCursor(14,3);
  291.   lcd.print(menu);
  292.   lcd.setCursor(6,1);
  293.   lcd.print(Motorok[menu]);  
  294.   delay(100);
  295. }
  296.  
  297. void beallittxt(void) {
  298.   Turbo=0;
  299.   digitalWrite(Relay1,LOW);
  300.   digitalWrite(Relay2,LOW);
  301.   lcd.clear();
  302.   lcd.setCursor(0,0);
  303.   lcd.print("Beallitas");
  304. }
  305.  
  306. void ejjelitxt(void) {
  307.   Turbo=0;
  308.   digitalWrite(Relay1,HIGH);
  309.   digitalWrite(Relay2,HIGH);
  310.   analogWrite(Motor1,Motorok[0]);
  311.   analogWrite(Motor2,Motorok[1]);
  312.   lcd.clear();
  313.   lcd.setCursor(1,0);
  314.   lcd.print("Ejjeli uzemmod");
  315. }
  316.  
  317. void nappalitxt(void) {
  318.   Turbo=0;
  319.   digitalWrite(Relay1,HIGH);
  320.   digitalWrite(Relay2,HIGH);
  321.   analogWrite(Motor1,Motorok[2]);
  322.   analogWrite(Motor2,Motorok[3]);
  323.   lcd.clear();
  324.   lcd.setCursor(1,0);
  325.   lcd.print("Nappali uzemmod");
  326. }
  327.  
  328. void stoptxt(void) {
  329.   Turbo=0;
  330.   digitalWrite(Relay1,LOW);
  331.   digitalWrite(Relay2,LOW);
  332.   analogWrite(Motor1,0);
  333.   analogWrite(Motor2,0);
  334.   lcd.clear();
  335.   lcd.setCursor(6,0);
  336.   lcd.print("STOP");
  337. }
  338.  
  339. void turbotxt(void) {
  340.   lcd.clear();
  341.   lcd.setCursor(5,0);
  342.   lcd.print("TURBO");
  343.   lcd.setCursor(0,1);
  344.   tmp="";
  345.   if (TPerc<=9){
  346.     tmp="0";
  347.   }
  348.   lcd.setCursor(5,1);
  349.   lcd.print(tmp);
  350.   lcd.print(TPerc);
  351.   lcd.print(":");
  352.   tmp="";
  353.   if (TSec<=9){
  354.     tmp="0";
  355.   }
  356.   lcd.print(tmp);
  357.   lcd.print(TSec);
  358. }
  359.  
  360. void naptartxt() {
  361.   Time ti=rtc.time();
  362.   naptar[0]=ti.yr-2000;
  363.   naptar[1]=ti.mon;
  364.   naptar[2]=ti.date;
  365.   naptar[3]=ti.hr;
  366.   naptar[4]=ti.min;
  367.   naptar[5]=ti.sec;
  368.   naptar[6]=ti.day;
  369.   lcd.setCursor(3,3);
  370.   lcd.print(ti.yr);
  371.   lcd.print("-");
  372.   tmp=""; if (ti.mon<=9) { tmp="0"; }
  373.   lcd.print(tmp);
  374.   lcd.print(ti.mon);
  375.   lcd.print("-");
  376.   tmp=""; if (ti.date<=9) { tmp="0"; }
  377.   lcd.print(tmp);
  378.   lcd.print(ti.date);
  379.   lcd.print("  ");
  380.   lcd.setCursor(3,2);
  381.   tmp=""; if (ti.hr<=9) { tmp="0"; }
  382.   lcd.print(tmp);
  383.   lcd.print(ti.hr);
  384.   lcd.print(":");
  385.   tmp=""; if (ti.min<=9) { tmp="0"; }
  386.   lcd.print(tmp);
  387.   lcd.print(ti.min);
  388.   lcd.print(":");
  389.   tmp=""; if (ti.sec<=9) { tmp="0"; }
  390.   lcd.print(tmp);
  391.   lcd.print(ti.sec);
  392.   lcd.print("   ");
  393.   lcd.setCursor(3,1);
  394.   lcd.print(sz[11+ti.day]);
  395.   lcd.print("    ");
  396.   lcd.setCursor(14,1);
  397.   lcd.print(Motorok[11+naptar[6]]);
  398.   //lcd.print(ti.sec);
  399.   //lcd.print(" ");
  400.   //lcd.print(":");
  401.   //lcd.print(ti.sec);
  402. }
  403.  
  404. void tikkel() {
  405.   naptartxt();
  406.   lighttime -= 1;
  407.   if (lighttime == 0) {
  408.     lcd.noBacklight();
  409.     lighttime=lightt;
  410.     light=false;
  411.   }
  412.   //Serial.println(millis() );
  413.   if (Turbo==1) {
  414.     if (TSec!=0) {
  415.       TSec --;
  416.     }
  417.     Serial.print(TPerc);
  418.     Serial.print(":");
  419.     Serial.println(TSec);
  420.     turbotxt();
  421.     if (TPerc==0 and TSec==0) {
  422.       Serial.println("Turbo vége");
  423.       Turbo=0;
  424.       delay(1000);
  425.       nappalitxt();
  426.     }
  427.     if (TSec==0) {
  428.       TSec=60;
  429.       TPerc --;
  430.     }
  431.   }
  432.  
  433.   Serial.print("naptar: ");
  434.   Serial.print(naptar[6]);
  435.   Serial.print("  bool: ");
  436.   Serial.println(Motorok[10+naptar[6]]);    //naptar(6)-ban a napok száma, 0 - nincs, hiba, 1 - vasárnap, 2 - hétfő ...
  437.   if (Motorok[10+naptar[6]] == 1) { //Motorok (10)-től, hogy azon a napon kell-e
  438. /*
  439.     Serial.print(Motorok[7]);
  440.     Serial.print(" ");
  441.     Serial.print(Motorok[8]);
  442.     Serial.print(" ");
  443.     Serial.print(Motorok[9]);
  444.     Serial.print(" ");
  445.     Serial.print(Motorok[10]);
  446.     Serial.print(" --- ");
  447.     Serial.print(naptar[3]);
  448.     Serial.print(" nap ");
  449.     Serial.println(naptar[4]);
  450. */
  451.     if (Motorok[7]==naptar[3] and Motorok[8]==naptar[4] and naptar[5]==0) {
  452.       Serial.println("BE nappali automata");
  453.       nappalitxt();
  454.     }
  455.     if (Motorok[9]==naptar[3] and Motorok[10]==naptar[4] and naptar[5]==0) {
  456.       ejjelitxt();
  457.       Serial.println("KI nappali automata");
  458.     }
  459.   }
  460.  
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement