Hi n00b, I am building a digital clock which counts the(50HZ) mains supply to generate time. I am at the stage of working out how to set the mins, hours. I tried using the your code for void set_time but it wouldnt work after including the necessary stuff. I am sending my code so far, so if yourself or anyone can help.
//#define hours_adjust 6 // Input pin for hours adjust button
#define minutes_adjust 7 // Input pin for minutes adjust button
int pulsePin = 3;
int last;
//int hours_increase = 0;
int minutes_increase = 0; // not seeting yet
unsigned long counter = 0;
unsigned long duration = 0;
unsigned long timeout = 1000000; // in microseconds
volatile int pulses = 0;
volatile int seconds = 0;
volatile int minutes = 0;
volatile int hours = 0;
volatile int days = 0;
void set_time() { // Function for setting the time
//pinMode(hours_adjust,INPUT); // Set the adjust buttons as inputs
pinMode(minutes_adjust,INPUT);
//hours_increase = digitalRead(hours_adjust); // Read the adjust buttons
minutes_increase = digitalRead(minutes_adjust); //read input value and store it
while(minutes_increase==HIGH){ // If either of the adjust buttons are high
//if (hours_increase==HIGH){// Increase hours if hours button pressed
//hours = hours + 1;
//}
// check whether the input is HIGH (button pressed)
if (minutes_increase == HIGH){// Increase minutes if minutes button pressed
minutes = minutes + 1;
}
}
}
void pulse_count() {
pulses++;
if (pulses == 50) {
pulses = 0; // reset
seconds ++;
}
// move forward one minute every 60 seconds
if (seconds >= 60) {
minutes++;
seconds = 0; // reset seconds to zero
Serial.println("minutes");
}
// move forward one hour every 60 minutes
if (minutes >= 60) {
hours++;
minutes = 0; //resets minutes to zero
Serial.println("hours");
}
if (hours >=24) {
hours = 0;
minutes = 0; // resets minutes to zero
days++;
Serial.println("days");
}
}
void setup() {
pinMode(pulsePin, INPUT);
pinMode(minutes_adjust, INPUT);
// enable the 20K pull-up resistor to steer
// the input pin to a HIGH reading.
digitalWrite(pulsePin, HIGH);
Serial.begin(9600);
attachInterrupt (1, pulse_count, RISING);
set_time(); // Run the time set function
Serial.println("Here we go");
}
void loop(){
if (seconds != last) {
counter++;
Serial.print(hours);
Serial.print(", ");
Serial.print(minutes);
Serial.print(", ");
Serial.print(seconds);
Serial.print(", ");
Serial.println();
last = seconds;
}
}
thanks