Help with Buttons

Howdy All, I am totally new the this. However i am trying to work out how to wire up two buttons. I am making a clock and need to apply two buttons, one for Hours and the other for Minutes.

This is the code i have.

valm = digitalRead(A1);    // add one minute when pressed
   if(valm== HIGH) {
     minute++;
     if (minute >=59) {
      minute = 0;
     }
     second=0;
     rtc.setTime(hour, minute, second);
     delay(100);
  }

  valh = digitalRead(A2);    // add one hour when pressed
   if(valh==HIGH) {
   hour++;
   if (hour>=24) {
    hour = 0;
   }
   second=0;
   rtc.setTime(hour, minute, second);
   delay(100);
  }

I understand that They need to be connected to A1 & A2. But as for how, i am clueless. Any help would be welcome.

Based on your code, you need to wire a resistor from A1 to GND as well as from A2 to GND and connect each button between the pin and VCC.

Welcome to the forum

Your topic has been moved to the Programming category of the forum

The easiest way to use button pin inputs on an Arduino is to use a pinMode() of INPUT_PULLUP to turn on the built in pullup resistor. This ensures that the pin is normally in a known HIGH state and not floating at an unknown voltage, maybe HIGH, maybe LOW, maybe changing.

To detect a button press the button is wired between the input pin and GND so that when the button is pressed the state of the pin changes to LOW. This can be detected and acted upon. Some users have problems with the notion that LOW means pressed and HIGH means not pressed but the processor does not care. Think of it as the button on the pushbutton being lower when pressed if it helps

Applying this principle to your code snippet requires some changes because presumably you want to increment minute when the button attached to A1 is pressed. So you need to test for LOW rather than HIGH

However, as you have not posted the whole sketch the snippet cannot be seen in context. For instance, what happens if the user holds down a button ? How fast should the associated value change ?

Please post your full sketch. Thank you for using code tags when posting the snippet. Many new users don't do that

Hi @ministry85

welcome to the arduino-forum

You should give each and everything in your code a meaningful name

In your case this means

const byte minuteButtonPin = A1;
const byte hourButtonPin   = A2;

valm = digitalRead(minuteButtonPin);    // comment no longer nescessary because code explains ITSELF // add one minute when pressed
   if(valm == HIGH) {
     minute++;
     if (minute >= 59) {
      minute = 0;
     }

     second = 0;
     rtc.setTime(hour, minute, second);
     delay(100);
  }

  valh = digitalRead(hourButtonPin);    // comment no longer nescessary because code explains ITSELF // add one hour when pressed
   if(valh == HIGH) {
   hour++;
   if (hour >= 24) {
    hour = 0;
   }

   second = 0;
   rtc.setTime(hour, minute, second);
   delay(100);
  }

Here is a tutorial that shows the different methods how to connect switches or buttons to a microcontroller and their advantages and disadvantages

best regards Stefan

So i have made a diagram of what i think i have learnt from what you both said.

Does this look right?

No.

If you use INPUT_PULLUP in pinMode() then you don't need any external resistors

Here is a diagram showing several ways to connect switches to an Arduino


I am afraid that I can't remember where I got this diagram so cannot credit the originator, but he is a member here. If anyone can remind me I would be grateful

Look at button S3.

Note that there is no external resistor and only 2 connections to the switch. Pin D3 of the Arduino is normally HIGH because of the INPUT_PULLUP and when the button is pressed pin D3 of the Arduino goes LOW. As you can see, it is the simplest way to wire a switch

I wonder if it helped if i put the code in which im using to make this Binary clock. Its from the guys who make the Binary Clock House: City Clock House

/*
An open-source binary clock for Arduino.
Based on the code from by Rob Faludi (http://www.faludi.com)
Code under (cc) by Lucas Berbesson
http://creativecommons.org/license/cc-gpl
*/
#include <DS3231.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

// Init a Time-data structure
Time  t;


int second=0, minute=0, hour=0,month=0,date=0; //start the time on 00:00:00
int munit,hunit,minuteTens,hourTens,valm=0,valh=0,ledstats,i;
// LEDS positions matrix
int leds[4][4] = {
 {17,1,17,0},
 {17,13,2,3},
 {10,9,7,4},
 {11,12,8,5}
};
void setup() {
  //set outputs
  for(int k=0;k<=13;k++) {
    pinMode(k, OUTPUT);
    digitalWrite(k, HIGH);
    
  }
  delay(400); 
  for(int k=0;k<=13;k++) {
    digitalWrite(k, LOW);
  }
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  rtc.begin();
}

