Go Down

Topic: idea to set the time in 2 different sequences alarm clock (Read 136 times) previous topic - next topic

chokri77

Sir
I have made this for our school ,it is working fine,but we need some modification.Timing for Friday is different from other days so please give an idea to set the time in 2 different sequences.i.e.
For other days timing should be
9-00
9-15
10-00
10-40
11-20
12-00
12-35
13-15
13-55
14-25
For Friday it should be
9-00
9-15
10-00
10-40
11-20
12-00
13-00
13-35
14-10
14-45

DangerToMyself

OPs code (after using CTRL+t)



Code: [Select]
//------------Program developed by R.GIRISH------------//
#include<EEPROM.h>

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
int i = 0;
int H = 0;
int M = 0;
int S = 0;
int J = 0;
int setting_value;
const int bell = 10;
const int P = A3;
const int N = A2;
const int setting_address = 0;
const int over_ride_off = 11;
boolean bell_status = true;
boolean Over_ride = true;
//------------------- Set Bell Timings from hours 1 to 23 hrs -------------------//
//---- 1st bell ------//
const int h1 = 18; //hours
const int m1 = 59; //Minutes
const int j1 = 5; //Minutes
//---- 2nd bell ------//
const int h2 = 0;
const int m2 = 0;
//---- 3rd bell ------//
const int h3 = 0;
const int m3 = 0;
//---- 4th bell ------//
const int h4 = 0;
const int m4 = 0;
//---- 5th bell ------//
const int h5 = 0;
const int m5 = 0;
//---- 6th bell ------//
const int h6 = 0;
const int m6 = 0;
//---- 7th bell ------//
const int h7 = 0;
const int m7 = 0;
//---- 8th bell ------//
const int h8 = 0;
const int m8 = 0;
//---- 9th bell ------//
const int h9 = 0;
const int m9 = 0;
//---- 10th bell ------//
const int h10 = 0;
const int m10 = 0;
//---- 11th bell ------//
const int h11 = 0;
const int m11 = 0;
//---- 12th bell ------//
const int h12 = 0;
const int m12 = 0;
//---- 13th bell ------//
const int h13 = 0;
const int m13 = 0;
//---- 14th bell ------//
const int h14 = 0;
const int m14 = 0;
//---- 15th bell ------//
const int h15 = 0;
const int m15 = 0;
//---- 16th bell ------//
const int h16 = 0;
const int m16 = 0;
//--------------- bell ring lenght in seconds -------//
const int Lenght = 3; //in seconds
//-------------------------- -------------------------//
void setup()
{
  lcd.begin(16, 2);
  pinMode(P, OUTPUT);
  pinMode(N, OUTPUT);
  pinMode(bell, OUTPUT);
  pinMode(over_ride_off, INPUT);
  digitalWrite(P, HIGH);
  digitalWrite(N, LOW);
  digitalWrite(over_ride_off, HIGH);
  attachInterrupt(0, over_ride, RISING);
  attachInterrupt(1, bell_setting, RISING);
  if (EEPROM.read(setting_address) != 1)
  {
    bell_setting();
  }
}
void loop()
{
  tmElements_t tm;
  lcd.clear();
  if (RTC.read(tm))
  {
    H = tm.Hour;
    M = tm.Minute;
    S = tm.Second;
    J = tm.Day;
    lcd.setCursor(0, 0);
    lcd.print("TIME:");
    lcd.print(tm.Hour);
    lcd.print(":");
    lcd.print(tm.Minute);
    lcd.print(":");
    lcd.print(tm.Second);
    lcd.setCursor(0, 1);
    lcd.print("DATE:");
    lcd.print(tm.Day);
    lcd.print("/");
    lcd.print(tm.Month);
    lcd.print("/");
    lcd.print(tmYearToCalendar(tm.Year));
  } else {
    if (RTC.chipPresent())
    {
      lcd.setCursor(0, 0);
      lcd.print("RTC stopped!!!");
      lcd.setCursor(0, 1);
      lcd.print("Run SetTime code");
    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Read error!");
      lcd.setCursor(0, 1);
      lcd.print("Check circuitry!");
    }
  }
  if (EEPROM.read(setting_address) == 1)
  {
    if (H == 0 && M == 0 && S == 0 && J == 0)
    {
      digitalWrite(bell, LOW);
    }
    if (H == h1 && M == m1 && S == 0  && J == j1)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h2 && M == m2 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h3 && M == m3 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h4 && M == m4 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h5 && M == m5 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h6 && M == m6 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h7 && M == m7 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h8 && M == m8 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h9 && M == m9 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h10 && M == m10 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h11 && M == m11 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h12 && M == m12 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h13 && M == m13 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h14 && M == m14 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h15 && M == m15 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
    if (H == h16 && M == m16 && S == 0)
    {
      for (i = 0; i < Lenght; i++)
      {
        digitalWrite(bell, HIGH);
        delay(1000);
      }
      digitalWrite(bell, LOW);
      i = 0;
    }
  }
  delay(1000);
}
void over_ride()
{
  lcd.clear();
  while (Over_ride)
  {
    digitalWrite(bell, HIGH);
    lcd.setCursor(0, 0);
    lcd.print("Press Exit to");
    lcd.setCursor(0, 1);
    lcd.print("Stop the bell!!!");
    if (digitalRead(over_ride_off) == LOW)
    {
      Over_ride = false;
      digitalWrite(bell, LOW);
    }
  }
  Over_ride = true;
}
void bell_setting()
{
  setting_value = 0;
  EEPROM.write(setting_address, setting_value);
  lcd.clear();
  while (bell_status)
  {
    lcd.setCursor(0, 0);
    lcd.print("Bell is Disabled");
    lcd.setCursor(0, 1);
    lcd.print("Press Exit.");
    if (digitalRead(over_ride_off) == LOW)
    {
      bell_status = false;
    }
  }
  bell_status = true;
  setting_value = 1;
  EEPROM.write(setting_address, setting_value);
}

