design project with DS1307

hey guys

I'm a total beginner in arduino, in fact i only just heard about it and started using it within the last month.

i'm using a DS1307 real time clock and i have successfully managed to input time and it reads it etc. I did this based on all your posts, searching playground, tutorials and examples.

My problem is actually setting an alarm time. I'm really struggling to find a code to follow and my plan is to set 2 alarm times based on 1 alarm time...

My goal is to set an alarm time using set potentiometer ranges; e.g turning a set pot range to set the alarm for 6am. (this is an extra element tho)

My priority: once this time has been set, i want it to count backwards 9 hours and set off a different alarm e.g. once 6am has been set, an alarm signal must go off at 9pm.

has anyone got any ideas as to how i might code this up?? Is it even do able??

many thanks

I have some application notes on the DS1337 which is very similar to the DS1307, See http://tinyurl.com/y8exe7k

One of the application hints shows how to setup the alarms and use interrupts. For the DS1337 you should be able set two alarms based on the pot setting. IIRC the DS1307 has one alarm. When the alarm goes off the first time you would set to the new value.

(* jcl *)


www: http://www.wiblocks.com twitter: http://twitter.com/wiblocks blog: http://luciani.org

yes I have followed that link many times on other posts but my output is not going to be an electric sound, i plan to make a sound with the use of solenoids to act as an alarm signal, by 'hitting' off different materials.

Thanks for that tho, it is the only reason i have not bought it already!

You are mixing up the term "alarm". The alarm coming out of the DS1337 (or DS3107) is an electrical signal not a sound. When the DS1337 generates an alarm it generates an electrical signal that interrupts the microcontroller.

When you receive the interrupt you can do whatever you want -- set a solenoid, reset the DS1337, flash a light, etc. In my example code I output the string "** ALARM **" to the USB port.

(* jcl *)


www: http://www.wiblocks.com twitter: http://twitter.com/wiblocks blog: http://luciani.org

yeah like i said, total newbie! thats quite embarrassing...

I wanting to keep my hardware as small as possible as well and that chip is quite expensive and it would have to travel to the uk.

I was really hoping for a code to follow from a similar project like what i did with working the time or a line of code which shows me or introduces me to methods of how to set an exact time for an output as my knowledge of arduino, is well extracting existing lines of code, I can't make my own and i havn't found anything to reference or modify.

This is my best efforts to set an exact time for the alarm with if and else statements with my limited knowledge!

I can get the hour set, but for e.g i can't do 6:30, i cannot set mins so therefore cannot set an exact alarm time, where i hope i can receive some advice on, I don't want you guys thinking that i have done nothing!! (i have also tested with case statements and still can't set an exact time, here i was also trying to keep it on for a duration)

What i have been doing for the past couple of weeks is try and find an existing code online where i can just input 6:30 or something...i'm not sure what can and cannot be done:-

// read the state of the set button value:
  buttonSet = digitalRead(dayoff);   // check if the dayoff button is pressed.

  if (buttonSet == HIGH) {           // if it has NOT been pressed, complete these steps
    digitalWrite(ledgreen, HIGH);    // turn green on:THIS IS TO ACT AS AN INDICACTOR!!
  
  if (hour == 6) { 
    digitalWrite(ledblue, HIGH);       // turn on blue
  } 
  if (hour == 7) {
    digitalWrite(ledblue, LOW);        // turn off blue
    digitalWrite(ledred, HIGH);        // turn on red
    delay(1000);                       // wait 1 seconds DELAYS EVERYTHING, CLOCK AND GREEN LED??
    digitalWrite(ledred, LOW);         // turn off red
    delay(1000);                       // wait 1 seconds
  }
  
  if (hour == 8) {
    digitalWrite(ledred, HIGH);         // turn on red
    digitalWrite(ledblue, HIGH);        // turn on blue
  }
  
    else {                               // otherwise...
    digitalWrite(ledgreen, LOW);      // no lights on:
    digitalWrite(ledblue, LOW);       
    digitalWrite(ledred, LOW);
  
    }
  }
}

ok, please let me know what you think!

All that this code is doing is turning some LEDs on and off. What does this have to do with the clock?

Why n ot post all of your code? Or, at least the stuff related to reading and setting the time.

I’m just turning LEDs on and off just now to test/ confirm if the program works. ( I’m just testing if i can control an output when i state)

So this is me trying to set alarm times. Currently, i can only set exact hours and i’m really unsure of how to 'count backwards 9 hours to make an automatic alarm time without the users input.

Here is a bit about my project, for some clarity i hope!
I’m trying to improve sleep pattern / rhythm. When you set your alarm time of say 7:15, i want my device to count backwards 9 hours and signal at 22:15 to indicate a time to start winding down and getting ready for bed to help achieve a longer sleep. This night time alarm will be determined by the morning alarm set. (the users only input)

