Published © GPL3+

Arduino Time Adjustable Relay Project

Would you like to control a device by entering the time information?

BeginnerFull instructions provided10,578
Arduino Time Adjustable Relay Project

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SEVEN-SEGMENT LED DISPLAY 4X
×1
LED BUTTON BOARD
×1
RELAY BOARD 5V
×1

Story

Read more

Code

1_DSP_COUNTER.ino

Arduino
display test
#include <SevenSeg.h>

/*
 
 Hello 

 I design developer tools for embedded electronic systems.
 You can buy my projects design files.
 https://www.altiumdesignerprojects.com

*/
SevenSeg disp(4,5,6,7,8,9,10);  // https://github.com/sigvaldm/SevenSeg
//            A,B,C,D,E,F,G
//DIGIT PORT  0 1 2 3 4 5 6     // my seven-segment display project
        
int digitPins[4]={3,2,1,0};
//               D1,D2,D3,D4
// SCAN PORT     T4,T3,T2,T1    // my seven-segment display project
 
char displaybuffer[4] = "0000";

word number = 0;

void setup() {

 disp.setDigitPins(4, digitPins);
 disp.setCommonAnode();
 disp.setActivePinState(HIGH,HIGH);
 disp.setTimer(2);
 disp.startTimer();

}  

void loop() {
  
 disp.write(displaybuffer);
 
 displaybuffer[0] = number / 1000     + 48;   //   1 
 displaybuffer[1] = number / 100 % 10 + 48;   //   2
 displaybuffer[2] = number / 10  % 10 + 48;   //   3
 displaybuffer[3] = number % 10       + 48;   //   4
 
 number++;
 delay(500);

}

ISR(TIMER2_COMPA_vect){
  disp.interruptAction();
}

2_DSP_TIME.ino

Arduino
display data testing
#include <SevenSeg.h>

/*
 
 Hello 

 I design developer tools for embedded electronic systems.
 You can buy my projects design files.
 https://www.altiumdesignerprojects.com

*/

SevenSeg disp(4,5,6,7,8,9,10);  // https://github.com/sigvaldm/SevenSeg
//            A,B,C,D,E,F,G

int digitPins[4]={3,2,1,0};
//               D1,D2,D3,D4

char displaybuffer[4] = "0000";

byte Minute;
byte Second;

void setup() {

 disp.setDigitPins(4, digitPins);
 disp.setCommonAnode();
 disp.setActivePinState(HIGH,HIGH);
 disp.setTimer(2);
 disp.startTimer();

 Minute = 12;
 Second = 34;

}  

void loop(){

 displaybuffer[0] = Minute / 10  + 48;   
 displaybuffer[1] = Minute % 10  + 48;
 displaybuffer[2] = Second / 10  + 48;
 displaybuffer[3] = Second % 10  + 48;

 disp.write(displaybuffer);

}

ISR(TIMER2_COMPA_vect){
  disp.interruptAction();
}

3_SECOND_UP_CONTROL.ino

Arduino
managing the second variable
/*
 
 Hello 

 I design developer tools for embedded electronic systems.
 You can buy my projects design files.
 https://www.altiumdesignerprojects.com

*/

word MilliSecond = 0; 
byte Second = 0;
byte Minute = 0;
byte lastSecond;
bool timerStart = false;

char rx_byte;

void setup(){
 Serial.begin(9600);

 noInterrupts();         // disable all interrupts
 TCCR1A = 0;             // set entire TCCR1A register to 0  //set timer1 interrupt at 1kHz  // 1 ms
 TCCR1B = 0;             // same for TCCR1B
 TCNT1  = 0;             // set timer count for 1khz increments
 OCR1A = 1999;           // = (16*10^6) / (1000*8) - 1
 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
 // turn on CTC mode
 TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
 TCCR1B |= (1 << CS11);  // enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 interrupts();           // enable

}

void loop(){
 
  if(Serial.available() > 0){       
    rx_byte = Serial.read();       
    if(rx_byte == '1'){
       timerStart = true; 
    }
    
    if(rx_byte == '2'){
       timerStart = false;
    }
    
    if(rx_byte == '3'){
       Second = 0;
    }
 }

  
} // loop