DangerToMyself

First off, you are going to need a way to know which day of the week it is. I've not used your time or RTC libraries. So, that's up to you to figure out. The few time/RTC related libraries I have used had a method for figuring "day of week".

Then, for each time on Friday that is different than the rest of the week, you'll need to do something like

if(today == Friday){ //use this time } else { //use this time }


If this were not a school project and time was not a factor, I would suggest using a different strategy. But, may be best to work with what you have.

If time is not a factor. I would look into using arrays, state machines and timing using millis.

Hope it helps

chokri77


chokri77

I have another code that allows to give the weekdays but the problem: how can I combine the two codes to see the alarms run according to the specific days
CODE 2 :
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

const char *tableauDesJours[] = {"Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"};

void setup ()
{
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
 
  // 'if' commenté : le RTC est mis à jour après chaque compilation.
  // 'if' non commenté : le RTC n'est PAS mis à jour après chaque compilation.
  //if (! RTC.isrunning())
  {
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // Ecriture spécifique du dayOfWeek oublié par la library "RTClib".
    Wire.beginTransmission(0x68);
    Wire.write(0x03);            // Pointe sur le registre dayOfWeek #4 d'adresse 0x003.
    Wire.write(decToBcd(6));     // Ecrit le dayOfWeek (ici Lundi = 1).
                               // A modifier manuellement en fonction du jour réel.
    Wire.endTransmission();
  }
}


void loop ()
{
  DateTime now = RTC.now();

  //Récupère la valeur du dayOfWeek oubliée par la library 'RTClib'.
  Wire.beginTransmission(0x68);   // Accède au RTC I2C d'adresse 0x68.
  Wire.write(0x03);               // Pointe le registre du dayOfWeek (0x03).
  Wire.endTransmission();
  Wire.requestFrom(0x68, 1);      // Demande de reception d'un octet depuis registre (0x03).
  byte dayOfWeek = Wire.read();   // Stocke la valeur reçue.
  Wire.endTransmission();


  Serial.println();
  Serial.print("Numero du jour de la semaine : ");
  Serial.print(dayOfWeek);
  Serial.println();
  Serial.print("Jour de la semaine: ");
  Serial.print(tableauDesJours[dayOfWeek-1]);
  Serial.println();
   
  Serial.print("Heure : ");
  print2digits(now.hour());
  Serial.print(":");
  print2digits(now.minute());
  Serial.print(":");
  print2digits(now.second());
  Serial.println();
   
  Serial.print("Jour : ");
  print2digits(now.day());
  Serial.println();
  Serial.print("Mois : ");
  print2digits(now.month());
  Serial.println();
  Serial.print("Annee : ");
  print2digits(now.year()); 
  Serial.println(); 
  delay(900);
}


void print2digits(int valeur)
{
  if (valeur >= 0 && valeur < 10) {
    Serial.print('0');
  }
  Serial.print(valeur);
}


byte decToBcd(byte valeur)
{
  return ((valeur/10*16) + (valeur%10));
}

DangerToMyself

In your first code, you have
Code: [Select]
    H = tm.Hour;
    M = tm.Minute;
    S = tm.Second;
    J = tm.Day;


I "guessing", after looking through the library files for the library DS1307RTC.h, that "tm.Wday" is the day of the week. You'll have to add that and test it to know for certain.

I'm getting that from this snippet from the cpp file which doesn't have many comments. So, it's just a guess. But the other 6 options are pretty self-explanatory.
Code: [Select]

  tm.Second = bcd2dec(sec & 0x7f);  
  tm.Minute = bcd2dec(Wire.read() );
  tm.Hour =   bcd2dec(Wire.read() & 0x3f);  // mask assumes 24hr clock
  tm.Wday = bcd2dec(Wire.read() );
  tm.Day = bcd2dec(Wire.read() );
  tm.Month = bcd2dec(Wire.read() );
  tm.Year = y2kYearToTm((bcd2dec(Wire.read())));

DangerToMyself

Also, when posting code, place it inside code tags. Button looks like </> (found at the upper left corner of the post form)

chokri77

M DangerToMyself
All thanks and appreciation
I am sorry I got tired
I'll try all your instructions

chokri77

As for the first possibility did not work
As for the second method (the six lines) I did not know how to enter them in the program tried not but did not succeed
please help

DangerToMyself

The second set is code from the library files. Not code for you to try. My fault. Shouldn't have added that to your confusion.


Not 100% positive, but tm.Wday I believe returns 0-6. Which is how many days since Sunday. Sunday being 0 and Saturday being 6.

Again, I have no way at the moment to confirm that. You'll have to do that. If it were me, I would temporarily change

 lcd.print(tm.Day);

To

 lcd.print(tm.Wday);

And see if it shows a number from 0 to 6.



Go Up