Let me preface this by saying I'm a beginner, and I know my code must have some logic errors. I need some fresh eyes on this, I have been messing with it a few days and am just running in circles at this point.

I'm trying to use buttons and button states to set hours and minutes of an alarm clock so I am trying to save the state of the buttons so after being pushed so it will remember the state rather than being changed on release. I am trying to get it so pushing button A in my code goes into the alarm clock set procedure, then until button C is pressed it will loop to set hours then minutes (I havent added minutes procedure yet) So button A starts procedure and button C completes it, while button B increases the hours/minutes variable by one.

I have thoroughly confused myself. I have two "lastButtonState" variables which may be unnecessary but it was the only thing I could think of.

Right now with my code it goes into the button C while loop but doesn't come out of it on button press. My logic for that was that the while loop should exit when button C is pressed, so I don't think I need anything within the loop to break out of it.

I have the button B setting hours part working, it just isn't exiting the button C loop.

I'm eventually going to put this code in its respective function and add in the set minutes area after set hours area.

Here is my code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
//const int buttonPin = 9;
const int buttonApin = 3;
const int buttonBpin = 2;
const int buttonCpin = 5;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
//int flag = 0;
int alarmHoursInner = 1;
int lastButtonState = 0;
int lastButtonState3 = 0;
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  //Input or output?
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);
  pinMode(buttonCpin, INPUT_PULLUP);

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(buttonApin);
  buttonState2 = digitalRead(buttonBpin);
  buttonState3 = digitalRead(buttonCpin);

  if (buttonState == HIGH) {
    while (buttonState3 == LOW) {

      buttonState2 = digitalRead(buttonBpin);
      if (lastButtonState != buttonState2) { // last button state starts at 0 (or OFF) this line tests current state against last state
        if (buttonState2 == LOW) { // if buttonState is HIGH it went from OFF to ON
          // Serial.print("Button B works!");
          ++alarmHoursInner; // increments variable for hours
          Serial.print(alarmHoursInner); // prints hours
          Serial.println("  "); // blank line
          delay(50); // delay
        else {

          Serial.print("Please push button B");
          Serial.println("  ");

      lastButtonState = buttonState2;
      lastButtonState3 = buttonState3;
  • 2
    start by renaming the buttons to some useful names that describe the function.... instead of buttonA, buttonB, buttonC call them something like mode, advance, exit .... modeButton, etc.
    – jsotola
    Aug 11, 2018 at 17:19
  • research Arduino state machine
    – jsotola
    Aug 11, 2018 at 17:20
  • Ok your input about renaming is noted, thanks! I have already looked at state changes pretty thoroughly. Could you point me in the direction of something specific about state machines that would help? I googled it and looked at a few links but what part of state machines should I focus on? So far, It looks like its what I have already known about states when I developed my code. What exactly is wrong with my code? Aug 11, 2018 at 19:19
  • lastButtonState3 = buttonState3; .... this line is indented too far, which makes it look like it is part of the if statement .... it is at the end of the while loop ..... buttonState3 will always be LOW when this line executes
    – jsotola
    Aug 11, 2018 at 23:23


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.