• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
problem z timerem
#1
Witam wszystkich. Jestem początkującym Arduinistą i nie wiele jeszcze kumam. Proszę o pomoc w rozwiązaniu problemu.
Aby zrobić zegar z timerami na UNO + LCD Keypad + DS1302 + moduł przekaźników poskładałem znalezione kody
i co nieco działa tzn. zegar działa, dwa timery działają, ale z jednym włączeniem i jednym wyłączeniem.
Jak próbuję zrobić więcej włączeń i wyłączeń i kod nawet się skompiluje i wgra, to zawsze jest konflikt na portach dla przekaźników.
Cały kod poniżej.

Kod:
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <virtuabotixRTC.h>

// relay pins
#define relay1      0
#define relay2      1

// define pins of RTC module

#define rtcSCLK     A3
#define rtcIO       A2
#define rtcCE       A1
#define LCD_BACKLIGHT 10

// creation of RTC object
virtuabotixRTC myRTC(rtcSCLK, rtcIO, rtcCE);

// keypad definitions
#define btnRIGHT    0
#define btnUP       1
#define btnDOWN     2
#define btnLEFT     3
#define btnSELECT   4
#define btnNONE     5

// mode definitions
#define modeSETUP   1
#define modeNORMAL  2

int mode = 1; // set default mode

// EEPROM adrressing
#define adr1ON    2
#define adr1OF    4
#define adr2ON    6
#define adr2OF    8
#define adr3ON    10
#define adr3OF    12
#define adr4ON    14
#define adr4OF    16
#define adr5ON    18
#define adr5OF    20
#define adr6ON    22
#define adr6OF    24
#define adr7ON    26
#define adr7OF    28
#define adr8ON    30
#define adr8OF    32

int eepromMin = 0;
int eepromHour = 0;

int eepromHourON = 0;
int eepromHourOF = 0;
int eepromMinON = 0;
int eepromMinOF = 0;

// pins used on LCD Keypad Shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_key     = 0;
int adc_key_in  = 0;

void setup() {
    
    //Serial.begin(9600);
    // digital pin output
    for (int i = 0; i <= 13; i++) {
        pinMode(i, OUTPUT);
    }

    // activate RTC module
       pinMode(rtcSCLK, OUTPUT);
    pinMode(rtcIO,   OUTPUT);
    pinMode(rtcCE,   OUTPUT);
       pinMode(LCD_BACKLIGHT,   OUTPUT);
       
       delay(500);

    // lcd initialization
    lcd.begin(16, 2);
       if(millis() < 3000)
       {
         digitalWrite(LCD_BACKLIGHT, HIGH);
         //analogWrite(LCD_BACKLIGHT, 255);
       }
     
       else
        {
         digitalWrite(LCD_BACKLIGHT, LOW);
         //analogWrite(LCD_BACKLIGHT, 128);
        }
}
       
void loop() {

    lcd.setCursor(0, 0);
    displayTime();

    // set & relay data eeprom
    lcd.setCursor(0, 1);
    lcd.print("1");
    relayAction(adr1ON, adr1OF, 1, relay1);

    lcd.setCursor(8, 1);
    lcd.print("2");
    relayAction(adr2ON, adr2OF, 9, relay2);
        
    // user SELECT, menu setting

    if (read_LCD_buttons() == btnSELECT) {
        while (read_LCD_buttons() == btnSELECT);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("UP. TIME SETTING");
        lcd.setCursor(0, 1);
        lcd.print("DN. PIN SETTING");
        while (read_LCD_buttons() == btnNONE);
        if (read_LCD_buttons() == btnUP) {
            while (read_LCD_buttons() == btnUP);
            setRTC();   //user UP, menu setting RTC
        }
        else if (read_LCD_buttons() == btnDOWN) {
            while (read_LCD_buttons() == btnDOWN);
            setPin();   //user DOWN, menu setting PIN
        }
    }
}

void relayAction(int adrON, int adrOF, int pos, int pin) {
    myRTC.updateTime();

    int MinToday = (myRTC.hours * 60) + myRTC.minutes;
    int MinEprON = (EEPROM.read(adrON) * 60) + EEPROM.read(adrON + 1);
    int MinEprOF = (EEPROM.read(adrOF) * 60) + EEPROM.read(adrOF + 1);

    lcd.setCursor(pos, 1);
    if (MinEprON == MinEprOF) {
        lcd.print("-");
        digitalWrite(pin, HIGH);
    }
    else if (MinEprON < MinEprOF) {
        if ((MinEprON <= MinToday) && (MinEprOF > MinToday)) {
            lcd.print("*");
            digitalWrite(pin, LOW);
        }
        else {
            lcd.print("p");
            digitalWrite(pin, HIGH);
        }
    }
    else if (MinEprON > MinEprOF) {
        if ((MinEprON >= MinToday) || (MinEprOF < MinToday)) {
            lcd.print("*");
            digitalWrite(pin, LOW);
        }
        else {
            lcd.print("p");
            digitalWrite(pin, HIGH);
        }
    }
}

/* ================================================== */
/* SETUP Functions                                    */
/* ================================================== */

void setRTC() {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("TIME SETTING");
    delay(1000);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("CURRENT ");
    setupShowValue(myRTC.hours, myRTC.minutes, 0);

    lcd.setCursor(0, 1);
    lcd.print("NEW ");

    myRTC.updateTime();
    setupShowValue(myRTC.hours, myRTC.minutes, 1);
    while (read_LCD_buttons() == btnNONE);
    setupChooseValueSetRTC(myRTC.hours, myRTC.minutes, 1);
    lcd.setCursor(0,0);
    lcd.print("SETTINGS SAVED   ");
    delay(1000);
    lcd.clear();
}

void setPin() {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PIN SETTING");
    delay(1000);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PIN 1 ON");
    eepromHour = EEPROM.read(adr1ON);
    eepromMin = EEPROM.read(adr1ON + 1);
    if (eepromHour >= 24) eepromHour = 0;
    if (eepromMin  >= 60) eepromMin  = 0;
    setupShowValue(eepromHour, eepromMin, 0);
    while (read_LCD_buttons() == btnNONE);
    setupChooseValue(eepromHour, eepromMin, adr1ON, 0);

    lcd.setCursor(0, 1);
    lcd.print("PIN 1 OFF");
    eepromHour = EEPROM.read(adr1OF);
    eepromMin = EEPROM.read(adr1OF + 1);
    if (eepromHour >= 24) eepromHour = 0;
    if (eepromMin  >= 60) eepromMin  = 0;
    setupShowValue(eepromHour, eepromMin, 1);
    while (read_LCD_buttons() == btnNONE);
    setupChooseValue(eepromHour, eepromMin, adr1OF, 1);


    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PINN 1 ON");
    eepromHour = EEPROM.read(adr5ON);
    eepromMin = EEPROM.read(adr5ON + 1);
    if (eepromHour >= 24) eepromHour = 0;
    if (eepromMin  >= 60) eepromMin  = 0;
    setupShowValue(eepromHour, eepromMin, 0);
    while (read_LCD_buttons() == btnNONE);
    setupChooseValue(eepromHour, eepromMin, adr5ON, 0);

    lcd.setCursor(0, 1);
    lcd.print("PINN 1 OFF");
    eepromHour = EEPROM.read(adr5OF);
    eepromMin = EEPROM.read(adr5OF + 1);
    if (eepromHour >= 24) eepromHour = 0;
    if (eepromMin  >= 60) eepromMin  = 0;
    setupShowValue(eepromHour, eepromMin, 1);
    while (read_LCD_buttons() == btnNONE);
    setupChooseValue(eepromHour, eepromMin, adr5OF, 1);


    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("PIN 2 ON");
    eepromHour = EEPROM.read(adr2ON);
    eepromMin = EEPROM.read(adr2ON + 1);
    if (eepromHour >= 24) eepromHour = 0;
    if (eepromMin  >= 60) eepromMin  = 0;
    setupShowValue(eepromHour, eepromMin, 0);
    while (read_LCD_buttons() == btnNONE);
    setupChooseValue(eepromHour, eepromMin, adr2ON, 0);

    lcd.setCursor(0, 1);
    lcd.print("PIN 2 OFF");
    eepromHour = EEPROM.read(adr2OF);
    eepromMin = EEPROM.read(adr2OF + 1);
    if (eepromHour >= 24) eepromHour = 0;
    if (eepromMin  >= 60) eepromMin  = 0;
    setupShowValue(eepromHour, eepromMin, 1);
    while (read_LCD_buttons() == btnNONE);
    setupChooseValue(eepromHour, eepromMin, adr2OF, 1);

}