ISR(TIMER1_COMPA_vect){   
 
 if(timerStart == true){ 
    MilliSecond++;
    if(MilliSecond >= 1000){
       MilliSecond = 0;
       Second++;
       if(Second >= 60){
          Second = 0;
          Minute++;
          if(Minute >= 60){
             Minute = 0;
          }
       }
    }

   if(Second != lastSecond){
      Serial.println("Time -- " + String(Minute) +":"+ String(Second));  
   }
   lastSecond = Second;    
 }
 
}

4_SECOND_DOWN_CONTROL.ino

Arduino
managing the second variable
/*
 
 Hello 

 I design developer tools for embedded electronic systems.
 You can buy my projects design files.
 https://www.altiumdesignerprojects.com

*/

word MilliSecond = 0; 
byte Second = 0;
byte Minute = 0;
byte lastSecond;
bool timerStart = false;

char rx_byte;

void setup(){
 Serial.begin(9600);

 noInterrupts();         // disable all interrupts
 TCCR1A = 0;             // set entire TCCR1A register to 0  //set timer1 interrupt at 1kHz  // 1 ms
 TCCR1B = 0;             // same for TCCR1B
 TCNT1  = 0;             // set timer count for 1khz increments
 OCR1A = 1999;           // = (16*10^6) / (1000*8) - 1
 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
 // turn on CTC mode
 TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
 TCCR1B |= (1 << CS11);  // enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 interrupts();           // enable

 Second = 45;
 Minute = 4;

}

void loop(){
 
  if(Serial.available() > 0){       
    rx_byte = Serial.read();       
    if(rx_byte == '1'){
       timerStart = true; 
    }
    
    if(rx_byte == '2'){
       timerStart = false;
    }
    
    if(rx_byte == '3'){
       Second = 0;
    }
 }

  
} // loop

ISR(TIMER1_COMPA_vect){   

 if(timerStart == true){ 
    MilliSecond++;
    if(MilliSecond >= 1000){
       MilliSecond = 0;
       Second--;
        if(Second >= 255){   
           Second = 59;
           Minute--;
         if(Minute >= 255){
            Second = 0;
            Minute = 0;
          }
        }
    }

   if(Second != lastSecond){
      Serial.println("Time -- " + String(Minute) +":"+ String(Second));  
   }
   lastSecond = Second;    
 }
  
}

5_SECOND_DISPLAY.ino

Arduino
second and display is working
#include <SevenSeg.h>

/*
 
 Hello 

 I design developer tools for embedded electronic systems.
 You can buy my projects design files.
 https://www.altiumdesignerprojects.com

*/

SevenSeg disp(4,5,6,7,8,9,10);   //  A,B,C,D,E,F,G
int digitPins[4]={3,2,1,0};      //  D1,D2,D3,D4

char displaybuffer[4] = "0000";
word MilliSecond = 0; 
byte Minute = 0;
byte Second = 0;
bool timerStart = false;

void setup(){

 disp.setDigitPins(4, digitPins);
 disp.setCommonAnode();
 disp.setActivePinState(HIGH,HIGH);
 disp.setTimer(0) ;
 disp.startTimer();

 noInterrupts();         // disable all interrupts
 TCCR1A = 0;             // set entire TCCR1A register to 0  //set timer1 interrupt at 1kHz  // 1 ms
 TCCR1B = 0;             // same for TCCR1B
 TCNT1  = 0;             // set timer count for 1khz increments
 OCR1A = 1999;           // = (16*10^6) / (1000*8) - 1
 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
 // turn on CTC mode
 TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
 TCCR1B |= (1 << CS11);  // enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 interrupts();           // enable
  
}  

void loop(){
 
 timerStart = true;
 
 displaybuffer[0] = Minute / 10  + 48;   
 displaybuffer[1] = Minute % 10  + 48;
 displaybuffer[2] = Second / 10  + 48;
 displaybuffer[3] = Second % 10  + 48;

 disp.write(displaybuffer);

}

