hey guys
im making a clock sketch and i need help with writing the bit where when the minute button is pressed and it will change the minutes so you can... well set the clock. heres where im at
// set clock part
minutevalue = digitalRead(minutePin);
if(minutevalue == 1) {
minute++;
} else {}
my problem is that when you press the button it obviously will scroll through the minutes way to quick to be able to set the clock easily. ive tried using a delay but that didnt work. sorry im not to advanced in the coding language.
thanks
One simple thing to do would be to make the program wait until you've stopped pressing the set minute button before carrying on. Something like this (untested):
// set clock part
minutevalue = digitalRead(minutePin);
if (minutevalue == 1) {
minute++;
// now wait for user to stop pressing button
while (digitalRead(minutePin)) {
// do nothing
}
}
Of course this means you can only do one press at a time, rather than keeping the button down. But it's a start.
thanks andrew thats good enough for me. wow you must have been the first couple of people to sign up for this forum cause i thought "Andrew" would have been taken pretty fast.
okay one more problem that i cant figure out is that when ever i try to set my alarm time i press the alarm set button and then you can you know set the alarm time. well when you press the minute button it works but when you press the hour button it does change. and yet it works fine when ever you try to set the clock it self. i dont get it.
heres the code
// Alarm Clock
int ledPin = 13;
int secondPin = 4;
int minutePin = 5;
int hourPin = 6;
int weekdayPin = 7;
int alarmPin = 8;
int minutevalue;
int hourvalue;
int alarmvalue;
int second=0, minute=0, hour=1, weekday=1;
int aminute=0, ahour=1;
void setup() {
Serial.begin(9600); // start up serial communications
pinMode(secondPin, INPUT); //pins for normally closed switches to set the time
pinMode(minutePin, INPUT);
pinMode(hourPin, INPUT);
pinMode(weekdayPin, INPUT);
}
void loop() {
static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
// (static variables are initialized once and keep their values between function calls)
// CLOCK PART
if (millis() - lastTick >= 985) {
lastTick = millis();
second++; }
if(second >= 60) {
minute++;
second = 0; }
if(minute >= 60) {
hour++;
minute = 0; }
if(hour > 12) {
hour = 1; }
// SET CLOCK PART
alarmvalue = digitalRead(alarmPin);
// Set minutes:
minutevalue = digitalRead(minutePin);
if(alarmvalue == 0) {
if(minutevalue == 1) {
minute++;
while (digitalRead(minutePin)) {
}
}
// Set hours:
hourvalue = digitalRead(hourPin);
if(hourvalue == 1) {
hour++;
while (digitalRead(hourPin)) {
}
}
Serial.print(hour);
Serial.print( ":" );
Serial.println(minute);
}
// SET ALARM PART
// Set alarm minutes:
if(alarmvalue == 1) {
if(minutevalue == 1) {
aminute++;
while (digitalRead(minutePin)) {
}
if(aminute >= 60) {
aminute = 0; }
}
// Set alarm hours:
if(hourvalue == 1) {
ahour++;
while (digitalRead(hourPin)) {
}
if(ahour > 12) {
ahour = 1; }
}
Serial.print(ahour);
Serial.print( ":" );
Serial.println(aminute);
}
}
You are missing a digitalRead(hourPin) before you test it in the part of your code where you set the alarm.
You could also use Tools > Auto Format (Ctrl-T) to clean up the hierarchy of your blocks. It was easier for me to compare the code where you set the time, to the code where you set the alarm, after I did that. You might find it helpful too. Lots of times people don't know it's there.