void setupChooseValue(int HourNew, int MinNew, byte Address, byte Pos) {
    while (read_LCD_buttons() != btnSELECT) {
        if (read_LCD_buttons() == btnRIGHT) {
            if (HourNew < 23) {
                HourNew++;
            }
        }
        else if (read_LCD_buttons() == btnLEFT) {
            if (HourNew > 0) {
                HourNew--;
            }
        }
        else if (read_LCD_buttons() == btnUP) {
            if (MinNew < 59) {
                MinNew++;
            }
        }
        else if (read_LCD_buttons() == btnDOWN) {
            if (MinNew > 0) {
                MinNew--;
            }
        }
        setupShowValue(HourNew, MinNew, Pos);
        delay(150);
    }
    while (read_LCD_buttons() != btnNONE);
    EEPROM.write(Address, HourNew);
    EEPROM.write(Address + 1, MinNew);
    delay(150);
}

void setupChooseValueSetRTC(int HourNew, int MinNew, byte Pos) {
    while (read_LCD_buttons() != btnSELECT) {
        if (read_LCD_buttons() == btnRIGHT) {
            if (HourNew < 23) {
                HourNew++;
            }
        }
        else if (read_LCD_buttons() == btnLEFT) {
            if (HourNew > 0) {
                HourNew--;
            }
        }
        else if (read_LCD_buttons() == btnUP) {
            if (MinNew < 59) {
                MinNew++;
            }
        }
        else if (read_LCD_buttons() == btnDOWN) {
            if (MinNew > 0) {
                MinNew--;
            }
        }
        setupShowValue(HourNew, MinNew, Pos);
        delay(150);
    }
    while (read_LCD_buttons() != btnNONE);
    myRTC.setDS1302Time(00, MinNew, HourNew, 2, 12, 6, 2018);
    delay(150);
}

void setupShowValue(int Hour, int Min, int Pos) {
    lcd.setCursor(11, Pos);
    print2digits(Hour);
    lcd.print(":");
    print2digits(Min);
}

/* ================================================== */
/* LCD Functions                                      */
/* ================================================== */

int read_LCD_buttons()
{
    adc_key_in = analogRead(0);       // read the value from the sensor

    if (adc_key_in > 1000) return btnNONE;
    if (adc_key_in < 50)   return btnRIGHT;  
    if (adc_key_in < 150)  return btnUP;
    if (adc_key_in < 300)  return btnDOWN;
    if (adc_key_in < 500)  return btnLEFT;
    if (adc_key_in < 850)  return btnSELECT;  
    return btnNONE;                // when all others fail, return this.
}

void eeprom_write_int(int p_address, int p_value) {
    byte lowByte = ((p_value >> 0) & 0xFF);
    byte highByte = ((p_value >> 8) & 0xFF);

    EEPROM.write(p_address, lowByte);
    EEPROM.write(p_address + 1, highByte);
}

unsigned int eeprom_read_int(int p_address) {
    byte lowByte = EEPROM.read(p_address);
    byte highByte = EEPROM.read(p_address + 1);

    return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}

void displayTime() {
    myRTC.updateTime();

    print2digits(myRTC.dayofmonth);
    lcd.print("/");
    print2digits(myRTC.month);
    //lcd.print("/");
    //print2digits(myRTC.year);
    lcd.print(" ");
    print2digits(myRTC.hours);
    lcd.print(":");
    print2digits(myRTC.minutes);
    lcd.print(":");
    print2digits(myRTC.seconds);
}

void print2digits(int number) {
    if (number >= 0 && number < 10)
        lcd.print('0');
    lcd.print(number, DEC);
}
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości