Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Beat timer v 1.0
- #include <LiquidCrystal.h>
- #include <Encoder.h>
- #include "Timer.h"
- #include <EEPROM.h>
- #define bstop A1
- #define bstart A0
- #define bzero A2
- #define valto A3
- #define relay 4
- #define siren 5
- Encoder myEnc(2, 3);
- Timer t;
- long oldPosition = -999; // Encoder starting oldposition
- int16_t tikktimeset = 500; // Relay ticking time, 500=5,0 sec
- uint16_t tikktimeactual = 0;
- int relaytimeset = 50; // Relay ontime
- int relaytimeactual = 0;
- uint16_t tikkcounter=0;
- uint16_t tikkalarm = 999; // Tick counter alarm
- bool tikken = false; // ON/OFF flag
- bool relaystatus = false;
- bool eepromchange = false; // Need save parameter to EEPROM
- uint16_t eepromtimeset = 1000; // How long to wait to save your data
- uint16_t eepromtimeactual = 0;
- const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- void setup() {
- pinMode(relay, OUTPUT);
- pinMode(siren, OUTPUT);
- pinMode(bstop, INPUT_PULLUP);
- pinMode(bstart, INPUT_PULLUP);
- pinMode(bzero, INPUT_PULLUP);
- pinMode(valto, INPUT_PULLUP);
- int tickEvent = t.every(10, tikk, (void*)2);
- Serial.begin(112500);
- Serial.println("Start ...");
- digitalWrite(relay,LOW);
- digitalWrite(siren,LOW);
- lcd.begin(16, 4);
- kezd();
- }
- void loop() {
- t.update();
- long newPosition = myEnc.read();
- if (newPosition != oldPosition) {
- oldPosition = newPosition;
- if (newPosition/4==1) {
- Serial.println("+");
- myEnc.write(0);
- if (digitalRead(bzero)==LOW) {
- tikkalarm++;
- if (tikkalarm>999) tikkalarm=999;
- lcd.setCursor(0, 3);
- lcd.print("Count: ");
- if (tikkcounter<100) lcd.print (" ");
- if (tikkcounter<10) lcd.print (" ");
- lcd.print(tikkcounter);
- lcd.print("/");
- if (tikkalarm<100) lcd.print (" ");
- if (tikkalarm<10) lcd.print (" ");
- lcd.print(tikkalarm);
- eepromchange=true;
- lcd.setCursor(15, 0);
- lcd.print("!");
- } else {
- if (digitalRead(valto)==LOW) {
- tikktimeset=tikktimeset+100;
- } else {
- tikktimeset++;
- }
- if (tikktimeset>9999) tikktimeset=9999;
- eepromchange=true;
- eepromtimeactual=0;
- float tmp1=tikktimeset;
- tmp1=tmp1/100;
- lcd.setCursor(10, 1);
- if (tikktimeset<1000) lcd.print(" ");
- lcd.print(tmp1);
- lcd.setCursor(15, 0);
- lcd.print("!");
- }
- }
- if (newPosition/4==-1) {
- Serial.println("-");
- myEnc.write(0);
- if (digitalRead(bzero)==LOW) {
- tikkalarm--;
- if (tikkalarm<1) tikkalarm=1;
- lcd.setCursor(0, 3);
- lcd.print("Count: ");
- if (tikkcounter<100) lcd.print (" ");
- if (tikkcounter<10) lcd.print (" ");
- lcd.print(tikkcounter);
- lcd.print("/");
- if (tikkalarm<100) lcd.print (" ");
- if (tikkalarm<10) lcd.print (" ");
- lcd.print(tikkalarm);
- eepromchange=true;
- lcd.setCursor(15, 0);
- lcd.print("!");
- } else {
- if (digitalRead(valto)==LOW) {
- tikktimeset=tikktimeset-100;
- } else {
- tikktimeset--;
- }
- if (tikktimeset<100) tikktimeset=100;
- eepromchange=true;
- eepromtimeactual=0;
- float tmp1=tikktimeset;
- tmp1=tmp1/100;
- lcd.setCursor(10, 1);
- if (tikktimeset<1000) lcd.print(" ");
- lcd.print(tmp1);
- lcd.setCursor(15, 0);
- lcd.print("!");
- }
- }
- }
- if (digitalRead(bstart)==LOW) {
- tikken=true;
- eepromchange=true;
- eepromtimeactual=0;
- lcd.setCursor(12, 0);
- lcd.print("ON ");
- Serial.println("Start");
- lcd.setCursor(15, 0);
- lcd.print("!");
- delay(500);
- }
- if (digitalRead(bstop)==LOW) {
- tikken=false;
- eepromchange=true;
- eepromtimeactual=0;
- lcd.setCursor(12, 0);
- lcd.print("OFF");
- Serial.println("Stop");
- lcd.setCursor(15, 0);
- lcd.print("!");
- digitalWrite(siren,LOW);
- delay(500);
- }
- if (digitalRead(valto)==LOW) { //
- digitalWrite(siren,LOW);
- }
- if (digitalRead(bzero)==LOW) {
- digitalWrite(siren,LOW);
- tikkcounter=0;
- lcd.setCursor(0, 3);
- lcd.print("Count: ");
- if (tikkcounter<100) lcd.print (" ");
- if (tikkcounter<10) lcd.print (" ");
- lcd.print(tikkcounter);
- lcd.print("/");
- if (tikkalarm<100) lcd.print (" ");
- if (tikkalarm<10) lcd.print (" ");
- lcd.print(tikkalarm);
- }
- }
- void tikk() {
- if (tikken==true){
- tikktimeactual++;
- long tmp;
- tmp=(tikktimeset-tikktimeactual)/10+1;
- lcd.setCursor(12, 2);
- if (tmp<9) lcd.print(" ");
- lcd.print(tmp);
- lcd.print(" ");
- if (tikktimeactual >= tikktimeset) {
- tikktimeactual=0;
- relaystatus=true;
- digitalWrite(relay,HIGH);
- tikkcounter++;
- lcd.setCursor(0, 3);
- lcd.print("Count: ");
- if (tikkcounter<100) lcd.print (" ");
- if (tikkcounter<10) lcd.print (" ");
- lcd.print(tikkcounter);
- lcd.print("/");
- if (tikkalarm<100) lcd.print (" ");
- if (tikkalarm<10) lcd.print (" ");
- lcd.print(tikkalarm);
- if (tikkcounter>=tikkalarm) {
- digitalWrite(siren,HIGH);
- tikkcounter=0;
- }
- lcd.setCursor(15, 3);
- lcd.print("*");
- }
- }
- if (relaystatus==true) {
- relaytimeactual++;
- if (relaytimeactual >= relaytimeset) {
- relaytimeactual=0;
- relaystatus=false;
- digitalWrite(relay,LOW);
- lcd.setCursor(15, 3);
- lcd.print(" ");
- }
- }
- if (eepromchange==true) {
- eepromtimeactual++;
- if (eepromtimeactual >= eepromtimeset) {
- //Serial.println("EEPROM WRITE");
- eepromtimeactual=0;
- eepromchange=false;
- byte tmp1=0;
- if (tikken==true) tmp1=1;
- EEPROM.write(0,tmp1);
- tmp1=tikktimeset/256;
- EEPROM.write(2,tmp1);
- EEPROM.write(1,tikktimeset-tmp1*256);
- tmp1=tikkalarm/256;
- EEPROM.write(4,tmp1);
- EEPROM.write(3,tikkalarm-tmp1*256);
- //EEPROM.write(2,2);
- //EEPROM.write(1,2);
- lcd.setCursor(15, 0);
- lcd.print(" ");
- }
- }
- }
- void kezd() {
- byte tmp=EEPROM.read(1);
- tikktimeset=tmp+256*EEPROM.read(2);
- tmp=EEPROM.read(3);
- tikkalarm=tmp+256*EEPROM.read(4);
- tikken=false;
- if(EEPROM.read(0)==1) tikken=true;
- lcd.print("BEAT TIMER ");
- lcd.setCursor(0, 1);
- lcd.print("Set timer:");
- lcd.setCursor(0, 2);
- lcd.print("Remaining:");
- lcd.setCursor(0, 3);
- lcd.print("Count: ");
- if (tikkcounter<100) lcd.print (" ");
- if (tikkcounter<10) lcd.print (" ");
- lcd.print(tikkcounter);
- lcd.print("/");
- if (tikkalarm<100) lcd.print (" ");
- if (tikkalarm<10) lcd.print (" ");
- lcd.print(tikkalarm);
- lcd.setCursor(12, 0);
- if (tikken==false) {
- lcd.print("OFF");
- } else {
- lcd.print("ON ");
- }
- float tmp1=tikktimeset;
- tmp1=tmp1/100;
- lcd.setCursor(10, 1);
- if (tikktimeset<1000) lcd.print(" ");
- lcd.print(tmp1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement