Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Relé teszter
- #include <LiquidCrystal_I2C.h>
- #include <Rotary.h>
- #include <EEPROM.h>
- #define gomb A0
- #define relay1 3
- #define relay2 2
- #define led 13
- #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) // |= Bitenkénti megengedő VAGY és értékadás
- #define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit))
- float Tapfeszultseg = 0;
- unsigned long currentMillis=0;
- unsigned long previousMillis=0;
- unsigned int darab=0;
- unsigned int ertek=0;
- unsigned int beTime=0;
- unsigned int kiTime=0;
- unsigned int beCounter=0;
- unsigned int kiCounter=0;
- bool be=false; // Relé be van-e kapcsolva
- bool fut=false;
- bool valt=true; // Volt-e adat változás a kiiráshoz
- int counter=20;
- byte menu=0;
- unsigned long lastDebounceTime = 0;
- unsigned long debounceDelay = 50;
- unsigned long longPressDelay = 1000;
- unsigned long buttonCounter;
- bool readingGomb;
- bool buttonShort=false;
- bool buttonLong=false;
- bool buttonShortPress=false;
- bool buttonLongPress=false;
- bool reading=true;
- bool setting=false;
- //LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
- LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- Rotary r = Rotary(A1, A2); // define rottary encoder and pins
- void setup() {
- ADMUX = (0 << ADLAR) | (1 << REFS0) | (14 << MUX0) ;
- sbi(ADCSRA, ADSC);
- Serial.begin(115200);
- Serial.println("Start ...");
- lcd.init(); // initialize the lcd
- lcd.backlight();
- //lcd.begin(16,2);
- pinMode(gomb, INPUT_PULLUP);
- pinMode(led, OUTPUT);
- pinMode(relay1, OUTPUT);
- pinMode(relay2, OUTPUT);
- digitalWrite(relay1, LOW);
- digitalWrite(relay2, LOW);
- r.begin(true);
- beTime=EEPROM.read(0);
- kiTime=EEPROM.read(1);
- //beTime=20;
- //kiTime=40;
- darab=EEPROM.read(2)+EEPROM.read(3)*256;
- //darab=0;
- while(bit_is_set(ADCSRA,ADIF)){
- Tapfeszultseg=(1024*1.1)/(ADCL+256*ADCH );
- sbi(ADCSRA, ADSC);
- }
- Serial.println(Tapfeszultseg);
- delay(500);
- while(bit_is_set(ADCSRA,ADIF)){
- Tapfeszultseg=(1024*1.1)/(ADCL+256*ADCH );
- sbi(ADCSRA, ADSC);
- }
- Serial.println(Tapfeszultseg);
- delay(500);
- lcd.clear();
- kiir();
- Serial.println("..");
- }
- void loop() {
- while(bit_is_set(ADCSRA,ADIF)){
- Tapfeszultseg=(1024*1.1)/(ADCL+256*ADCH );
- sbi(ADCSRA, ADSC);
- }
- unsigned char result = r.process();
- if (counter>0) {
- Serial.println(Tapfeszultseg);
- counter--;
- }
- if (Tapfeszultseg<4) {
- lcd.noBacklight();
- digitalWrite(relay1, LOW);
- digitalWrite(relay2, LOW);
- digitalWrite(led, LOW);
- ment();
- digitalWrite(led, HIGH);
- }
- gombnyomas1();
- if (setting==false) { // normál
- if (buttonShort==true and readingGomb==HIGH){
- reading=false;
- buttonShort=false;
- Serial.println("Short Press");
- if (fut==false) {
- fut=true;
- beCounter=beTime;
- digitalWrite(relay1,HIGH);
- } else {
- fut=false;
- be=false;
- digitalWrite(led,LOW);
- digitalWrite(relay2,LOW);
- delay(500);
- digitalWrite(relay1,LOW);
- ment();
- }
- kiir();
- }
- } else { // beállítás
- if (buttonShort==true and readingGomb==HIGH){
- reading=false;
- buttonShort=false;
- Serial.println("Short Press");
- menu++;
- if (menu==1) {
- ertek=beTime;
- }
- if (menu==2) {
- beTime=ertek;
- ertek=kiTime;
- }
- if (menu==3) {
- kiTime=ertek;
- menu=0;
- setting=0;
- ment();
- lcd.clear();
- }
- kiir();
- }
- if (result == DIR_CCW) {
- ertek++;
- if (ertek>100) ertek=100;
- kiir();
- }
- if (result == DIR_CW) {
- ertek--;
- if (ertek<10) ertek=10;
- kiir();
- }
- }
- if (buttonLong==true){
- reading=false;
- buttonLong=false;
- Serial.println("Long Press");
- fut=false;
- setting=true;
- beCounter=0;
- kiCounter=0;
- ertek=beTime;
- menu=1;
- kiir();
- }
- currentMillis = millis();
- if (currentMillis - previousMillis >= 100) {
- previousMillis = currentMillis;
- tikk();
- }
- }
- void tikk() {
- if (kiCounter>0 and fut==true) {
- kiCounter--;
- valt=true;
- kiir();
- if (kiCounter==0) {
- beCounter=beTime;
- be=false;
- digitalWrite(led,LOW);
- digitalWrite(relay2,LOW);
- }
- }
- if (beCounter>0 and fut==true) {
- beCounter--;
- valt=true;
- kiir();
- if (beCounter==0) {
- kiCounter=kiTime;
- be=true;
- digitalWrite(led,HIGH);
- digitalWrite(relay2,HIGH);
- darab++;
- }
- }
- }
- void kiir() {
- valt=false;
- if (menu==0) {
- lcd.setCursor(0,0);
- if (fut==false) {
- lcd.print("Waiting ");
- } else {
- lcd.print("Running ");
- }
- if (be==true) {
- lcd.print("*");
- } else {
- lcd.print(" ");
- }
- lcd.setCursor(10,0);
- if (darab<10) lcd.print(" ");
- if (darab<100) lcd.print(" ");
- if (darab<1000) lcd.print(" ");
- if (darab<10000) lcd.print(" ");
- lcd.print(darab);
- lcd.setCursor(0,1);
- lcd.print("Be:");
- if (beCounter<10) lcd.print(" ");
- if (beCounter<100) lcd.print(" ");
- if (beCounter<1000) lcd.print(" ");
- lcd.print(beCounter);
- lcd.print(" ");
- lcd.setCursor(8,1);
- lcd.print("Ki:");
- if (kiCounter<10) lcd.print(" ");
- if (kiCounter<100) lcd.print(" ");
- if (kiCounter<1000) lcd.print(" ");
- lcd.print(kiCounter);
- lcd.print(" ");
- }
- if (menu==1) {
- lcd.setCursor(0,0);
- lcd.print(" Setting ");
- lcd.setCursor(0,1);
- lcd.print("Be:");
- if (ertek<10) lcd.print(" ");
- if (ertek<100) lcd.print(" ");
- if (ertek<1000) lcd.print("*");
- lcd.print(ertek);
- lcd.print(" ");
- lcd.setCursor(7,1);
- lcd.print("*Ki:");
- if (kiCounter<10) lcd.print(" ");
- if (kiCounter<100) lcd.print(" ");
- if (kiCounter<1000) lcd.print(" ");
- lcd.print(kiCounter);
- lcd.print(" ");
- }
- if (menu==2) {
- lcd.setCursor(0,0);
- lcd.print(" Setting ");
- lcd.setCursor(0,1);
- lcd.print("Be:");
- if (beCounter<10) lcd.print(" ");
- if (beCounter<100) lcd.print(" ");
- if (beCounter<1000) lcd.print(" ");
- lcd.print(beCounter);
- lcd.print(" ");
- lcd.setCursor(8,1);
- lcd.print("Ki:");
- if (ertek<10) lcd.print(" ");
- if (ertek<100) lcd.print(" ");
- if (ertek<1000) lcd.print("*");
- lcd.print(ertek);
- lcd.print("*");
- }
- }
- void gombnyomas() {
- readingGomb = digitalRead(gomb);
- if (readingGomb==LOW ) { // meg van nyomva a gomb
- if ((millis() - lastDebounceTime) > debounceDelay) { // Letelt-e a prell idő
- buttonShort=true;
- }
- if ((millis() - lastDebounceTime) > longPressDelay) { //
- buttonLong=true;
- buttonShort=false;
- }
- } else { // nincs megnyomva a gomb
- lastDebounceTime=millis();
- if (buttonShort==true) {
- buttonShortPress=true;
- buttonShort=false;
- }
- if (buttonLong==true) {
- buttonLongPress=true;
- buttonLong=false;
- }
- }
- if (buttonShortPress==true) {
- buttonShortPress=false;
- Serial.println("Short Press");
- }
- if (buttonLongPress==true) {
- reading=true;
- lastDebounceTime=millis();
- buttonLongPress=false;
- Serial.println("Long Press");
- }
- }
- void gombnyomas1() {
- readingGomb = digitalRead(gomb);
- if (readingGomb==LOW and reading==true) {
- buttonCounter++;
- if ((millis() - lastDebounceTime) > debounceDelay) { // Letelt-e a prell idő
- buttonShort=true;
- }
- if ((millis() - lastDebounceTime) > longPressDelay) { //
- buttonLong=true;
- buttonShort=false;
- }
- }// else {
- if (readingGomb==HIGH) {
- lastDebounceTime=millis();
- buttonCounter=0;
- reading=true;
- }
- }
- void ment() {
- EEPROM.write(0,beTime);
- EEPROM.write(1,kiTime);
- int tmp=darab/256;
- EEPROM.write(3,tmp);
- EEPROM.write(2,darab-256*tmp);
- }
- // END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement