Silícios LabPCBWay
Published © GPL3+

Programmable Timer for Activation of Devices - Part II

In this the second part you'll learn how to implement the timer and function to program the hour to activate the device.

IntermediateFull instructions provided2 hours2,156
Programmable Timer for Activation of Devices - Part II

Things used in this project

Hardware components

PCBWay Custom PCB
PCBWay Custom PCB
×1
Keypad Membrane - UTSOURCE
×1
Jumper Wires - UTSOURCE
×1
10kR Rotary Potentiometer - UTSOURCE
×1
LCD Display 16x2 - UTSOURCE
×1
Arduino UNO - UTSOURCE
×1
Real-Time Clock - UTSOURCE
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic Circuit

Code

Code for Second Part of the System

Arduino
#include <DS1307.h>
#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>

#define MEMORY 100

const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

char tempo[9] = "";
char data[9] = "";

int DataTime[7];

byte SegAnt = 0, SegAtual = 0;

byte ReadKeyPad(void);
void AdjustTime(void);
void ActivationHour(void);

void setup()
{
      Serial.begin(9600);
      DS1307.begin();
      //Config pins to control columns of the matrix keyboard
      for(int key = 8; key < 12; key++)
      {
        pinMode(key, OUTPUT);  
      }

      //Config pins to read lines of the matrix keyboard
      for(int key = 12; key < 16; key++)
      {
        pinMode(key, INPUT);  
      }

      for(int key = 8; key < 12; key++)
      {
        digitalWrite(key, LOW);  
      }

      lcd.begin(16,2);

             lcd.clear();
             lcd.setCursor(4,0);
             lcd.print("Invalid");
             lcd.setCursor(6,1);  
             lcd.print("Hour");
             delay(30000);
}

void loop()
{
    if(EEPROM.read(MEMORY) != 73)
    {
      AdjustTime();
      EEPROM.write(MEMORY, 73);
      lcd.clear();
      ActivationHour();
      lcd.clear();
    }
  
    DS1307.getDate(DataTime);    
    SegAtual = DataTime[6];

    if(ReadKeyPad() == 10)
    {
      AdjustTime();
      lcd.clear();
      ActivationHour();
      lcd.clear();
    }

    for(byte i = 1; i < 5; i++)
    {
      Serial.println(EEPROM.read(i));  
    }
    
    if(abs(SegAtual - SegAnt) >= 1)
    {
    sprintf(tempo, "%02d:%02d:%02d", DataTime[4], DataTime[5], DataTime[6]);
    sprintf(data, "%02d/%02d/%02d", DataTime[2], DataTime[1], DataTime[0]);

    lcd.setCursor(4,1);
    lcd.print(tempo);
    lcd.setCursor(4,0);
    lcd.print(data);
    SegAnt = SegAtual;
    }
}

byte ReadKeyPad(void)
{   
  byte teclado[4][4] = {
  {1,2,3,10},
  {4,5,6,11},
  {7,8,9,12},
  {14,0,15,13}};
    
    byte coluna[4] = {8,9,10,11};
    byte linha[4]  = {12,13,14,15};
    bool tecla;
    
    byte digito_tecla = 16;

    for(byte y = 0; y <= 3; y++)
    {
      digitalWrite(coluna[y], LOW);
    }
    
    for(byte y = 0; y <= 3; y++)
    {
      
      digitalWrite(coluna[y], HIGH);

        for(byte x = 0; x <= 3; x++)
        {
            tecla = digitalRead(linha[x]);  
            
            if(tecla == 1)
            {
              digito_tecla = teclado[x][y];
              return digito_tecla;
            }             
        }

     digitalWrite(coluna[y], LOW);
    }

    return digito_tecla;
}

