I'm not sure what is wrong with the code I am using (DS3231)

I am currently working on my Year 12 Design and Technology major project and am making a clock from an arduino that uses light. I am using a DS3231 RTC module and the code I have is not working for some reason. I want the code to allow the LED to be off the whole time and only come on when I want it to at a specific time, fade out and then turn on again the following day. Could someone please go through the code I'm using and see if I need to change anything and get some help?

Thanks

#include <DS3231.h> //Include the clock library

// Changable Vars
int fadeTime = 1; // How long the light will fade to max
int setHour = 15; // Set hours to wake (military time)
int setMin = 30; // Set minute to wake
int uled = 9; // Set pinout with with PWM

// Set up Vars
DS3231  rtc(SDA, SCL);
Time t;
void start();
 
void setup()
{
  pinMode(uled, OUTPUT);
  Serial.begin(9600); // Match to serial monitor
  rtc.begin();
}

void loop()
{
  t = rtc.getTime(); // Make a time class called 't'
  
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());

  if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!
  {
    start();
  }
  
  // Wait one second before repeating
  delay (1000);
}

void start()
{
  // Fix for SB LED
  analogWrite(uled, 1);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 2);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 3);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 4);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 4);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 5);
  delay((fadeTime * 60000)/50);


  // Fade script
  for (int i = 6 ; i <= 255; i++)
  {
    analogWrite(uled, i);
    delay(((fadeTime * 60000)/306));
    Serial.print(" mil sec ");
    Serial.print(((fadeTime * 60000)/306));
    Serial.print(" PWM " );
    Serial.print(i);


  }
  
  delay(20000); // Stay Bright
  analogWrite(uled, 0); // Turn off
  
}

This is the video I used to help me set up the arduino, I followed it exactly and it does not turn on and then off how I want it to

Welcome. Please read the forum instructions, especially topic #7, how to post code. Once your code is inserted correctly, members can more easily review and test your code to provide answers. Posted inline as you have done, can result in the forum software stripping special characters and destroying the formating.

The one thing I see straight off is a bad comparison. This code will not do what you're expecting:

...
int setHour = 15; // Set hours to wake (military time)
int setMin = 30; // Set minute to wake
...
 if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!

This is because the DS3231 returns time values in base 16 format, not base 10. This explained in the device datasheet. That means you really need these values:

...
int setHour = 0x15; // Set hours to wake (military time)
int setMin = 0x30; // Set minute to wake
...
 if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!
...

If you still have difficulty, please explain what does not work. General statements like "the code I have is not working for some reason" is of no help. You must be specific about what is not working.

This is because the DS3231 returns time values in base 16 format, not base 10. This explained in the device datasheet. That means you really need these values:
...
int setHour = 0x15; // Set hours to wake (military time)
int setMin = 0x30; // Set minute to wake

Nonsense. The storage registers of the ds3231 are set up to hold the values in binary coded decimal (BCD).
The library you use #include <DS3231.h> extracts the data from the registers and puts it into a 12 or 24 hour format.

the code I have is not working for some reason.

Along with revising your code to be within the code tags, please provide a more complete explanation of "not working". What does it not do that you want it to do. What does it do that you don't want it to do??

Also, please provide a link to the library you are using. There are several with the same name.

Also, please provide a link to the library you are using. There are several with the same name.

So you you don't know which library he's using but you know it returns clock data in base 10?

Okay, I'll bow out, your physic powers are far stronger than mine.

So you you don't know which library he's using but you know it returns clock data in base 10?

You explicitly referred to the device datasheet not the library. Please provide a link which shows base 16.

DS3231 returns time values in base 16 format, not base 10. This explained in the device datasheet.

If you can provide a link to a ds3231 library which returns time in base 16 that would be appreciated. All libraries I am familiar with return either a base 10 number or a character string,

My apologies. Serious brain fart. I knew the native format wasn't base 10 and my brain (being dyslexic) spit out base 16 rather than the correct term. I screw up acronyms and spelling all the time, especially those with repeating vowels. Speil chack makes me appair smartr then eye m.

I want the code to make the led turn on at say 7:30 am, let it fade out for around 5-10 minutes and then turn off completely until it turns on again the next day at 7:30 am and does the same thing. My problem is the led stays on and the time I put in for it fade out does not do that at all.

Let's see your current sketch.

Use the </> icon in the posting menu to show us your code.

.

Clearly you didn't use the icon properly. It looks like you just copied and pasted the symbol from the suggestion. Open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

fadeTime * 60000

This calculation will overflow. fadeTime must be a long type.

#include <DS3231.h> //Include the clock library

