Real-Time Device Control

Now automate your appliances smartly by setting the time when you want your gadget to switch on and off

AdvancedFull instructions provided24 hours499
Real-Time Device Control

Things used in this project

Story

Read more

Code

Device Control using Timer

Arduino
code for on board red led control in real time
#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal.h>
DS1307 rtc;
LiquidCrystal lcd(8,7,11,12,13,14); // rs, en, D4, D5, D6, D7

int temp1,temp2,h1,m1,h2,m2;

int sw1=6;
int sw2=5; 
int sw3=4;
int sw4=3;


void set_on_schedule()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SET ON TIME:");
    lcd.setCursor(0,1);
    lcd.print("0:0");
    lcd.setCursor(0,1);
    while(digitalRead(sw2)==HIGH)
    {
      if(digitalRead(sw3)==LOW)
      {
        h1=h1+1;
        if(h1>23)
        {
          h1=0;
        }
        while(digitalRead(sw3)==LOW);
        delay(300);
      }

      if(digitalRead(sw4)==LOW)
      {
        m1=m1+1;
        if(m1>59)
        {
          m1=0;
        }
        while(digitalRead(sw4)==LOW);
        delay(300);
      }      
      lcd.setCursor(0,1);
      lcd.print(h1);
      lcd.print(":");
      lcd.print(m1);
    }
    lcd.clear();
    lcd.setCursor(0,0);
}


void set_off_schedule()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SET OFF TIME:");
    lcd.setCursor(0,1);
    lcd.print("0:0");
    lcd.setCursor(0,1);
    while(digitalRead(sw2)==HIGH)
    {
      if(digitalRead(sw3)==LOW)
      {
        h2=h2+1;
        if(h2>23)
        {
          h2=0;
          lcd.clear();
        }
        while(digitalRead(sw3)==LOW);
        delay(300);
      }

      if(digitalRead(sw4)==LOW)
      {
        m2=m2+1;
        if(m2>59)
        {
          m2=0;
          lcd.clear();
        }
        while(digitalRead(sw4)==LOW);
        delay(300);
      }      
      lcd.setCursor(0,1);
      lcd.print(h2);
      lcd.print(":");
      lcd.print(m2);
    }
    lcd.clear();
    lcd.setCursor(0,0);
}

void setup()
{

  // configure push buttons as input
  
  
  pinMode(2,OUTPUT);
  digitalWrite(2,LOW);

  //configure push buttons as input

  pinMode(sw1,INPUT);
  pinMode(sw2,INPUT);
  pinMode(sw3,INPUT);
  pinMode(sw4,INPUT);

  //make status of input pins as HIGH initially

  digitalWrite(sw1,HIGH);
  digitalWrite(sw2,HIGH);
  digitalWrite(sw3,HIGH);
  digitalWrite(sw4,HIGH);

  // initialize LCD
  
  lcd.begin(16,2);

  // write a message on RTC

  lcd.print("Welcome");

  // init RTC
  
  //only set the date+time one time
  //rtc.set(0, 01, 11, 29, 04, 2019); //08:00:00 24.12.2014 //sec, min, hour, day, month, year

  //stop/pause RTC
  // rtc.stop();

  //start RTC
  rtc.start();
}


void loop()
{
  uint8_t sec, min1, hour, day, month;
  uint16_t year;

  //get time from RTC
  rtc.get(&sec, &min1, &hour, &day, &month, &year);


  if(digitalRead(sw1)==LOW)
  {
    // enter into time set mode
    set_on_schedule();
    delay(1000);
    set_off_schedule();
    delay(1000);
  }

  
  lcd.setCursor(0,0);
  lcd.print("Time:");
  lcd.print(hour,DEC);
  lcd.print(":");
  lcd.print(min1,DEC);
  lcd.print(":");
  lcd.print(sec,DEC);

  if(sec==59)
  {
    delay(1000);
    lcd.setCursor(11,0);
    lcd.print("0  ");
  }

  lcd.setCursor(0,1);
  lcd.print("Date:");
  lcd.print(day);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year); 
  //wait a second

  if(hour==h1 && min1==m1)
  {
    digitalWrite(2,HIGH);
  }
  if(hour==h2 && min1==m2)
  {
    digitalWrite(2,LOW);
  }
  
  delay(1000);
}

Credits

Dr. Umesh Dutta

Dr. Umesh Dutta

27 projects • 41 followers
Working as Director of Innovation Centre at Manav Rachna, India. I am into development for the last 12 years.
Texas Instruments University Program

Texas Instruments University Program

54 projects • 95 followers
TI helps students discover what's possible to engineer their future. Learn about TI tech for the classroom: www.university.ti.com
Energia

Energia

25 projects • 23 followers
Founder of @energiaproject

Comments