Experiencing difficulty with coming up with a way to know when a new minute occurs. I do not care about the actual minute; just, when it occurs.
My thought is to make a web page, using images of digits to build a count down clock. Library example:
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <Wire.h> // http://arduino.cc/en/Main/Software included in download
#include <SPI.h>
#include <Ethernet.h>
#include <RTCTimedEvent.h>
int time;
int time2;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,7);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
time =60; //Initial timer value
//initial buffer for 1 timer
RTCTimedEvent.initialCapacity = sizeof(RTCTimerInformation)*1;
//event for every minute
RTCTimedEvent.addTimer(TIMER_ANY, //minute
TIMER_ANY, //hour
TIMER_ANY, //day fo week
TIMER_ANY, //day
TIMER_ANY, //month
minuteCall);
}
void loop(){
RTCTimedEvent.loop();
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.println(time2);
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
minuteCall((RTCTimerInformation* Sender)time2) {
Serial.print(RTCTimedEvent.time.minute, DEC);
time2 = time - 1;
return();
}
Why not just ask the clock what time it is, on each pass through loop? When the second value this time is less than last time, a new minute has occurred? It is not necessary to have that library hold your hand.
I think the library will work if I can pass a value from the the minuteCall function. For example start at 60 minutes, then each time minuteCall is called subtract 1 minute.
I am having trouble visualizing this step.
Code compiles, for now only print minute value when minuteCall is called.
/*
/* Arduino 1.0+
Count down timer using web display page using digit images.
digit images not implemented yet.
*/
#include <Wire.h> // http://arduino.cc/en/Main/Software included in download
#include <SPI.h>
#include <Ethernet.h>
#include <RTCTimedEvent.h> // http://code.google.com/p/ebl-arduino/wiki/RTCTimedEvent
int countDown = 60; //60 Minute countdown timer
int countTime;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,7);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Wire.begin();
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
RTCTimedEvent.initialCapacity = sizeof(RTCTimerInformation)*1;
//event for every minute
RTCTimedEvent.addTimer(TIMER_ANY, //minute
TIMER_ANY, //hour
TIMER_ANY, //day fo week
TIMER_ANY, //day
TIMER_ANY, //month
minuteCall);
}
void loop(){
RTCTimedEvent.loop();
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.println(countTime);
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
void minuteCall(RTCTimerInformation* Sender) {
Serial.println("New minute: ");
Serial.print(RTCTimedEvent.time.minute, DEC);
//I do not need to keep track of actual minute from RTC.
//Plan to use this event to subtract 1 minute from the value of countDown (60 minutes).
}
void minuteCall(RTCTimerInformation* Sender) {
Serial.println("New minute: ");
Serial.print(RTCTimedEvent.time.minute, DEC);
//I do not need to keep track of actual minute from RTC.
//Plan to use this event to subtract 1 minute from the value of countDown (60 minutes).
}
You should not be calling interrupt-dependent functions, like Serial.print() in an ISR, where interrupts are disabled.
Since this function is only called when the minute number changes, why worry about what the minute value was. If you only want to change values once a minute, approximately, using interrupts is the WRONG approach. Learn to read from the clock!
But, not in general. In that case, the print()ing doesn’t actually happen until the ISR ends. In the ISR, the data is buffered. As long as there is room in the buffer, print() will buffer the data and return.
If the buffer gets full, though, print() will block, waiting from room in the buffer. No room will ever appear, because interrupts are disabled in an ISR, and interrupts are required to make more room in the buffer.
In general, Serial.print() in an ISR is a bad idea.