Go Down

Topic: allarm timer arduino + set buttons lcd (Read 409 times) previous topic - next topic

antoniore

Hi guys I made a draft of the code that's going to set the time and date the alarm of my arduino directly from the buttons. Below place the sketch if someone could give us a look at the beginner again and surely there will be errorini.
Thanks in advance

Code: [Select]
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>


#define RELAY_PIN 2                   // PIN definitions

#define BACKLIGHT 2
#define CONTRAST  3

#define SET       A0
#define PIU       A1
#define MENO      A2

#define STATE_OFF  0                  //FSM states
#define STATE_ON   1                  //FSM states
int fsm_state;

char buffer[10];
unsigned long time=0;
unsigned long timeSet=0;
int  setModeTime=3000;

RTC_DS1307 RTC;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup () {
    pinMode( BACKLIGHT, OUTPUT );
    pinMode( CONTRAST, OUTPUT );
   
    digitalWrite( BACKLIGHT, HIGH );
    analogWrite( CONTRAST, 40 );
   
     pinMode(RELAY_PIN, OUTPUT);
     digitalWrite(RELAY_PIN, LOW);
     
    fsm_state = STATE_OFF;
   
    Wire.begin();
    RTC.begin();
    lcd.begin(16, 2);

    if (! RTC.isrunning()) {
      Serial.println("RTC is NOT running!");
      RTC.adjust(DateTime(__DATE__, __TIME__));
    }

    pinMode( SET, INPUT );
    pinMode( PIU, INPUT );
    pinMode( MENO, INPUT );
}

void loop () {

    if ( analogRead( SET ) < 1000) { time = millis(); }

    DateTime now = RTC.now();
    int nowHourMinute = now.hour() * 100 + now.minute();


    sprintf(buffer,  "%02d/%02d/%d", now.day(), now.month(), now.year());
    lcd.setCursor(0,0);
    lcd.print( buffer );

    char buffer[10] = ""; 
    sprintf(buffer,  "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
    lcd.setCursor(0,1);
    lcd.print( buffer );
   
     char buffer_allarm[10] = ""; 
    sprintf(buffer,  "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
    lcd.setCursor(0,3);
    lcd.print( buffer_allarm );

    if (time > 0 && setModeTime < (millis() - time) ) { setMode( now ); }

    delay(1000);
}

void setMode( DateTime now ) {
    boolean setMode = true;
    int setModeLevel = 0;

    int _day = now.day();
    int _month = now.month();
    int _year = now.year();
    int _hour = now.hour();
    int _min = now.minute();
    int _sec = now.second();
    int _hourallarm = now.hour();
    int _minuteallarm = now.minute();
    int _secallarm = now.second();
   
    lcd.clear();
    lcd.setCursor(0,0);
    sprintf(buffer,  "%s: %02d", "Giorno", _day);
    delay( 1000 );
    timeSet = millis();

    while ( setMode ) {
      if ( analogRead(SET) > 1000 || analogRead(PIU) > 1000 || analogRead(MENO) > 1000 ) { timeSet = millis(); }

      lcd.setCursor(0,0);

      // Set Day
      if ( setModeLevel == 0 ) {
        if ( analogRead(PIU) > 1000 && _day < 31) { _day++; }
        if ( analogRead(MENO) > 1000 && _day > 1) { _day--; }

        sprintf(buffer,  "%s: %02d", "Giorno", _day);
      }
      // Set Month
      if ( setModeLevel == 1 ) {
        if ( analogRead(PIU) > 1000 && _month < 12) { _month++; }
        if ( analogRead(MENO) > 1000 && _month > 1) { _month--; }

        sprintf(buffer,  "%s: %02d", "Mese", _month);
      }
      // Set Year
      if ( setModeLevel == 2 ) {
        if ( analogRead(PIU) > 1000 && _year < 9999) { _year++; }
        if ( analogRead(MENO) > 1000 && _year > 1900) { _year--; }

        sprintf(buffer,  "%s: %02d", "Anno", _year);
      }
      // Set Hour
      if ( setModeLevel == 3 ) {
        if ( analogRead(PIU) > 1000 && _hour < 24) { _hour++; }
        if ( analogRead(MENO) > 1000 && _hour > 1) { _hour--; }

        sprintf(buffer,  "%s: %02d", "Ora", _hour);
      }
      // Set Minute
      if ( setModeLevel == 4 ) {
        if ( analogRead(PIU) > 1000 && _min < 60) { _min++; }
        if ( analogRead(MENO) > 1000 && _min > 1) { _min--; }

        sprintf(buffer,  "%s: %02d", "Minuti", _min);
      }
      // Set Second
      if ( setModeLevel == 5 ) {
        if ( analogRead(PIU) > 1000 && _sec < 60) { _sec++; }
        if ( analogRead(MENO) > 1000 && _sec > 0) { _sec--; }

        sprintf(buffer,  "%s: %02d", "Secondi", _sec);
      }
       // Set Hour
      if ( setModeLevel == 6 ) {
        if ( analogRead(PIU) > 1000 && _hour < 24) { _hour++; }
        if ( analogRead(MENO) > 1000 && _hour > 1) { _hour--; }

        sprintf(buffer,  "%s: %02d", "Ora", _hourallarm);
      }
      // Set Minute
      if ( setModeLevel == 7 ) {
        if ( analogRead(PIU) > 1000 && _min < 60) { _min++; }
        if ( analogRead(MENO) > 1000 && _min > 1) { _min--; }

        sprintf(buffer,  "%s: %02d", "Minuti", _minuteallarm);
      }
      // Set Second
      if ( setModeLevel == 8 ) {
        if ( analogRead(PIU) > 1000 && _sec < 60) { _sec++; }
        if ( analogRead(MENO) > 1000 && _sec > 0) { _sec--; }

        sprintf(buffer,  "%s: %02d", "Secondi", _secallarm);
      }
   
     
      lcd.print( buffer );
      if ( analogRead(SET) > 1000 ) { lcd.clear(); setModeLevel++;  }
      if ( setModeLevel > 8 ) { setModeLevel=0; }

      if (timeSet > 0 && (setModeTime*2) < (millis() - timeSet) ) {
         RTC.adjust(DateTime(_year, _month, _day, _hour, _min, _sec));
         setMode = false;
      }
      delay(200);
    }
     lcd.print( buffer );
      if ( analogRead(SET) > 1000 ) { lcd.clear(); setModeLevel++;  }
      if ( setModeLevel > 8 ) { setModeLevel=0; }

      if (timeSet > 0 && (setModeTime*2) < (millis() - timeSet) ) {
         RTC.adjust(DateTime(_hourallarm, _minuteallarm, _secallarm));
         setMode = false;
      }
      delay(200);
    }

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy