Display Days Without An Accident

Hope u guys could help me. I need to make a display (three digits) to count days, just like that. I need to add the day the momento the clock hits 12:00 am so I guess I needed to use a RTC. I’d like you guys to check my code so you can understand what I’m talking about, sorry for my bad english!

#include <Time.h>
#include <TimeLib.h>

#include <TimeAlarms.h>

byte a = 0;
byte b = 0;
byte c = 0;
int BotonReset = 13;
double cont, B;

void setup() {
DDRD = B11111111;
DDRB = B00011111;
pinMode (BotonReset, INPUT);
Serial.begin(9600);
setTime(11,6,0,3,10,16);

Alarm.alarmRepeat(0,0,0, MorningAlarm);
}

void loop() {
PORTD = a;
PORTB = 1;
delay (50);

PORTD = b;
PORTB = 2;
delay (50);

PORTD = c;
PORTB = 4;
delay (50);

void MorningAlarm(){
b=b+1
}
if (b>9)
{
b=0;
a=a+1;
}
if (a>9)
{
a=0;
c=c+1;
}
if (c>9)
{
c=0;
}
B = digitalRead(BotonReset);
if (B == 1)
{
a=0;
b=0;
c=0;
}
}

Nothing wrong with your English.

Only something wrong with way you posted your code ;) Please edit your post and

type [code] before the code type [/code] after the code

so it looks like

your code here

You seem to have MorningAlarm defined inside of loop. That's not going to work.

sterretje:
Nothing wrong with your English.

Only something wrong with way you posted your code :wink: Please edit your post and

type [code] before the code
type [/code] after the code

so it looks like

your code here

Oh, I’m sorry, I’m new here :slight_smile:

Here it is!

#include <Time.h>
#include <TimeAlarms.h>

void setup()
{
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
  // create the alarms 
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day 
  Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm);  // 8:30:30 every Saturday 

 
  Alarm.timerRepeat(15, Repeats);            // timer for every 15 seconds    
  Alarm.timerOnce(10, OnceOnly);             // called once after 10 seconds 
}

void  loop(){  
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
  Serial.println("Alarm: - turn lights off");    
}

void EveningAlarm(){
  Serial.println("Alarm: - turn lights on");           
}

void WeeklyAlarm(){
  Serial.println("Alarm: - its Monday Morning");      
}

void ExplicitAlarm(){
  Serial.println("Alarm: - this triggers only at the given date and time");       
}

void Repeats(){
  Serial.println("15 second timer");         
}

void OnceOnly(){
  Serial.println("This timer only triggers once");  
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Delta_G: You seem to have MorningAlarm defined inside of loop. That's not going to work.

That's what I thought but I saw an example and copy paste it, in the example "MorningAlarm" was defined inside the loop, I just don't get it, I trully suck at coding, but besides all the mistakes inside my code, do you understand what I intend to do? I hope you guys can help me, greetings!

OK, that code looks better. What's the issue? Does it work? If not, what does it do or not do and how does that compare to what you expected?

Delta_G: OK, that code looks better. What's the issue? Does it work? If not, what does it do or not do and how does that compare to what you expected?

Right now I'm having a childish issue, I guess, it has something to do with the libraries I'm using, mind if you have a look, I'll be truly thankfull.

 Prueba_time_DSA.ino: In function 'void setup()':
Prueba_time_DSA:22: error: 'MorningAlarm' was not declared in this scope
Prueba_time_DSA.ino: In function 'void loop()':
Prueba_time_DSA:40: error: a function-definition is not allowed here before '{' token
Multiple libraries were found for "Time.h"
 Used: C:\Users\ERIK KURI\Documents\Arduino\libraries\Time-master
 Not used: C:\Users\ERIK KURI\Documents\Arduino\libraries\Time
Multiple libraries were found for "TimeAlarms.h"
 Used: C:\Users\ERIK KURI\Documents\Arduino\libraries\TimeAlarms-master
 Not used: C:\Users\ERIK KURI\Documents\Arduino\libraries\TimeAlarms
'MorningAlarm' was not declared in this scope

As you can see, it says that MorningAlarm is defined, but I think it has something to do with it's respective library. I feel pretty ashamed for having this kind of trouble, you see, I'm new with this kind of stuff and I'm not familiar with it.

It looks like you have multiple copies of Time and TimeAlarms libraries. You need to look and remedy that.

Are these errors for the code in the first post or the code you posted later? It looks like it is for the code in the first post and it is complaining about just what I told you. You can't have a function definition inside another function. Get the braces sorted out and it should compile.

Use TimeLib.h not Time.h - the new version of the compiler added in 1.6.10 has it's own time.h, and this conflicts with the old Arduino one, so Time.h (the arduino library) had to be renamed to TimeLib.h. You should not be including Time.h, nor should any of the libraries you're using (they may need to be updated).

Already fixed the libraries problem and now the only issue I have is about the function defined inside the other function ,which I didn’t understand, well, kind of, the thing here is that my code is based in a code a found in the internet. Let me show you the comilation error and the code I’m using from the internet.

 Prueba_time_DSA.ino: In function 'void setup()':
Prueba_time_DSA:22: error: 'MorningAlarm' was not declared in this scope
Prueba_time_DSA.ino: In function 'void loop()':
Prueba_time_DSA:40: error: a function-definition is not allowed here before '{' token
'MorningAlarm' was not declared in this scope

And this is the code:

 * TimeAlarmExample.pde
 *
 * This example calls alarm functions at 8:30 am and at 5:45 pm (17:45)
 * and simulates turning lights on at night and off in the morning
 * A weekly timer is set for Saturdays at 8:30:30
 *
 * A timer is called every 15 seconds
 * Another timer is called once only after 10 seconds
 *
 * At startup the time is set to Jan 1 2011  8:29 am
 */
// Questions?  Ask them here:
// http://forum.arduino.cc/index.php?topic=66054.0

#include <TimeLib.h>
#include <TimeAlarms.h>

AlarmId id;

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor

  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  // create the alarms, to trigger at specific times
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day
  Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm);  // 8:30:30 every Saturday

  // create timers, to trigger relative to when they're created
  Alarm.timerRepeat(15, Repeats);           // timer for every 15 seconds
  id = Alarm.timerRepeat(2, Repeats2);      // timer for every 2 seconds
  Alarm.timerOnce(10, OnceOnly);            // called once after 10 seconds
}

void loop() {
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm() {
  Serial.println("Alarm: - turn lights off");
}

void EveningAlarm() {
  Serial.println("Alarm: - turn lights on");
}

void WeeklyAlarm() {
  Serial.println("Alarm: - its Monday Morning");
}

void ExplicitAlarm() {
  Serial.println("Alarm: - this triggers only at the given date and time");
}

void Repeats() {
  Serial.println("15 second timer");
}

void Repeats2() {
  Serial.println("2 second timer");
}

void OnceOnly() {
  Serial.println("This timer only triggers once, stop the 2 second timer");
  // use Alarm.free() to disable a timer and recycle its memory.
  Alarm.free(id);
  // optional, but safest to "forget" the ID after memory recycled
  id = dtINVALID_ALARM_ID;
  // you can also use Alarm.disable() to turn the timer off, but keep
  // it in memory, to turn back on later with Alarm.enable().
}

void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

You probably need to either move the MorningAlarm (and sibblings) to the top of the code or you need to provide a prototype.

Prototype example

#include <TimeLib.h>
#include <TimeAlarms.h>

AlarmId id;

// prototypes
void MorningAlarm();
void EvevningAlarm();
void WeeklyAlarm();
void ExplicitAlarm();
void Repeats();
void Repeats2();
void OnceOnly();

void setup()
{
  ...
  ...
}

void setup()
{
  ...
  ...
}

// functions to be called when an alarm triggers:
void MorningAlarm()
{
  Serial.println("Alarm: - turn lights off");
}

remainder of your functions
...
...

The compiler needs to know about the syntax of functions before you use them. The IDE tries to tries to be clever and do it for you but does get it wrong at occasion. And I think this is one of those occasions.

Would it be easier just to have a constant '0' display and make sure an accident happens at work each day ? :)

So I finally got my code to compile but now when I want to simulate it on PROTEUS it doesn’t work, let me show you the picture of the simulation, you will see that on my display there is a number 2, and it never changes. On my Arduino the pin 1 is HIGH and I use a BCD to 7 segments decoder. I use the transistors to multiplex(?) I don’t know if that is a word. I’ll give you the code too, hope you can help me…

#include <DS1307RTC.h>
#include <Wire.h>
#include <TimeAlarms.h>
#include <TimeLib.h>


byte c;
byte b=0;
byte a=0;
int cont;
int BotonReset = 13;
int Bot;
void MorningAlarm();
void setup() {
  Serial.begin(9600);
  while (!Serial);
  setSyncProvider(RTC.get);

  setTime(15,14,00,10,10,2016);
  
  if(timeStatus()!=timeSet)
       Serial.println("Unable to sync with the RTC");
        else
             Serial.println("RTC has set the system time");

Alarm.alarmRepeat(15,15,00, MorningAlarm);
pinMode (BotonReset, INPUT);
DDRD = B11111111;
DDRB = B00011111;

}

void loop() {
  // put your main code here, to run repeatedly:
b=cont;
PORTD = b;
PORTB = 1;
delay (50);

PORTD = c;
PORTB = 2;
delay (50);

PORTD = a;
PORTB = 4;
delay (50);
}
void MorningAlarm(){
cont=cont+1;
if (cont>9)
{
  cont=0;
  c=c+1;
}
if (c>9)
{
  c=0;
  a=a+1;
}
if (a>9)
{
  a=0;
}
Bot = digitalRead (BotonReset);
if (Bot == 1)
{
  cont = 0;
  a = 0;
  b = 0;
  c = 0;
}
}

kurioso92:
So I finally got my code to compile but now when I want to simulate it on PROTEUS it doesn’t work, let me show you the picture of the simulation, you will see that on my display there is a number 2, and it never changes. On my Arduino the pin 1 is HIGH and I use a BCD to 7 segments decoder. I use the transistors to multiplex(?) I don’t know if that is a word. I’ll give you the code too, hope you can help me…

I don’t see any picture.

And are you sure that the problem is not with PROTEUS?

Besides, you’re better off not using alarms. Just remember the date of the most recent accident and calculate how many days from that date to today’s date. Here is a useful helper function:

uint16_t ymdToDays(uint8_t y, uint8_t m, uint8_t d) {
  // Converts date (year, month, day) to day number.
  // Input the year as a number from 0 to 99. (0 means AD 2000)
  
  // some basic sanity checks
  if (y>99) return 0xFFFF;
  if ((m<1)||(m>12)) return 0xFFFF;
  if ((d<1)||(d>31)) return 0xFFFF;
  
  y+=4; // so that the next line can't make it go negative
  if (m<=2) {y--; m+=12;} // treat Jan. and Feb. as part of preceding year
  
  uint16_t n=365*y; // whole years (not counting leap days)
  n+=(y>>2); // add in the leap days
  n+=(31*(uint16_t)m); // take care of months (assuming 31 days per month)
  
  // the next few lines compensate for short months
  if     (m>11) n-=4;
  else if (m>9) n-=3;
  else if (m>6) n-=2;
  else if (m>4) n-=1;
  
  n+=d; // take care of the day of the month
  
  return (n-1495); // make 2000-01-01(Sat) be day 0
}

And here is useful sketch for making sure your RTC is working.
(It writes the time to the Serial monitor. Just make sure you select the correct Serial monitor baud rate.)

#include <Wire.h>

byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
 
void setup()
{
  Wire.begin();
  Serial.begin(9600); // this should match your Serial monitor baud rate
 
  // clear /EOSC bit
  // Sometimes necessary to ensure that the clock
  // keeps running on just battery power. Once set,
  // it shouldn't need to be reset but it's a good
  // idea to make sure.
//  Wire.beginTransmission(0x68); // address DS3231
//  Wire.write(0x0E); // select register
//  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
//  Wire.endTransmission();
}
 
void loop()
{
  // ask RTC for the time
  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((byte)0); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
  // check for a reply from the RTC, and use it if we can
  if (Wire.available() >= 7) { 
    // if we're here, we got a reply and it is long enough
    // so now we read the time
    ss = bcd2bin(Wire.read()); // get seconds
    mi = bcd2bin(Wire.read()); // get minutes
    hh = bcd2bin(Wire.read()); // get hours
    wd = bcd2bin(Wire.read());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC"); 
  }
  delay(500);
}

byte bcd2bin(byte x) {
  // converts from binary-coded decimal to a "regular" binary number
  return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}

void printTime() {
  // just like it says on the tin
  Serial.print ("\'");
  if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
  if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
  if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print("  ");
  if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
  if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
  if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}