Okay...
So I believe this is what I've wanted to accomplish. It may not be pretty, but it is a starting point. Basically, if you press "Button 1" the timer starts. Pressing "Button 2" will then start a secondary timer. The secondary timer will continue to increment as long as Button 2 is pressed. Releasing Button 2 will stop the secondary timer. If you press Button 2 again, the timer will continue from where it left off.
// Program Size: 6,660/32,256 bytes
#include <Wire.h>
const int button1 = 2;
const int button2 = 3;
const int LED = 13;
boolean butt1State, butt2State;
int seconds, minutes, hours,
init_seconds, init_minutes, init_hours,
elap_seconds, elap_minutes, elap_hours,
t_seconds, t_minutes, t_hours,
last_second;
boolean passed_second=true;
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Wire.begin();
Serial.begin(9600);
// 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() {
butt1State=digitalRead(button1);
butt2State=digitalRead(button2);
if(butt1State==LOW) {
initialTime();
Serial.print(init_hours);Serial.print(":");Serial.print(init_minutes);Serial.print(":");Serial.println(init_seconds);
Serial.print("Ti");Serial.print("\t");Serial.print("T");Serial.print("\t");Serial.print("Te");Serial.print("\t");Serial.println("Tt");
delay(1000);
}
if(digitalRead(button2)==LOW) { // Use digitalRead(button2)==LOW for start/stop
elapsedTime();
if(last_second != seconds && (seconds%60!=0)) {
t_seconds+=seconds/seconds;
last_second=seconds;
} else if(seconds%60==0 && passed_second==true) {
t_seconds++;
passed_second=false;
}
if(t_seconds==60) {
t_seconds=0;
t_minutes++;
}
// When 60 seconds has been reached and if the last minute has changed after a second has gone by
// Increase the minute by 1
if(seconds%60==0 && passed_second==true) {
minutes++; // 1 minute increase
passed_second=false; // A second has not passed yet, wait for it to pass
}
// Wait for a full second to pass
if((elap_seconds-init_seconds)==1) {
passed_second=true;
}
Serial.print(init_hours);Serial.print(":");Serial.print(init_minutes);Serial.print(":");Serial.print(init_seconds);Serial.print("\t");
Serial.print(elap_hours);Serial.print(":");Serial.print(elap_minutes);Serial.print(":");Serial.print(elap_seconds);Serial.print("\t");
Serial.print(hours);Serial.print(":");Serial.print(minutes);Serial.print(":");Serial.print(seconds);Serial.print("\t");
Serial.print(t_hours);Serial.print(":");Serial.print(t_minutes);Serial.print(":");Serial.println(t_seconds);
}
}
int initialTime() {
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write(0x00); // start at register
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
while(Wire.available()) {
init_seconds = Wire.read();
init_seconds = (((init_seconds & 0b11110000)>>4)*10 + (init_seconds & 0b00001111)); // convert BCD to decimal
}
}
int elapsedTime() {
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write(0x00); // start at register
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
while(Wire.available()) {
elap_seconds = Wire.read();
elap_seconds = (((elap_seconds & 0b11110000)>>4)*10 + (elap_seconds & 0b00001111)); // convert BCD to decimal
seconds = (((60 + elap_seconds) - init_seconds) % 60);
}
}
EDIT: Now this code will time how long each button is pressed. initialTime(); is called once in Setup and only once again in Loop.
N.B. Here are some of the things to work out: (1)This code can not count both buttons at the same time (not needed for my purposes); (2)the code is repetitive, try and make a new function; (3)Overall Timer does not increment minutes
// Program Size: 6,660/32,256 bytes
#include <Wire.h>
const int button1 = 2;
const int button2 = 3;
const int LED = 13;
boolean butt1State, butt2State;
int seconds, minutes, hours,
init_seconds, init_minutes, init_hours,
elap_seconds, elap_minutes, elap_hours,
b1_seconds, b1_minutes, b1_hours,
b2_seconds, b2_minutes, b2_hours,
last_second;
boolean passed_second=true;
boolean start=true;
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Wire.begin();
Serial.begin(9600);
// 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();
initialTime();
Serial.print("Start Time: ");Serial.print(init_hours);Serial.print(":");Serial.print(init_minutes);Serial.print(":");Serial.println(init_seconds);
Serial.print("Time");Serial.print("\t");Serial.print("B1 Timer");Serial.print("\t");Serial.println("B2 Timer");
}
void loop() {
if(start==true) {
initialTime();
start=false;
}
butt1State=digitalRead(button1);
butt2State=digitalRead(button2);
if(digitalRead(button1)==LOW) { // Use digitalRead(button#)==LOW for start/stop
elapsedTime();
if(last_second != seconds && (seconds%60!=0)) {
b1_seconds+=seconds/seconds;
last_second=seconds;
} else if(seconds%60==0 && passed_second==true) {
b1_seconds++;
passed_second=false;
}
if(b1_seconds==60) {
b1_seconds=0;
b1_minutes++;
}
// When 60 seconds has been reached and if the last minute has changed after a second has gone by
// Increase the minute by 1
if(seconds%60==0 && passed_second==true) {
//minutes++; // 1 minute increase
passed_second=false; // A second has not passed yet, wait for it to pass
}
// Wait for a full second to pass
if((elap_seconds-init_seconds)==1) {
passed_second=true;
}
Serial.print(hours);Serial.print(":");Serial.print(minutes);Serial.print(":");Serial.print(seconds);Serial.print("\t");
Serial.print(b1_hours);Serial.print(":");Serial.print(b1_minutes);Serial.print(":");Serial.print(b1_seconds);Serial.print("\t");
Serial.print(b2_hours);Serial.print(":");Serial.print(b2_minutes);Serial.print(":");Serial.println(b2_seconds);
}
if(digitalRead(button2)==LOW) { // Use digitalRead(button#)==LOW for start/stop
elapsedTime();
if(last_second != seconds && (seconds%60!=0)) {
b2_seconds+=seconds/seconds;
last_second=seconds;
} else if(seconds%60==0 && passed_second==true) {
b2_seconds++;
passed_second=false;
}
if(b2_seconds==60) {
b2_seconds=0;
b2_minutes++;
}
// When 60 seconds has been reached and if the last minute has changed after a second has gone by
// Increase the minute by 1
if(seconds%60==0 && passed_second==true) {
minutes++; // 1 minute increase
passed_second=false; // A second has not passed yet, wait for it to pass
}
// Wait for a full second to pass
if((elap_seconds-init_seconds)==1) {
passed_second=true;
}
Serial.print(hours);Serial.print(":");Serial.print(minutes);Serial.print(":");Serial.print(seconds);Serial.print("\t");
Serial.print(b1_hours);Serial.print(":");Serial.print(b1_minutes);Serial.print(":");Serial.print(b1_seconds);Serial.print("\t");
Serial.print(b2_hours);Serial.print(":");Serial.print(b2_minutes);Serial.print(":");Serial.println(b2_seconds);
}
}
int initialTime() {
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write(0x00); // start at register
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
while(Wire.available()) {
init_seconds = Wire.read();
init_seconds = (((init_seconds & 0b11110000)>>4)*10 + (init_seconds & 0b00001111)); // convert BCD to decimal
}
}
int elapsedTime() {
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write(0x00); // start at register
Wire.endTransmission();
Wire.requestFrom(0x68, 1);
while(Wire.available()) {
elap_seconds = Wire.read();
elap_seconds = (((elap_seconds & 0b11110000)>>4)*10 + (elap_seconds & 0b00001111)); // convert BCD to decimal
seconds = (((60 + elap_seconds) - init_seconds) % 60);
}
}