ISR(TIMER0_COMPA_vect){
  disp.interruptAction ();
}


ISR(TIMER1_COMPA_vect){   
 
 if(timerStart == true){ 
    MilliSecond++;
    if(MilliSecond >= 1000){
       MilliSecond = 0;
       Second++;
       if(Minute >= 60){
          Minute = 0;
       }
    }
 }
 
}

6_BUTTONS_TIME.ino

Arduino
data entry with buttons
#include <SevenSeg.h>
#include <Button.h>

/*
 
 Hello 

 I design developer tools for embedded electronic systems.
 You can buy my projects design files.
 https://www.altiumdesignerprojects.com

*/

SevenSeg disp(4,5,6,7,8,9,10); //  A,B,C,D,E,F,G
int digitPins[4]={3,2,1,0};    //  D1,D2,D3,D4

char displaybuffer[4] = "0000";

Button minutePlus  = Button(A0,BUTTON_PULLDOWN); // Minute +
Button minuteMinus = Button(A1,BUTTON_PULLDOWN); // Minute -

Button secondPlus  = Button(A2,BUTTON_PULLDOWN); // Second +
Button secondMinus = Button(A3,BUTTON_PULLDOWN); // Second -

Button startButton = Button(A4,BUTTON_PULLDOWN); // RUN

#define relayPin     A5  

word MilliSecond = 0; 
byte Second = 0;
byte Minute = 0;
byte lastSecond;
bool timerStart = false;
bool relayStatus = false;

const byte wait = 125;

void setup(){
 pinMode(relayPin, OUTPUT);
 
 disp.setDigitPins(4, digitPins);
 disp.setCommonAnode();
 disp.setActivePinState(HIGH,HIGH);
 disp.setTimer(2);
 disp.startTimer();

 noInterrupts();         // disable all interrupts
 TCCR1A = 0;             // set entire TCCR1A register to 0  //set timer1 interrupt at 1kHz  // 1 ms
 TCCR1B = 0;             // same for TCCR1B
 TCNT1  = 0;             // set timer count for 1khz increments
 OCR1A = 1999;           // = (16*10^6) / (1000*8) - 1
 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
 // turn on CTC mode
 TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
 TCCR1B |= (1 << CS11);  // enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 interrupts();           // enable

}  

void loop(){

if(relayStatus == false){   // status
  
 if(minutePlus.isPressed()){
    Minute++;
    delay(wait);    
     if(Minute >= 60){
        Second = 0;
        Minute = 0;
     }         
 } 

 if(minuteMinus.isPressed()){  
    Minute--;
    delay(wait);     
     if(Minute >= 255) {    
        Minute = 0;
     }    
 }

 if(secondPlus.isPressed()){
    Second++;
    delay(wait);
     if(Second >= 60){
        Second = 0;      
        Minute++;
     } 
 }

 if(secondMinus.isPressed()){
    Second--;
    delay(wait);    
     if(Minute == 0 && Second >= 255){  
        Second=0;
     } 
      if(Second >= 255 ){
         Second = 59;
         Minute--;
          if(Minute >= 255 ){            
             Minute = 0;
          }           
      } 
 }

 if(startButton.isPressed()){
    timerStart = true;
    relayStatus = true;
    digitalWrite(relayPin,HIGH);   // role on
    delay(50); 
 }
 
} // relaystatus


 displaybuffer[0] = Minute / 10  + 48;   
 displaybuffer[1] = Minute % 10  + 48;
 displaybuffer[2] = Second / 10  + 48;
 displaybuffer[3] = Second % 10  + 48;

 disp.write(displaybuffer);

} // loop

ISR(TIMER2_COMPA_vect){
  disp.interruptAction ();
}

ISR(TIMER1_COMPA_vect){   

 if(timerStart == true){ 
    MilliSecond++;
    if(MilliSecond >= 1000){
       MilliSecond = 0;
       Second--;
        if(Second >= 255){   
           Second = 59;
           Minute--;
         if(Minute >= 255){
            Second = 0;
            Minute = 0;
          }
        }
    }
 
 }
  
}

