element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino project with RTC - Problem with Alarms
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 1 reply
  • Subscribers 390 subscribers
  • Views 325 views
  • Users 0 members are here
Related

Arduino project with RTC - Problem with Alarms

rockhudson
rockhudson over 9 years ago

I am trying to improve the coding to allow the 2 alarms to be used for 2 different time periods.I have had some success in getting the relay to stay activated for the desired time period.

In developing this idea I first got it working with Alarm1 on the DS3231 that I am using.  This worked fine apart for the time period was shorter than expected.  I used 60000 millis for 1 minute but it only spanned 32 secs?

This was not a major problem(I just doubled the figure to get a minute), however I want to use Alarm 2 as well, so I came up with just ORing the Alarm2 in the if statemenent.

When I tried this it completely ignored Alarm2.

Here is the line:

if(DS3231_triggered_a1()== true|| DS3231_triggered_a2()== true){

Where am I going wrong, the statement looks OK to me?

Have I got my logic wrong?

I see it as:

Alarm1 - TRUE OR Alarm2 -FALSE = TRUE

Alarm1 - FALSE OR Alarm2 - TRUE = TRUE

Alarm1 - FALSE OR Alarm2 - FALSE = FALSE

 

Any help would be appreciated!

Here is the full code:

// rtc_ds3231_alarm13 (Working with sensor)

// during an alarm the INT pin of the RTC is pulled low

//SDA - pin A4

// SCL - pin A5

// this is handy for minimizing power consumption for sensor-like devices,

// since they can be started up by this pin on given time intervals.

#include <Wire.h>

#include "ds3231.h"

#define BUFF_MAX 256

// time when to wake up

uint8_t wake1_HOUR = 17;

uint8_t wake1_MINUTE = 2;

uint8_t wake1_SECOND = 00;

 

uint8_t wake2_HOUR = 17;

uint8_t wake2_MINUTE = 5;

int relaypin = 10;

int SQWpin = 8;

int tankSensePin = A0;

int curCounter = 0;

int sensorValue = 0;

boolean Alarm1On = false;

boolean Alarm2On = false;

unsigned long StartTime=0;

unsigned long EndTime=0;

// Generally, you should use "unsigned long" for variables that hold time

// The value will quickly become too large for an int to store

// constants won't change :

unsigned long prev = 1000, interval = 1000;

void set_alarm(void) {

  // flags define what calendar component to be checked against the current time in order

  // to trigger the alarm - see datasheet

  // A1M1 (seconds) (0 to enable, 1 to disable)

  // A1M2 (minutes) (0 to enable, 1 to disable)

  // A1M3 (hour)    (0 to enable, 1 to disable)

  // A1M4 (day)     (0 to enable, 1 to disable)

  // DY/DT          (dayofweek == 1/dayofmonth == 0)

  uint8_t flags1[5] = { 0, 0, 0, 1, 1 };

 

  // A2M2 (minutes) (0 to enable, 1 to disable)

  // A2M3 (hour)    (0 to enable, 1 to disable)

  // A2M4 (day)     (0 to enable, 1 to disable)

  uint8_t flags2[3] = {0, 0, 1};

 

  // set Alarm1

  DS3231_set_a1(wake1_SECOND, wake1_MINUTE, wake1_HOUR, 0, flags1);

 

  // activate Alarm1

  DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);

 

  // set Alarm2

  DS3231_set_a2(wake2_MINUTE, wake2_HOUR, 0, flags2);

 

  // activate Alarm2

  DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);

 

}

 

void setup() {

  Serial.begin(9600);

  Wire.begin();

  pinMode(SQWpin, INPUT);

  pinMode(relaypin, OUTPUT);

  pinMode(tankSensePin, INPUT);

  DS3231_init(DS3231_INTCN);

  DS3231_clear_a1f();

  DS3231_clear_a2f();

  set_alarm();

}

 

void loop()

{

char buff[BUFF_MAX];

  unsigned long now = millis();

  struct ts t;

  // once a while show what is going on

  if ((now - prev > interval) && (Serial.available() <= 0)) {

    DS3231_get(&t);

    // display current time

    snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year,

             t.mon, t.mday, t.hour, t.min, t.sec);

             Serial.println(buff);    

int tankSenseReading = analogRead(tankSensePin);

    Serial.println(tankSenseReading);

if (tankSenseReading>500){   

Serial.println(tankSenseReading);

  Serial.println("Tank OK");

    if(DS3231_triggered_a1()== true|| DS3231_triggered_a2()== true){   

   StartTime=millis();

  

   Serial.println(StartTime);

   Serial.println(EndTime);

Serial.println("Timing Started");   

digitalWrite(relaypin, HIGH);

    }

if(now >=EndTime){

  EndTime=StartTime+120000;

  digitalWrite(relaypin, LOW);

  Serial.println("Timing Stopped");

   DS3231_clear_a1f();

}

}

prev = now;

}

}

  • Sign in to reply
  • Cancel

Top Replies

  • DAB
    DAB over 9 years ago +1
    Your first clue is that it only ran 32 seconds. The Arduino is probably using only a 16 Bit integer, so when you shove 600000 into it, you will only get a max value of about 32,767 or 32 seconds. The solution…
  • DAB
    0 DAB over 9 years ago

    Your first clue is that it only ran 32 seconds.

     

    The Arduino is probably using only a 16 Bit integer, so when you shove 600000 into it, you will only get a max value of about 32,767 or 32 seconds.

     

    The solution is easy, just implement a standard Hr, Min, Sec timer and use 1 second updates to keep track of time.

     

    Then just adjust your software accordingly.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube