Adding button to the code

hello, I build a nixie clock recently and found out the dst is not programmed in the rtc.
I tried making a button that adds an hour when pushed but it wont work. can somebody give tips on the code?

the button is from A3 (pin 17) to ground

#include <MD_DS1307.h>
#include <Wire.h>
#define A1 1
#define B1 2
#define C1 3
#define D1 4
#define A2 5
#define B2 6
#define C2 7
#define D2 8
#define A3 9
#define B3 10
#define C3 11
#define D3 12
#define A4 13
#define B4 14
#define C4 15
#define D4 16
const byte DSTset = 17;
char A[4] = {A1, A2, A3, A4};
char B[4] = {B1, B2, B3, B4};
char C[4] = {C1, C2, C3, C4};
char D[4] = {D1, D2, D3, D4};
int zero;
int one;
int two;
int three;
int hour;
int minute;
int minute_old;
void setup() {
  pinMode(A1, OUTPUT);
  pinMode(B1, OUTPUT);
  pinMode(C1, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(B2, OUTPUT);
  pinMode(C2, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(B3, OUTPUT);
  pinMode(C3, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(B4, OUTPUT);
  pinMode(C4, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(DSTset, INPUT_PULLUP);
  for (char i = 0; i < 4; i++) {
    digitalWrite(A[i], HIGH);
    digitalWrite(B[i], HIGH);
    digitalWrite(C[i], HIGH);
    digitalWrite(D[i], HIGH);
  }
for (int i = 0; i < 10; i++) {
for (int m = 0; m < 4; m++){
    writenumber(m, i);
  }
    delay(100);
  }
  if (!RTC.isRunning())
    RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
//  Serial.begin(9600);
  minute_old = (RTC.m / 10) % 10;
}
void loop() {
  RTC.readTime();
  hour = RTC.h;
  minute = RTC.m;
  zero = (hour / 10) % 10;
  one = hour % 10;
  two =  (minute / 10) % 10;
  three = minute % 10;
//  Serial.println(zero);
//  Serial.println(one);
//  Serial.println(two);
//  Serial.println(three);
  if (minute_old != two) {
  sweep();
  writenumber(2, two);
  minute_old = two;
  }
  else {
  writenumber(0, zero);
  }
  writenumber(1, one);
  writenumber(2, two);
  writenumber(3, three);
  delay(1000);
  
  const bool SwitchState = digitalRead(DSTset); 
  if (SwitchState == HIGH)  // read DST switch
  {
  hour++;  // add 1 to the hours
  }
}

void writenumber(int a, int b) {
  switch (b) {
    case 1:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 2:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 3:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 4:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], LOW);
      break;
    case 5:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 6:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 7:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 8:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], HIGH);
      digitalWrite(C[a], HIGH);
      digitalWrite(D[a], LOW);
      break;
    case 9:
      digitalWrite(A[a], LOW);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], HIGH);
      break;
    case 0:
      digitalWrite(A[a], HIGH);
      digitalWrite(B[a], LOW);
      digitalWrite(C[a], LOW);
      digitalWrite(D[a], HIGH);
      break;
  }
}

void off(int a) {
  digitalWrite(A[a], HIGH);
  digitalWrite(B[a], HIGH);
  digitalWrite(C[a], HIGH);
  digitalWrite(D[a], HIGH);
}

void sweep() {
  for (int i = 0; i < 10; i++) {
  for (int m = 0; m < 4; m++){
  writenumber(m, i);
  }
  delay(100);
  }
}

Welcome to the forum

    pinMode(DSTset, INPUT_PULLUP);

The DSTset pin will held HIGH unless your circuit takes it LOW

Bearing that in mind, why are you testing whether the pin state is HIGH in this code ?

    const bool SwitchState = digitalRead(DSTset);
    if (SwitchState == HIGH)  // read DST switch
    {
        hour++;  // add 1 to the hours
    }

How is the switch wired ?

wow thank you for the quick response.

well I tested it at first with low so I tested it after with high and left it there because neither worked, but i understand why it should be low.

The switch is solderd to A3, from there i go to a switch which is normally open (NO) and the common of the switch is connected to GND

OK. You certainly need to test for LOW

However, at the start of loop() you have

    RTC.readTime();
    hour = RTC.h;

so anything that you do to hour as a result of reading the button is immediately overwritten by the value read from the RTC

If you want to add/subtract an offset from the RTC hour then you need the button to change the offset value and add the offset to hour before displaying it

Take a look at the StateChangeDetection example in the IDE to see how to add 1 to a variable each time a button is pressed. Note that further code may be needed to eliminate the effect of the switch contacts bouncing and causing false inputs

well

well the point of the button is that i push it when dst sets in and when it ends push it again (latching button) so its back to normal without dst time.

If you mentioned before that it was a latching button then I missed it. I was mislead by the hour++;

What happens when you detect LOW as a button press and add 1 to the hour ? Even if that works then it is wrong. Presumably when the hour is 23 you want to adjust it to zero so you need more logic code to deal with that

sorry didn't mention that before.

damn this is going way over my head.
I hoped this was an easy fix.

The code that I have right now comes from a tutorial and I have no idea how to adjust everything for it to work.
do you have an example?

Thats why I tried to add the hour just after the rtc reading so it adjusts automatically but I guess that was to easy for it to work

Start by moving the DST adjustment code to before you display the time. Where you have it currently the hour is read again before you show it so adding 1 to it has no effect

Once you have got it working with adding 1 to the hour when the switch is closed then you can take care of the change from 23 to 0 or 12 to 1 depending on how you are displaying the time

okay thanks for your help sir

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