Advertisement
untoha

derevnya_main

Mar 12th, 2020
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. // основной микроконтроллер моей деревни =)  
  2.  
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #include <DS1302.h>          
  6.  
  7. LiquidCrystal_I2C lcd(0x3F,20,4);
  8. DS1302 rtc(10, 9, 8);
  9.  
  10. int relay_1 = 2;
  11. int relay_2 = 3;
  12. int relay_3 = 4;
  13. int btn_pin = A1;
  14. int btn_accuracy = 3;       // значение кнопки бывает +-1 . 3 с запасом
  15. int relays_pins[3] = {2,3,4};         // пины реле
  16. bool relays_state[3] = {false,false,false};   // состояния реле
  17. int waiters[4] = {0,10,0,60};      // counter,max,counter,max..
  18. bool inmenu = false;
  19. bool antikrot = false;            // включен ли анти крот
  20.  
  21. void setup() {
  22.   Serial.begin(9600);  
  23.   lcd.init();                      // Инициализация дисплея  
  24.   lcd.backlight();                 // Подключение подсветки
  25.   //lcd.setCursor(0,0);              // Установка курсора в начало первой строки
  26.   //lcd.print("Hello");       // Набор текста на первой строке
  27.   //lcd.setCursor(0,1);              // Установка курсора в начало второй строки
  28.   //lcd.print("Toha.");       // Набор текста на второй строке
  29.   printline("hello",0);
  30.   printline("           toha.",1);
  31.  
  32.   for (int i=0; i<=sizeof(relays_pins)-1; i++){   //выключаем все реле
  33.      digitalWrite(relays_pins[i], HIGH);
  34.      pinMode(relays_pins[i], OUTPUT); }
  35.  
  36.   /*rtc.writeProtect(false);        // init time
  37.   rtc.halt(false);
  38.   // Make a new time object to set the date and time.
  39.   // Sunday, September 22, 2013 at 01:38:50.
  40.   Time t(2020, 3, 4, 22, 50, 50, Time::kSunday);
  41.   // Set the time and date on the chip.
  42.   rtc.time(t);*/
  43.  
  44.   //EEPROM.write(0, 30);        //  set temperature_tostart  
  45.   //EEPROM.write(3, 5);         // set hysteresis
  46. }
  47.  
  48. void loop() {  
  49.   //checktime();  
  50.   checkbuttons();
  51.   checkradio();
  52.   if (!inmenu)
  53.     updatewaiters();
  54.    
  55.   delay(1000);  
  56. }
  57.  
  58. void updatewaiters(){
  59.     for (int i=0; i<=(sizeof(waiters)/2)-1; i=i+2){
  60.       waiters[i] = waiters[i] + 1;
  61.       if (waiters[i] > waiters[i+1]){
  62.         waiters[i] = 0;
  63.         managewaiters(i);
  64.       }
  65.     }    
  66. }
  67.  
  68. void managewaiters(int id){
  69.   switch (id)
  70.   {
  71.   case 0:       // led monitor
  72.     lcd.noBacklight();
  73.     break;
  74.   case 2:    
  75.     anti_krot();
  76.     break;
  77.   default:  
  78.     break;
  79.   }
  80. }
  81.  
  82. void checkradio(){
  83.   if (digitalRead(11) == HIGH)    // B
  84.     relaymanager(1);  
  85.   if (digitalRead(12) == HIGH)    // D  unused
  86.     Serial.println("RADIO");      
  87.   if (digitalRead(7) == HIGH)    // C  unused
  88.     Serial.println("RADIO");    
  89.   if (digitalRead(6) == HIGH)    // A
  90.     relaymanager(0);    
  91. }
  92.  
  93. void checkbuttons(){    
  94.   int btn_val = analogRead(btn_pin) - 15;     // для истории. при подключении радиомодуля, значения кнопок увеличились. хз.
  95.   //msg(btn_val);
  96.   if (btn_val > 400-btn_accuracy && btn_val < 400+btn_accuracy)
  97.     relaymanager(0);
  98.   if (btn_val > 200-btn_accuracy && btn_val < 200+btn_accuracy)
  99.     relaymanager(1);
  100.   //if (btn_val > 30-btn_accuracy && btn_val < 30+btn_accuracy)
  101.   //if (btn_val > 85-btn_accuracy && btn_val < 85+btn_accuracy)
  102.   if (btn_val > 0-btn_accuracy && btn_val < 0+btn_accuracy){
  103.     if (!antikrot){
  104.       antikrot = true;
  105.       printline("anti_krot", 0);
  106.       printline("turned on", 1);
  107.     }
  108.     else{
  109.       antikrot = false;
  110.       printline("anti_krot", 0);
  111.       printline("turned off", 1);
  112.     }
  113.   }
  114. }
  115.  
  116. void checktime(){
  117.   Time t = rtc.time();
  118.   //Serial.println(t.hr);
  119.   //Serial.println(t.min);
  120.   Serial.println(t.sec);
  121. }
  122.  
  123. void radio(){
  124.   if (digitalRead(5) == HIGH){      // тупо читать пины    
  125.   }
  126. }
  127.  
  128. void relaymanager(int id){
  129.   if (relays_state[id]){
  130.     digitalWrite(relays_pins[id], HIGH);
  131.     relays_state[id] = false;
  132.     printline("relay id:"+String(id), 0);
  133.     printline("turned off", 1);
  134.   }
  135.   else{
  136.     digitalWrite(relays_pins[id], LOW);
  137.     relays_state[id] = true;
  138.     printline("relay id:"+String(id), 0);
  139.     printline("turned on", 1);
  140.   }
  141. }
  142.  
  143. void printline(String str, int line) {
  144.   waiters[0] = 1;
  145.   lcd.backlight();
  146.   lcd.setCursor(0, line);
  147.   lcd.print("                ");       // clear line
  148.   lcd.setCursor(0, line);
  149.   lcd.print(str);  
  150. }
  151.  
  152. void anti_krot(){
  153.   if (!antikrot)
  154.     return;
  155.   for (int i=1; i<=5; i++){
  156.     digitalWrite(relays_pins[2], LOW);
  157.     delay(200);  
  158.     digitalWrite(relays_pins[2], HIGH);
  159.     delay(200);  
  160.   }
  161.     digitalWrite(relays_pins[2], LOW);
  162.     delay(5000);  
  163.     digitalWrite(relays_pins[2], HIGH);
  164. }
  165.  
  166. void menu(){        // хз надо ли это меню
  167.   inmenu = true;
  168. }
  169.  
  170. void msg(int str){
  171.   //String m = " "+str;
  172.   Serial.println(str);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement