I have bought a DS3234 real-time clock because apparently they are programmable. First of all I was wondering if anyone knew how you would go about programming it to emit a signal once every minute.
Assuming it emits a signal every minute:
- How can I put the Arduino into sleep mode whilst it waits for a signal from the external clock?
- How can I program the external clock to send a signal once every minute?
- How do I use the interrupt function to run the program?
By run the program, I would like to do this:
- Use the interrupt loop to keep a count of the number of signals received from clock
- When the count reaches 15 (or any arbitrary number) switch to loop1() (this being the first code)
- Loop1 should run and then the count reset to 0, the interrupt will kick back in and keep count
- When reaches 15 again then commence loop2()
Currently I am waiting for the clock to arrive, but I made a simple sketch and was trying to get it to work. It didn't run as smoothly as I wanted but was part way there. I downloaded the TimerOne library so that I could change to clock speed. I set it to cycle once every 2.5 seconds for this experiment.
#include <TimerOne.h>
volatile byte count = 0; //
const byte interruptPin = 20; // originally I used int to define them
volatile byte state = LOW; // but started to try different things
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(20, OUTPUT); // Defines interupt pin
digitalWrite(20, LOW);
// This function changes the speed of the 16MHz crystal
// Time given in microseconds, currently cycles once every 2.5 seconds
Timer1.initialize(2500000);
// attachInterrupt(digitalPinToInterrupt(20), Timer, state);
Timer1.attachInterrupt(Timer); // Attach the service routine here
attachInterrupt(digitalPinToInterrupt(20), Timer, HIGH); // The interrupt conditions?
}
void loop()
{
Serial.println("You done it!"); // Code success
count = 0; // Resets the count
delay(50000);
digitalWrite(20, LOW); // Sets interrupt pin to low to start void Timer() again
}
void Timer()
{
if (count < 5 && digitalRead(13) == LOW)
{
digitalWrite(20, LOW);
digitalWrite( 13, HIGH); // Visual representation of clock cycle
delay(50000);
digitalWrite(13, LOW);
count = count + 1;
Serial.println(count);
delay(5000);
}
else (count >= 5); //
{ // When count is high enough, interrupt pin = HIGH
digitalWrite(20, HIGH); // therefore switch to main loop
}
}
Within this code I have not figured out how to make it sleep between cycles, however is that possible when using the internal clock?