7_TIMER_RELAY_PROJECT_UNO.ino

Arduino
... and ready
#include <SevenSeg.h>
#include <Button.h>

/*
 
 Hello 

 I design developer tools for embedded electronic systems.
 You can buy my projects design files.
 https://www.altiumdesignerprojects.com

*/

SevenSeg disp(4,5,6,7,8,9,10); //  A,B,C,D,E,F,G
int digitPins[4]={3,2,1,0};    //  D1,D2,D3,D4

char displaybuffer[4] = "0000";

Button minutePlus  = Button(A0,BUTTON_PULLDOWN); // Minute +
Button minuteMinus = Button(A1,BUTTON_PULLDOWN); // Minute -

Button secondPlus  = Button(A2,BUTTON_PULLDOWN); // Second +
Button secondMinus = Button(A3,BUTTON_PULLDOWN); // Second -

Button startButton = Button(A4,BUTTON_PULLDOWN); // RUN

#define relayPin     A5  

word MilliSecond = 0; 
byte Second = 0;
byte Minute = 0;
byte lastSecond;

bool timerStart = false;
bool relayStatus = false;

const byte wait = 150;

void setup(){
 pinMode(relayPin, OUTPUT);
 
 disp.setDigitPins(4, digitPins);
 disp.setCommonAnode();
 disp.setActivePinState(HIGH,HIGH);
 disp.setTimer(2);
 disp.startTimer();

 noInterrupts();         // disable all interrupts
 TCCR1A = 0;             // set entire TCCR1A register to 0  //set timer1 interrupt at 1kHz  // 1 ms
 TCCR1B = 0;             // same for TCCR1B
 TCNT1  = 0;             // set timer count for 1khz increments
 OCR1A = 1999;           // = (16*10^6) / (1000*8) - 1
 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
 // turn on CTC mode
 TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
 TCCR1B |= (1 << CS11);  // enable timer compare interrupt
 TIMSK1 |= (1 << OCIE1A);
 interrupts();           // enable

}  

void loop(){

  if(relayStatus == false){   // status
    
     if(minutePlus.isPressed()){
        Minute++;
        delay(wait);    
         if(Minute >= 60){
            Second = 0;
            Minute = 0;
         }         
     } 
    
     if(minuteMinus.isPressed()){  
        Minute--;
        delay(wait);     
         if(Minute >= 255) {    
            Minute = 0;
         }    
     }
    
     if(secondPlus.isPressed()){
        Second++;
        delay(wait);
         if(Second >= 60){
            Second = 0;      
            Minute++;
         } 
     }
    
     if(secondMinus.isPressed()){
        Second--;
        delay(wait);    
         if(Minute == 0 && Second >= 255){  
            Second=0;
         } 
          if(Second >= 255 ){
             Second = 59;
             Minute--;
              if(Minute >= 255 ){            
                 Minute = 0;
              }           
          } 
     }
    
     if(startButton.isPressed()){
        timerStart = true;
        relayStatus = true;
        digitalWrite(relayPin,HIGH);   // role on
        delay(10); 
     }
   
  } // relaystatus

 displaybuffer[0] = Minute / 10  + 48;   
 displaybuffer[1] = Minute % 10  + 48;
 displaybuffer[2] = Second / 10  + 48;
 displaybuffer[3] = Second % 10  + 48;

 disp.write(displaybuffer);

} // loop

ISR(TIMER2_COMPA_vect){
  disp.interruptAction();
}

ISR(TIMER1_COMPA_vect){   

 if(timerStart == true){ 
    MilliSecond++;
    if(MilliSecond >= 1000){
       MilliSecond = 0;
       Second--;
        if(Second >= 255){   
           Second = 59;
           Minute--;
         if(Minute >= 255){
            Second = 0;
            Minute = 0;
            timerStart = false;
            relayStatus = false;
            digitalWrite(relayPin,LOW);            
          }
        }
    }
 }
 
}

Credits

Comments