Following Alarm Clock Tutorial, Buttons do not work

I am following a tutorial on how to build an adjustable alarm clock, here is the link. I have followed the set-up and the RTC and 7 segment display seems to be working fine. However, the buttons do not seem to be working like the tutorial states as the time does not seem to be adjusted when I press them (you have to press the right most button first, which I have done). What might be the issue with the set-up or the code?

#include <Wire.h>
#include "RTClib.h"
#include <TM1637Display.h>

RTC_DS3231 rtc;

#define CLK 8
#define DIO 9

TM1637Display display(CLK, DIO);

const uint8_t blank[] = {0x00, 0x00, 0x00,0x00};

int setButton = 2; // pushbutton for setting alarm
int hourButton = 3; // pushbutton for hour
int minButton = 4; // pushbutton for minutes
int exitButton = 5; // pushbutton for exit of set alarm
int buzzer = 13;

int t, a, Hour, Min, h, m;
int set_time, alarm_time, auto_alarmStop, set_alarm_min, set_alarm_hour;

int setButtonState = 0; // pushbutton state for setting alarm
int hourButtonState = 0; // pushbutton state for hour
int minButtonState = 0;// pushbutton state for minutes
int exitButtonState = 0; // pushbutton state for exit of set alarm

int activate;

void setup() {
  
  Serial.begin(9600);  // Begin serial communication at a baud rate of 9600: 
  delay(3000);  // Wait for console opening:
  if (! rtc.begin()) {  // Check if RTC is connected correctly:
    Serial.println("Couldn't find RTC");
while (1);
  }

  if (rtc.lostPower()) { // Check if the RTC lost power and if so, set the time:
    Serial.println("RTC lost power, lets set the time!");
    // The following line sets the RTC to the date & time this sketch was compiled:
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // July 02, 2020 at 3:47am you would call:
    //rtc.adjust(DateTime(2020, 7, 02, 03, 47, 0));
  }
  
  Wire.begin();
  display.setBrightness(0xA);            
  pinMode(setButton,
INPUT_PULLUP);      
  pinMode(hourButton, INPUT_PULLUP);     
  pinMode(minButton, INPUT_PULLUP);      
  pinMode(exitButton, INPUT_PULLUP);     
  pinMode(buzzer, OUTPUT);
  activate = 0;
}

void loop() {
    
    DateTime now = rtc.now();
    Hour = now.hour();
    Min = now.minute();
    t =(now.hour()* 100 )+ now.minute();
    a=now.second()%2;
    if(a==0){display.setColon(false);}
    else{display.setColon(true);}

    switch(activate){
    case 0:
    display.showNumberDec(t, true);
    setButtonState = digitalRead(setButton);
    if(setButtonState == LOW){ delay(50); activate = 1;}
    if(t == alarm_time &&
now.second()==0){activate = 2;}
    break;
    case 1:
       display.setSegments(blank);//clear display
       display.showNumberDec(0, true);
       display.setColon(true);
       while(activate == 1){
       hourButtonState = digitalRead(hourButton);
       if(hourButtonState == LOW){
           h++;
           edit();
           set_alarm_hour = h;
           delay(200);
       }
       
       minButtonState = digitalRead(minButton);
       if(minButtonState == LOW){
          m++;
          edit();
          set_alarm_min = m;
 delay(200);
       }
       
       
       exitButtonState = digitalRead(exitButton);
       if(exitButtonState == LOW){delay(50); activate = 0;}
     }
     alarm_time = (set_alarm_hour*100)+set_alarm_min;
    break;
    case 2:
      alarm();
      display.showNumberDec(t, true);
      auto_alarmStop = alarm_time+1;
      exitButtonState = digitalRead(exitButton);
      if(exitButtonState == LOW){delay(50); activate = 0;}
      if(t == auto_alarmStop){activate = 0;}
      display.showNumberDec(t, true);
 break;
      
    }
    delay(5);
} 
 

void edit(){
  if(m==60){m=0; h++;}
  if(m<0){m=59; h--;}
  if(h==24){h=0;}
  if(h<0){h=23; m=59;}
  set_time = (h*100)+ m;
  display.showNumberDec(set_time, true);
  display.setColon(true);
}

void alarm(){
    tone(13, 500, 200);
    delay(100);
    tone(13, 1000, 200);
    delay(100);
    tone(13, 2000, 200);
    delay(100);
    tone(13, 3000, 200);
    delay(100);
    tone(13, 4000, 200);
    delay(100);
    tone(13, 3000, 200);
    delay(100);
    tone(13, 2000, 200);
    delay(100);
    tone(13, 1000, 200);
    delay(100); 
}


The green wires to the switches should go to Vcc (the red power rail), not ground.

my bad - I posted the wrong tutorial, I actually used this one. Unfortunately, even with the set-up shown above, the buttons still do not work... maybe there is an issue with the code?

Post clear photos of your wiring, please.

I should've phrased my reply properly - sorry, I took those photos myself. But, here is how I have set the circuit up now, and the buzzer seems to be working as well:

The switches still do not work however.

Have you tested the switches and switch code independently of your other code by testing a sketch that only reads the pushbuttons?

By the way, the indentation and formatting of your code is pretty loose. Please use ctrl-T in the IDE to auto format it prior to uploading it here..

Are you sure about that? OP is using INPUT_PULLUP option.

1 Like

Your photo shows the buttons wired up to Vcc. The tutorial you linked to shows them wired up to Ground. With INPUT_PULLUP, they need to go to ground.

I am terribly sorry in that I misinterpreted the original photo. Apologies.

The above means that the switches should be wired to ground and to the input, not to Vcc.

Like so:

I decided to follow the counter tutorial on the same link. It looks like it didn't work at first, but suddenly it did once I used another breadboard - so I think the breadboard could've been the issue. Retrying the alarm clock, there are times when the buttons work but they mostly don't seem to at other times, so the connections may also be the issue...

See reply #6, you need to troubleshoot the switches separately.

Now, you are playing with two other sketches that have a lot of other code that might produce side effects. "Divide and conquer".

The switch logic in those sketches is extremely primitive. It replaces normal debounce and state change detection with a single 200ms time out debounce. That makes them sluggish and creates a barrier to smooth human interaction.

The page you linked to, isn't much of a tutorial, as the explanations of the hardware and software operation is very limited.

So, I tried the debounce test on the separate buttons and realised that they did not work because they were too far away from the 5V and GND connections, my circuit can function properly now. Thanks a lot

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.