// Changable Vars
int fadeTime = 1; // How long the light will fade to max
int setHour = 23; // Set hours to wake (military time)
int setMin = 53; // Set minute to wake
int uled = 9; // Set pinout with with PWM

// Set up Vars
DS3231  rtc(SDA, SCL);
Time t;
void start();
 
void setup()
{
  pinMode(uled, OUTPUT);
  Serial.begin(9600); // Match to serial monitor
  rtc.begin();
}

void loop()
{
  t = rtc.getTime(); // Make a time class called 't'
  
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());

  if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!
  {
    start();
  }
  
  // Wait one second before repeating
  delay (1000);
}

void start()
{
  // Fix for SB LED
  analogWrite(uled, 1);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 2);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 3);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 4);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 4);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 5);
  delay((fadeTime * 60000)/50);


  // Fade script
  for (int i = 6 ; i <= 255; i++)
  {
    analogWrite(uled, i);
    delay(((fadeTime * 60000)/306));
    Serial.print(" mil sec ");
    Serial.print(((fadeTime * 60000)/306));
    Serial.print(" PWM " );
    Serial.print(i);


  }
  
  delay(20000); // Stay Bright
  analogWrite(uled, 0); // Turn off
  
}

Sorry about the post previous but here is the code

Are you aware the delay() function freezes code execution during the delay period.

.

Try a serial debug print of

(fadeTime * 60000)/50

and see what you get

I had done that previously during prortyping aarg but the light did not fade and it stayed on

Malak44:
I had done that previously during prortyping aarg but the light did not fade and it stayed on

Oh, really? What was the debug output?

  t = rtc.getTime(); // Make a time class called 't'

Make a time class? Mmmm. Okay. No.

You made a Time type variable here:

Time t;

What you are doing in that line is assigning a value to it.

But I think if you dig deeper, you will find that Time is a struct not a class.

There was no debug output

aarg:
But I think if you dig deeper, you will find that Time is a struct not a class.

Which is (besides the standard visibility of members) exactly the same.

avr_fred:
Welcome. Please read the forum instructions, especially topic #7, how to post code. Once your code is inserted correctly, members can more easily review and test your code to provide answers. Posted inline as you have done, can result in the forum software stripping special characters and destroying the formating.

The one thing I see straight off is a bad comparison. This code will not do what you're expecting:

...

int setHour = 15; // Set hours to wake (military time)
int setMin = 30; // Set minute to wake
...
if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!




This is because the DS3231 returns time values in base 16 format, not base 10. This explained in the device datasheet. That means you really need these values: 

...
int setHour = 0x15; // Set hours to wake (military time)
int setMin = 0x30; // Set minute to wake
...
if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!
...




If you still have difficulty, please explain what does not work. General statements like "the code I have is not working for some reason" is of no help. You must be specific about what is not working.

I got this error while writing this code

#include <DS3231.h> //Include the clock library

// Changable Vars
int fadeTime = 1; // How long the light will fade to max
int setHour = 23; // Set hours to wake (military time)
int setMin = 53; // Set minute to wake
int uled = 9; // Set pinout with with PWM

// Set up Vars
DS3231  rtc(SDA, SCL);
Time t;
void start();
 
void setup()
{
  pinMode(uled, OUTPUT);
  Serial.begin(9600); // Match to serial monitor
  rtc.begin();
}

void loop()
{
  t = rtc.getTime(); // Make a time class called 't'
  
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());

  if (t.hour == setHour && t.min == setMin) // Check if it's time to wake up!
  {
    start();
  }
  
  // Wait one second before repeating
  delay (1000);
}

void start()
{
  // Fix for SB LED
  analogWrite(uled, 1);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 2);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 3);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 4);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 4);
  delay((fadeTime * 60000)/50);
  analogWrite(uled, 5);
  delay((fadeTime * 60000)/50);


  // Fade script
  for (int i = 6 ; i <= 255; i++)
  {
    analogWrite(uled, i);
    delay(((fadeTime * 60000)/306));
	Serial.debug((fadeTime * 60000)/50);
    Serial.print(" mil sec ");
    Serial.print(((fadeTime * 60000)/306));
    Serial.print(" PWM " );
    Serial.print(i);


  }
  delay(20000); // Stay Bright
  analogWrite(uled, 0); // Turn off
  
}
Arduino: 1.8.3 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/MalakAbdulahad/Downloads/SunRiseAlarm/SunRiseAlarm.ino: In function 'void start()':
SunRiseAlarm:67: error: 'class HardwareSerial' has no member named 'debug'
   Serial.debug((fadeTime * 60000)/50);
          ^
exit status 1
'class HardwareSerial' has no member named 'debug'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.