Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Beat timer

Szerelo May 20th, 2020 1,249 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Beat timer v 1.0
  2.  
  3. #include <LiquidCrystal.h>
  4. #include <Encoder.h>
  5. #include "Timer.h"
  6. #include <EEPROM.h>
  7.  
  8. #define bstop A1
  9. #define bstart A0
  10. #define bzero A2
  11. #define valto A3
  12. #define relay 4
  13. #define siren 5
  14.  
  15. Encoder myEnc(2, 3);
  16. Timer t;
  17.  
  18. long oldPosition  = -999;     // Encoder starting oldposition
  19. int16_t tikktimeset = 500;    // Relay ticking time, 500=5,0 sec
  20. uint16_t tikktimeactual = 0;
  21. int relaytimeset = 50;        // Relay ontime
  22. int relaytimeactual = 0;
  23. uint16_t tikkcounter=0;
  24. uint16_t tikkalarm = 999;     // Tick counter alarm
  25. bool tikken = false;          // ON/OFF flag
  26. bool relaystatus = false;
  27. bool eepromchange = false;    // Need save parameter to EEPROM
  28. uint16_t eepromtimeset = 1000;   // How long to wait to save your data
  29. uint16_t eepromtimeactual = 0;
  30.  
  31. const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
  32. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  33.  
  34. void setup() {
  35.   pinMode(relay, OUTPUT);
  36.   pinMode(siren, OUTPUT);
  37.   pinMode(bstop, INPUT_PULLUP);
  38.   pinMode(bstart, INPUT_PULLUP);
  39.   pinMode(bzero, INPUT_PULLUP);
  40.   pinMode(valto, INPUT_PULLUP);
  41.   int tickEvent = t.every(10, tikk, (void*)2);
  42.   Serial.begin(112500);
  43.   Serial.println("Start ...");
  44.   digitalWrite(relay,LOW);
  45.   digitalWrite(siren,LOW);
  46.   lcd.begin(16, 4);
  47.   kezd();
  48. }
  49.  
  50. void loop() {
  51.   t.update();
  52.   long newPosition = myEnc.read();
  53.   if (newPosition != oldPosition) {
  54.     oldPosition = newPosition;
  55.     if (newPosition/4==1) {
  56.       Serial.println("+");
  57.       myEnc.write(0);
  58.       if (digitalRead(bzero)==LOW) {
  59.         tikkalarm++;
  60.         if (tikkalarm>999) tikkalarm=999;
  61.         lcd.setCursor(0, 3);
  62.         lcd.print("Count: ");
  63.         if (tikkcounter<100) lcd.print (" ");
  64.         if (tikkcounter<10) lcd.print (" ");
  65.         lcd.print(tikkcounter);
  66.         lcd.print("/");
  67.         if (tikkalarm<100) lcd.print (" ");
  68.         if (tikkalarm<10) lcd.print (" ");
  69.         lcd.print(tikkalarm);
  70.         eepromchange=true;
  71.         lcd.setCursor(15, 0);
  72.         lcd.print("!");
  73.       } else {
  74.         if (digitalRead(valto)==LOW) {
  75.           tikktimeset=tikktimeset+100;
  76.         } else {
  77.           tikktimeset++;
  78.         }
  79.         if (tikktimeset>9999) tikktimeset=9999;
  80.         eepromchange=true;
  81.         eepromtimeactual=0;
  82.         float tmp1=tikktimeset;
  83.         tmp1=tmp1/100;
  84.         lcd.setCursor(10, 1);
  85.         if (tikktimeset<1000) lcd.print(" ");
  86.         lcd.print(tmp1);
  87.         lcd.setCursor(15, 0);
  88.         lcd.print("!");
  89.       }
  90.     }
  91.     if (newPosition/4==-1) {
  92.       Serial.println("-");
  93.       myEnc.write(0);
  94.       if (digitalRead(bzero)==LOW) {
  95.         tikkalarm--;
  96.         if (tikkalarm<1) tikkalarm=1;
  97.         lcd.setCursor(0, 3);
  98.         lcd.print("Count: ");
  99.         if (tikkcounter<100) lcd.print (" ");
  100.         if (tikkcounter<10) lcd.print (" ");
  101.         lcd.print(tikkcounter);
  102.         lcd.print("/");
  103.         if (tikkalarm<100) lcd.print (" ");
  104.         if (tikkalarm<10) lcd.print (" ");
  105.         lcd.print(tikkalarm);
  106.         eepromchange=true;
  107.         lcd.setCursor(15, 0);
  108.         lcd.print("!");
  109.       } else {
  110.       if (digitalRead(valto)==LOW) {
  111.         tikktimeset=tikktimeset-100;
  112.       } else {
  113.         tikktimeset--;
  114.       }
  115.         if (tikktimeset<100) tikktimeset=100;
  116.         eepromchange=true;
  117.         eepromtimeactual=0;
  118.         float tmp1=tikktimeset;
  119.         tmp1=tmp1/100;
  120.         lcd.setCursor(10, 1);
  121.         if (tikktimeset<1000) lcd.print(" ");
  122.         lcd.print(tmp1);
  123.         lcd.setCursor(15, 0);
  124.         lcd.print("!");
  125.       }
  126.     }
  127.   }
  128.   if (digitalRead(bstart)==LOW) {
  129.     tikken=true;
  130.     eepromchange=true;
  131.     eepromtimeactual=0;
  132.     lcd.setCursor(12, 0);
  133.     lcd.print("ON ");
  134.     Serial.println("Start");
  135.     lcd.setCursor(15, 0);
  136.     lcd.print("!");
  137.     delay(500);
  138.   }
  139.   if (digitalRead(bstop)==LOW) {
  140.     tikken=false;
  141.     eepromchange=true;
  142.     eepromtimeactual=0;
  143.     lcd.setCursor(12, 0);
  144.     lcd.print("OFF");
  145.     Serial.println("Stop");
  146.     lcd.setCursor(15, 0);
  147.     lcd.print("!");
  148.     digitalWrite(siren,LOW);
  149.     delay(500);
  150.   }
  151.   if (digitalRead(valto)==LOW) {    //
  152.     digitalWrite(siren,LOW);
  153.   }
  154.   if (digitalRead(bzero)==LOW) {
  155.     digitalWrite(siren,LOW);
  156.     tikkcounter=0;
  157.     lcd.setCursor(0, 3);
  158.     lcd.print("Count: ");
  159.     if (tikkcounter<100) lcd.print (" ");
  160.     if (tikkcounter<10) lcd.print (" ");
  161.     lcd.print(tikkcounter);
  162.     lcd.print("/");
  163.     if (tikkalarm<100) lcd.print (" ");
  164.     if (tikkalarm<10) lcd.print (" ");
  165.     lcd.print(tikkalarm);
  166.   }
  167. }
  168.  
  169. void tikk() {
  170.   if (tikken==true){
  171.     tikktimeactual++;
  172.     long tmp;
  173.     tmp=(tikktimeset-tikktimeactual)/10+1;
  174.     lcd.setCursor(12, 2);
  175.     if (tmp<9) lcd.print(" ");
  176.     lcd.print(tmp);
  177.     lcd.print("  ");
  178.     if (tikktimeactual >= tikktimeset) {
  179.       tikktimeactual=0;
  180.       relaystatus=true;
  181.       digitalWrite(relay,HIGH);
  182.       tikkcounter++;
  183.       lcd.setCursor(0, 3);
  184.       lcd.print("Count: ");
  185.       if (tikkcounter<100) lcd.print (" ");
  186.       if (tikkcounter<10) lcd.print (" ");
  187.       lcd.print(tikkcounter);
  188.       lcd.print("/");
  189.       if (tikkalarm<100) lcd.print (" ");
  190.       if (tikkalarm<10) lcd.print (" ");
  191.       lcd.print(tikkalarm);
  192.       if (tikkcounter>=tikkalarm) {
  193.         digitalWrite(siren,HIGH);
  194.         tikkcounter=0;
  195.       }
  196.       lcd.setCursor(15, 3);
  197.       lcd.print("*");
  198.     }
  199.   }
  200.   if (relaystatus==true) {
  201.     relaytimeactual++;
  202.     if (relaytimeactual >= relaytimeset) {
  203.       relaytimeactual=0;
  204.       relaystatus=false;
  205.       digitalWrite(relay,LOW);
  206.       lcd.setCursor(15, 3);
  207.       lcd.print(" ");
  208.     }
  209.   }
  210.   if (eepromchange==true) {
  211.     eepromtimeactual++;
  212.     if (eepromtimeactual >= eepromtimeset) {
  213.       //Serial.println("EEPROM WRITE");
  214.       eepromtimeactual=0;
  215.       eepromchange=false;
  216.       byte tmp1=0;
  217.       if (tikken==true) tmp1=1;
  218.       EEPROM.write(0,tmp1);
  219.       tmp1=tikktimeset/256;
  220.       EEPROM.write(2,tmp1);
  221.       EEPROM.write(1,tikktimeset-tmp1*256);
  222.       tmp1=tikkalarm/256;
  223.       EEPROM.write(4,tmp1);
  224.       EEPROM.write(3,tikkalarm-tmp1*256);
  225.       //EEPROM.write(2,2);
  226.       //EEPROM.write(1,2);
  227.       lcd.setCursor(15, 0);
  228.       lcd.print(" ");
  229.     }
  230.   }
  231. }
  232.  
  233. void kezd() {
  234.   byte tmp=EEPROM.read(1);
  235.   tikktimeset=tmp+256*EEPROM.read(2);
  236.   tmp=EEPROM.read(3);
  237.   tikkalarm=tmp+256*EEPROM.read(4);
  238.   tikken=false;
  239.   if(EEPROM.read(0)==1) tikken=true;
  240.   lcd.print("BEAT TIMER  ");
  241.   lcd.setCursor(0, 1);
  242.   lcd.print("Set timer:");
  243.   lcd.setCursor(0, 2);
  244.   lcd.print("Remaining:");
  245.   lcd.setCursor(0, 3);
  246.   lcd.print("Count: ");
  247.   if (tikkcounter<100) lcd.print (" ");
  248.   if (tikkcounter<10) lcd.print (" ");
  249.   lcd.print(tikkcounter);
  250.   lcd.print("/");
  251.   if (tikkalarm<100) lcd.print (" ");
  252.   if (tikkalarm<10) lcd.print (" ");
  253.   lcd.print(tikkalarm);
  254.   lcd.setCursor(12, 0);
  255.   if (tikken==false) {
  256.     lcd.print("OFF");
  257.   } else {
  258.     lcd.print("ON ");
  259.   }
  260.   float tmp1=tikktimeset;
  261.   tmp1=tmp1/100;
  262.   lcd.setCursor(10, 1);
  263.   if (tikktimeset<1000) lcd.print(" ");
  264.   lcd.print(tmp1);
  265.  
  266. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top