void loop() {
  t = rtc.getTime();
  second = t.sec;
  minute = t.min;
  hour = t.hour;


  munit = minute%10; //sets the variable munit and hunit for the unit digits
  hunit = hour%10;
  minuteTens = (int)(minute/10);
  hourTens = (int)(hour/10);
  //minutes units
  if(munit & 1) {  digitalWrite(leds[3][3], HIGH);} else {  digitalWrite(leds[3][3],LOW);}
  if(munit & 2) {digitalWrite(leds[2][3], HIGH);} else {digitalWrite(leds[2][3],LOW);}
  if(munit & 4) {digitalWrite(leds[1][3], HIGH);} else {digitalWrite(leds[1][3],LOW);}
  if(munit & 8) {digitalWrite(leds[0][3], HIGH);} else {digitalWrite(leds[0][3],LOW);}

  //minutes
  if(minuteTens & 1)  {digitalWrite(leds[3][2], HIGH);} else {digitalWrite(leds[3][2],LOW);}
  if(minuteTens & 2)  {digitalWrite(leds[2][2], HIGH);} else {digitalWrite(leds[2][2],LOW);}
  if(minuteTens & 4) {digitalWrite(leds[1][2], HIGH);} else {digitalWrite(leds[1][2],LOW);}

  //hour units
  if(hunit & 1) {digitalWrite(leds[3][1], HIGH);} else {digitalWrite(leds[3][1],LOW);}
  if(hunit & 2) {digitalWrite(leds[2][1], HIGH);} else {digitalWrite(leds[2][1],LOW);}
  if(hunit & 4) {digitalWrite(leds[1][1], HIGH);} else {digitalWrite(leds[1][1],LOW);}
  if(hunit & 8) {digitalWrite(leds[0][1], HIGH);} else {digitalWrite(leds[0][1],LOW);}

  //hour
  if(hourTens & 1)  {digitalWrite(leds[3][0], HIGH);} else {digitalWrite(leds[3][0],LOW);}
  if(hourTens & 2)  {digitalWrite(leds[2][0], HIGH);} else {digitalWrite(leds[2][0],LOW);}

   valm = digitalRead(A1);    // add one minute when pressed
   if(valm== HIGH) {
     minute++;
     if (minute >=59) {
      minute = 0;
     }
     second=0;
     rtc.setTime(hour, minute, second);
     delay(100);
  }

  valh = digitalRead(A2);    // add one hour when pressed
   if(valh==HIGH) {
   hour++;
   if (hour>=24) {
    hour = 0;
   }
   second=0;
   rtc.setTime(hour, minute, second);
   delay(100);
  }
  delay(50);
}


The two buttons i have are these:

So am i not going to need a 10K Ohm resister to each button?

I am really sorry if i sound so dumb at this.

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

don't you want the value to reach 59?

look this over.
buttons wired between pin and ground

const byte PinMin = A1;
const byte PinHr  = A2;

      byte butMinState;
      byte butHrState;

int  mins = 55;
int  hrs  = 20;

// -----------------------------------------------------------------------------
void
loop ()
{
    byte but = digitalRead (PinMin);
    if (butMinState != but)  {
        butMinState = but;
        delay (20);         // debounce

        if (LOW == but)  {
            if (60 <= ++mins)
                mins = 0;
            Serial.print   (" mins ");
            Serial.println (mins);
        }
    }

    but = digitalRead (PinHr);
    if (butHrState != but)  {
        butHrState = but;
        delay (20);         // debounce

        if (LOW == but)  {
            if (24 <= ++hrs)
                hrs = 0;
            Serial.print   (" hrs ");
            Serial.println (hrs);
        }
    }
}


void
setup ()
{
    Serial.begin (9600);

    pinMode (PinMin, INPUT_PULLUP);
    butMinState = digitalRead (PinMin);

    pinMode (PinHr, INPUT_PULLUP);
    butHrState = digitalRead (PinHr);
}

Here is a picture that shows it

The button you are using is not just a simple button.
It is a complete circuitry that includes a resistor

Do you have a breadboard. Something like this

You seem to be a beginner about electronics. Everybody here started as a
"I know nothing about electronics"

So it is just a matter of time and effort to gain knowledge about electronics

Do you have a digital multimeter?

I just wanted to say thanks for all your help on this.

When i realised the buttons had resistors on, i connected the cables to the right arduino input and then programmed the lights to the correct button press it worked.

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