void AdjustTime()
{
   int times[6];
   int  DadosTempo[7];
   bool controle = 0;
   bool EstadoSobe = 0, EstadoDesce = 0;
   byte cont = 0;
   byte number = 0;
   int digitos[6];
   byte PosCursor = 0;
   byte t = 0;
   bool Validate = 0;
   char tempo[7] = "";
   char data[15] = "";
  
   DS1307.getDate(DadosTempo);
   
   sprintf(tempo, "%02d:%02dh", DadosTempo[4], DadosTempo[5]);
   sprintf(data, "%02d/%02d/%02d", DadosTempo[2], DadosTempo[1], DadosTempo[0]);
   
         times[4] = DadosTempo[4];
         times[5] = DadosTempo[5];
         times[3] = DadosTempo[3];
         times[2] = DadosTempo[2];
         times[1] = DadosTempo[1];
         times[0] = DadosTempo[0];
         times[6] = DadosTempo[6];
   
             digitos[0] = times[4]/10; //Armazena a Dezena das Horas
             digitos[1] = times[4]%10; //Armazena a Unidade das Horas
             digitos[2] = times[5]/10; //Armazena a Dezena dos Minutos
             digitos[3] = times[5]%10; //Armazena a Unidade dos Minutos
   
         do
         {
           
             lcd.setCursor(0,0);
             lcd.print("                ");
             lcd.setCursor(2,0);  
             lcd.print("Adjust hour:");
             lcd.setCursor(0,1);
             lcd.print("                ");
             lcd.setCursor(5,1);
             lcd.print(tempo);
             PosCursor = 5;
           
           do
           {
             number = ReadKeyPad();
             delay(100);
             if( (number >= 0 && number <= 9) && (controle == 0) && (cont < 4) )
             {
               digitos[cont] = number;  
               cont++;
               controle = 1;
               lcd.setCursor(PosCursor,1);
               lcd.print(number);                
               PosCursor++;
               if(cont == 2 || cont == 4)
               {
                 PosCursor = PosCursor + 1;
               }
             }
             if(number == 16 && controle == 1)
             {
               controle = 0;
             }
             if(number == 12)
             { 
                 for(cont = 0; cont < 4; cont++)
                 {
                   digitos[cont] = 0;
                 }  
               lcd.setCursor(0,1);
               lcd.print("                ");
               lcd.setCursor(5,1);
               lcd.print("00:00h");
               PosCursor = 5;
               cont = 0;
               for(int i = 4; i < 6; i++)
               {
                 times[i] = 0;  
               }
             }
  
           }while(number != 13);  
           
           times[4] = (digitos[0]*10) + digitos[1];
           times[5] = (digitos[2]*10) + digitos[3];
           
           if((times[4] < 0 || times[4] > 23) || (times[5] < 0 || times[5] > 59))
           {
             lcd.clear();
             lcd.setCursor(3,0);
             lcd.print("Invalid");
             lcd.setCursor(4,1);  
             lcd.print("Hour");
             
             delay(2000);
             Validate = 1;
             
             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("                ");
             lcd.setCursor(1,0);  
             lcd.print("Adjust Hours:");
             lcd.setCursor(0,1);
             lcd.print("                ");
             lcd.setCursor(5,1);
             lcd.print("00:00h");
             sprintf(tempo, "%02d:%02dh", 0, 0);
             PosCursor = 5;
             cont = 0;

             for(int i = 0; i < 4; i++)
             {
               times[i] = 0;  
             }   
             
           }
         }while(Validate == 1);
             
             do
             {
  
               number = ReadKeyPad();
               delay(200);
  
             }while(number != 16);
/*----------------------------------------Date Configuration-----------------------------------------------*/
  
           PosCursor = 4;
           
           do
           {
             //Transformacao dos digitos da data para dezenas e unidades
             digitos[0] = times[2]/10; //Armazena a Dezena da Data
             digitos[1] = times[2]%10; //Armazena a Unidade da Data
             digitos[2] = times[1]/10; //Armazena a Dezena do Mes
             digitos[3] = times[1]%10; //Armazena a Unidade do Mes
             digitos[4] = times[0]/10; //Armazena a Dezena do Ano
             digitos[5] = times[0]%10; //Armazena a Unidade do Ano
             sprintf(data, "%02d/%02d/%02d", times[2], times[1], times[0]);
  
          
               lcd.setCursor(0,0);
               lcd.print("                ");
               lcd.setCursor(2,0);  
               lcd.print("Adjust Date:");           
               lcd.setCursor(0,1);
               lcd.print("                ");
               lcd.setCursor(4,1);
               lcd.print(data);
               PosCursor = 4;
               cont = 0;
             
           do
           {
  
             number = ReadKeyPad();
             delay(100);
  
             if( (number >= 0 && number <= 9) && (controle == 0) && (cont < 6) )
             {
               digitos[cont] = number;
               cont++;
               controle = 1;              
               lcd.setCursor(PosCursor,1);
               lcd.print(number);                
               PosCursor++;
               if(cont == 2 || cont == 4)
               {
                 PosCursor = PosCursor + 1;
               }
             }
  
             if(number == 16 && controle == 1)
             {
               controle = 0;
             }
  
             if(number == 12)
             { 
                 for(cont = 0; cont < 6; cont++)
                 {
                   digitos[cont] = 0;
                 }  
               lcd.setCursor(0,1);
               lcd.print("                ");
               lcd.setCursor(4,1);
               lcd.print("00/00/00");
               PosCursor = 4;
               cont = 0;
             }
           }while(number != 13);
  
           times[2] = (digitos[0]*10) + digitos[1]; //Transformando os numeros lidos para data do mes em dois dígitos
           times[1] = (digitos[2]*10) + digitos[3]; //Transformando os numeros lidos para mes em dois dígitos
           times[0] = (digitos[4]*10) + digitos[5]; //Transformando os numeros lidos para ano em dois dígitos
  
           if((times[2] <= 0 || times[2] > 31) || (times[1] <= 0 || times[1] > 12) || times[0] <= 0)
           {
               lcd.clear();
               lcd.setCursor(3,0);
               lcd.print("Invalid");
               lcd.setCursor(4,1);  
               lcd.print("Date");
               delay(2000);
               Validate = 1;
               lcd.clear();
               lcd.setCursor(0,0);
               lcd.print("                ");
               lcd.setCursor(2,0);  
               lcd.print("Adjuste Date:");           
               lcd.setCursor(0,1);
               lcd.print("                ");
               lcd.setCursor(3,1);
               lcd.print("00/00/00");
               PosCursor = 5;
               cont = 0;
               
               for(byte i = 0; i < 3; i++)
               {
                 times[i] = 0;  
               }

           }
         }while(Validate == 1);
         do
         {
           number = ReadKeyPad();
           delay(200);
         }while(number != 16);
  
           lcd.clear();
  
          DS1307.setDate(times[0],times[1],times[2],times[3],times[4],times[5],00);//year,month,date of month, day of week,hour,minutes,second
}