So far i have managed to get my DS1307 working, an on / off for setting the alarm and turning it off and now i’m just trying to improve setting the time. I’m not even sure if this is how to set an alarm time, i.e. am i using the right set of code?

I’m also failing to find any ’ counting ’ lines of code i could modify.

As you can see, i’ve still got quite a lot to do but I want to learn and achieve my goal, I suppose i just need someone to guide me in the right directions, for now and introduce me to correct coding techniques!

This is my full code:

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

// constants won't change. They're used here to set pin numbers:
const int ledblue = 9;         // the number of the blue pin
const int ledred = 10;         // the number of the red pin
const int ledgreen =  11;      // the number of the green pin
 
const int dayoff = 4;          // the number of the set button pin

// variables will change: memory store for inputs
int buttonSet = 1;             //variable for reading dayoff -always on unlesss pressed, so assume high value-

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(0x80);
  Wire.endTransmission();
}*/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second,        // 0-59
                   byte minute,        // 0-59
                   byte hour,          // 1-23
                   byte dayOfWeek,     // 1-7
                   byte dayOfMonth,    // 1-28/29/30/31
                   byte month,         // 1-12
                   byte year)          // 0-99
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0);
   Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));      // change here for 12 hour am/pm 
                                   // bit 6 (also need to change readDateDs1307)
   Wire.send(decToBcd(dayOfWeek));
   Wire.send(decToBcd(dayOfMonth));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}


void setup()
{
  digitalWrite(ledblue, LOW);  // start with blue off 
  digitalWrite(ledred, LOW);   // start with red off
  digitalWrite(ledgreen, LOW); // start with green off

  pinMode(ledblue, OUTPUT);    // initialize the blue led as an output:
  pinMode(ledred, OUTPUT);     // initialize the red led as an output:
  pinMode(ledgreen, OUTPUT);   // initialize the green led as an output:
 
  pinMode(dayoff, INPUT);      // initialize the set button pin as an input:
   
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  Serial.begin(9600);
   
//input time here
  second = 50;
  minute = 59;
  hour = 5;
  dayOfWeek = 5;
  dayOfMonth = 4;
  month = 01;
  year = 10;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop(){  
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print("  Day_of_week:");
  Serial.println(dayOfWeek, DEC);

  delay(1000);

  // read the state of the set button value:
  buttonSet = digitalRead(dayoff);   // check if the dayoff button is pressed.

  if (buttonSet == HIGH) {           // if it has NOT been pressed, complete these steps
    digitalWrite(ledgreen, HIGH);    // turn green on:THIS IS TO ACT AS AN INDICACTOR!!
  
  if (hour == 6) { 
    digitalWrite(ledblue, HIGH);       // turn on blue
  } 
  if (hour == 7) {
    digitalWrite(ledblue, LOW);        // turn off blue
    digitalWrite(ledred, HIGH);        // turn on red
    delay(1000);                       // wait 1 seconds DELAYS EVERYTHING, CLOCK AND GREEN LED??
    digitalWrite(ledred, LOW);         // turn off red
    delay(1000);                       // wait 1 seconds
  }
  
  if (hour == 8) {
    digitalWrite(ledred, HIGH);         // turn on red
    digitalWrite(ledblue, HIGH);        // turn on blue
  }
  
    else {                               // otherwise...
    digitalWrite(ledgreen, LOW);      // no lights on:
    digitalWrite(ledblue, LOW);       
    digitalWrite(ledred, LOW);
  
    }
  }
}

I understand what you want to do, but I don't see how this code is helping.

You really need two sketches. One sets the RTC to some known time, when a button is pressed.

The second sketch never sets the time. Instead, it only reads the time, and sounds an alarm if the time is equal to the alarm time.

As for getting one user input, and defining two different action times from that one input, that's easy.

Set wake time to the user input. Then, subtract 9 from the wake hour, and set the sleep time. If the wake hour is negative, add 24 hours.

If the wake time is 7:15, 7 - 9 = -2, so add 24, and sleep time becomes 22:15.

Every pass through loop, read the time. If it's equal to sleep time, make some soothing sound for a short period of time. If it's equal to the wake time, make some god-awful racket for some longer period of time.

You could add a snooze button to make the noise again later. Every time the snooze button is pressed, shorten the snooze time.

ok ok, brill! Thanks: I'm going to apologize in advance for this next bit...

One sets the RTC to some known time, when a button is pressed.

Instead, it only reads the time, and sounds an alarm if the time is equal to the alarm time.

So how would i go about doing those? What coding language is appropriate for setting times and reading them?

If the wake time is 7:15, 7 - 9 = -2, so add 24, and sleep time becomes 22:15.

that seems doable... but again i just don't know how or where to begin with starting to construct something like that.

My knowledge of ardunio is purely based on the ardunio examples, maybe im not looking in the right place or recognizing the code but could you point me to any examples showing this or similar that i could follow?

I'm just really stuck!

Many Thanks

The code you have now sets the time in the setup method. It lights an LED when a button is pushed.

Copy that sketch, and get rid of the code to read and display the time. Get rid of the code to light up LEDs. Move the code to set the time, to make it happen when the button is pushed.

That takes care of setting the time.

Then, get rid of the call to set the time from the original sketch. It now reads the time. There are several variables that are populated when you read the time - second, minute, hour, etc. You should be able to figure out which of these does what.

Start simple. Make an LED light up for 1/2 a second when second is 0, 10, 20, 30, 40, or 50.

Let us know how that goes for you.

Thank you! I’ll get back to you

ok, i think this is what you meant for me to do, correct me if i am wrong but i have tested it and it works, i continued to use the if statemeants:

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

// constants won't change. They're used here to set pin numbers:
const int ledblue = 9;         // the number of the blue pin
const int ledred = 10;         // the number of the red pin
const int ledgreen =  11;      // the number of the green pin


const int dayoff = 4;          // the number of the set button pin

// variables will change: memory store for inputs
int buttonSet = 1;             //variable for reading dayoff -always on unlesss pressed, so assume high value-

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(0x80);
  Wire.endTransmission();
}*/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second,        // 0-59
                   byte minute,        // 0-59
                   byte hour,          // 1-23
                   byte dayOfWeek,     // 1-7
                   byte dayOfMonth,    // 1-28/29/30/31
                   byte month,         // 1-12
                   byte year)          // 0-99
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0);
   Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.send(decToBcd(dayOfWeek));
   Wire.send(decToBcd(dayOfMonth));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}


void setup()
{
  digitalWrite(ledblue, LOW);  // start with blue off 
  digitalWrite(ledred, LOW);   // start with red off
  digitalWrite(ledgreen, LOW); // start with green off

  pinMode(ledblue, OUTPUT);    // initialize the blue led as an output:
  pinMode(ledred, OUTPUT);     // initialize the red led as an output:
  pinMode(ledgreen, OUTPUT);   // initialize the green led as an output:

  pinMode(dayoff, INPUT);      // initialize the set button pin as an input:
   
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  Serial.begin(9600);
   
  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 58;
  minute = 15;
  hour = 6;
  dayOfWeek = 5;
  dayOfMonth = 4;
  month = 01;
  year = 10;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop(){  
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print("  Day_of_week:");
  Serial.println(dayOfWeek, DEC);

  delay(1000);

  // read the state of the set button value:
  buttonSet = digitalRead(dayoff);   // check if the dayoff button is pressed.

  if (buttonSet == HIGH) {           // if it has NOT been pressed, complete these steps
    digitalWrite(ledgreen, HIGH);    // turn green on:THIS IS TO ACT AS AN INDICACTOR!!
  
  if (hour == 6) {if (minute == 17) { if (second == 0){  //exact time!!!
    digitalWrite (ledred, HIGH);
    delay(500);
    digitalWrite (ledred, LOW);
    delay(500);
  }}}
  if (second == 10){
    digitalWrite (ledred, HIGH);
    delay(500);
    digitalWrite (ledred, LOW);
    delay(500);
  }
  if (second == 20){
    digitalWrite (ledred, HIGH);
    delay(500);
    digitalWrite (ledred, LOW);
    delay(500);
  }
  if (second == 30){
    digitalWrite (ledred, HIGH);
    delay(500);
    digitalWrite (ledred, LOW);
    delay(500);
  }
  if (second == 40){
    digitalWrite (ledred, HIGH);
    delay(500);
    digitalWrite (ledred, LOW);
    delay(500);
  }
    else {
    digitalWrite(ledgreen, LOW);    
    digitalWrite (ledred, LOW);
  
  }
}
}

so even tho it is only on for 1 second, that is enough to trigger a set off 'alarm' codes?

Thank you so much for that! big step achieved! well for me anyway!

could you, or anyone, walk me through how to construct a counting system if at all possible please?

Many thanks

You can combine some of the blocks of code:

if(seconds == 0 || seconds == 10 || seconds == 20 ||
    seconds == 30 || seconds == 40 || seconds == 50)
{
   // flash the LED
}

Or, you could use the modulo operator:

if(seconds % 10 == 0)
{
   // flash the LED
}

could you, or anyone, walk me through how to construct a counting system if at all possible please?

Can you explain what you want? What do you want to count?

thank you for that feedback, i'm placing that in my code.

This is what i want my product to do:

step 1: select one of the pre-set alarm times, using a potentiometer (still to achieve)

step 2: based on this selection, a second alarm is set 9 hours behind the selected time. (this is the' bed time ' indicator)

the alarm mechanism is the solenoids (still to achieve)

currently, i don't really know where to start

This is what i have so far with my potentiometer. I have successfully managed to get ranges / divides in the potentiometer. The problem is applying the pot ranges and allocating it a set time. This currently does not work. I also had to remove the blocks of code to get my head around what i was doing.

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68

// constants won't change. They're used here to set pin numbers:
const int ledblue = 9;         // the number of the blue pin
const int ledred = 10;         // the number of the red pin
const int ledgreen =  11;      // the number of the green pin
 

const int dayoff = 4;          // the number of the set button pin

// variables will change: memory store for inputs
int buttonSet = 1;             //variable for reading dayoff -always on unlesss pressed, so assume high value-

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/*void stopDs1307()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(0x80);
  Wire.endTransmission();
}*/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second,        // 0-59
                   byte minute,        // 0-59
                   byte hour,          // 1-23
                   byte dayOfWeek,     // 1-7
                   byte dayOfMonth,    // 1-28/29/30/31
                   byte month,         // 1-12
                   byte year)          // 0-99
{
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   Wire.send(0);
   Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
   Wire.send(decToBcd(minute));
   Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   Wire.send(decToBcd(dayOfWeek));
   Wire.send(decToBcd(dayOfMonth));
   Wire.send(decToBcd(month));
   Wire.send(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}


void setup()
{
  digitalWrite(ledblue, LOW);  // start with blue off 
  digitalWrite(ledred, LOW);   // start with red off
  digitalWrite(ledgreen, LOW); // start with green off

  pinMode(ledblue, OUTPUT);    // initialize the blue led as an output:
  pinMode(ledred, OUTPUT);     // initialize the red led as an output:
  pinMode(ledgreen, OUTPUT);   // initialize the green led as an output:
 
  pinMode(dayoff, INPUT);      // initialize the set button pin as an input:
   
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  Serial.begin(9600);
   
  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 58;
  minute = 15;
  hour = 6;
  dayOfWeek = 5;
  dayOfMonth = 4;
  month = 01;
  year = 10;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop(){  
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print("  Day_of_week:");
  Serial.println(dayOfWeek, DEC);

  delay(1000);

// read the analog input into a variable:
   int analogValue = analogRead(0);
   // print the result:
   Serial.println(analogValue/146);
   // wait 10 milliseconds for the analog-to-digital converter
   // to settle after the last reading:
   delay(1000);
   
  // read the state of the set button value:
  buttonSet = digitalRead(dayoff);   // check if the dayoff button is pressed.

  if (buttonSet == HIGH) {           // if it has NOT been pressed, complete these steps
    digitalWrite(ledgreen, HIGH);    // turn green on:THIS IS TO ACT AS AN INDICACTOR!!
  
   if (analogValue/146 == 1) { if (second == 0){      //if the pot value divided by 7 is = 1 then...
    digitalWrite (ledred, HIGH);
    delay(500);
    digitalWrite (ledred, LOW);
    delay(500);
  }}
   if (analogValue/146 == 3) { if (second == 10){      //if the pot value divided by 7 and is = 3 then...
    digitalWrite (ledred, HIGH);
  }}

    else {
    digitalWrite(ledgreen, LOW);    
    digitalWrite (ledred, LOW);
  
  }
}
}

what do you think? any pointers and comments will be greatly appreciated.

and so far i can’t compose any code for counting time backwards, really struggling to get started on it…help!

Why are you dividing the value read from the analog pin (the potentiometer setting) by 146? The pot value should result in a analogRead value between 0 and 1023.

Time goes forward, always. Unless you can figure out a way to travel faster than the speed of light.

You know what time it is. You know what time the alarm should go off. It is simple to subtract 9 hours from when the alarm should go off.

I was trying to control an alarm time with a potentiometer value, so only one of the set alarms would go off if the time matched the pot value. e.g.option 1= 6:15 with a pot value=1 then alarm, this ensures not all alarms go off, just one desired by the user by turning a dial. (so a no. of alarm are available, but only 1 will go off chosen by the output of the pot)

My plan was then to assign a separate time to that pot value, so if dial 1 = 6: 15 then i would program that pot value of 1 = 22:15 to set off the ' night time ' alarm.

I am a beginner who feels out of her depth! I vaguely know how to do it, can you provide me with some clarity?