void ActivationHour(void)
{
   int times[6];
   int  DadosTempo[7];
   bool controle = 0;
   bool EstadoSobe = 0, EstadoDesce = 0;
   byte cont = 0;
   byte number = 0;
   int digitos[6];
   byte PosCursor = 0;
   bool Validate = 0;
   lcd.clear();
   
         for(byte i = 0; i < 6; i++)
         {
           digitos[i] = 0;
         }
         do
         {  
             lcd.setCursor(0,0);
             lcd.print("                ");
             lcd.setCursor(0,0);  
             lcd.print("Activation Hour:");
             lcd.setCursor(0,1);
             lcd.print("                ");
             lcd.setCursor(5,1);
             lcd.print("00:00h");
             PosCursor = 5;
           do
           {
             number = ReadKeyPad();
             delay(100);
             if( (number >= 0 && number <= 9) && (controle == 0) && (cont < 4) )
             {
               digitos[cont] = number;  
               cont++;
               controle = 1;
               lcd.setCursor(PosCursor,1);
               lcd.print(number);                
               PosCursor++;
               if(cont == 2 || cont == 4)
               {
                 PosCursor = PosCursor + 1;
               }
             }
             if(number == 16 && controle == 1)
             {
               controle = 0;
             }
             if(number == 12)
             { 
                 for(cont = 0; cont < 4; cont++)
                 {
                   digitos[cont] = 0;
                 }  

               lcd.setCursor(0,1);
               lcd.print("                ");
               lcd.setCursor(5,1);
               lcd.print("00:00h");
               PosCursor = 5;
               cont = 0;
               for(int i = 4; i < 6; i++)
               {
                 times[i] = 0;  
               }
             }
           }while(number != 13);  
           times[4] = (digitos[0]*10) + digitos[1];
           times[5] = (digitos[2]*10) + digitos[3];
           
           if((times[4] < 0 || times[4] > 23) || (times[5] < 0 || times[5] > 59))
           {
             lcd.clear();
             lcd.setCursor(3,0);
             lcd.print("Invalid");
             lcd.setCursor(4,1);  
             lcd.print("Hour");
             delay(2000);
             Validate = 1;

             lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("                ");
             lcd.setCursor(1,0);  
             lcd.print("Activation Hour:");
             lcd.setCursor(0,1);
             lcd.print("                ");
             lcd.setCursor(5,1);

             lcd.print("00:00h");
             sprintf(tempo, "%02d:%02dh", 0, 0);

             PosCursor = 5;
             cont = 0;

             for(cont = 0; cont < 4; cont++)
             {
               digitos[cont] = 0;
             }  
   
           }
         }while(Validate == 1);

           EEPROM.write(1, times[4]);
           EEPROM.write(2, times[5]);

             do
             {
               number = ReadKeyPad();
               delay(200);
             }while(number != 16);
}

Credits

Silícios Lab

Silícios Lab

72 projects • 173 followers
Hello, I love program microcontrollers and works with electronic projects.
PCBWay

PCBWay

90 projects • 146 followers
We are a PCB and assembly manufacturer, As low as $5/10pcs and 24 hours delivery time. We are committed to helping creators build project.

Comments