Library <Button.h> not working with my code

Hello!
I’m int the middle of a project trying to make an alarm clock. I wish to use buttons for purposes like setting alarm time and such. The problem is that the Button library is not working with my code, if I try the example that follows with the library it works perfect but when I try the same setup with my code my Arduino wont register me pressing the Button. Please help me and try to explain carefully, I am pretty new to Arduino :smiley:

I know it looks terrible but I am not nearly done, so please don’t be to hateful :stuck_out_tongue:
Here is my code:

#include <Button.h>
#include <RtcDS3231.h>
#include "SSD1306AsciiWire.h"
#include <Wire.h>
#include <Arduino.h>

Button button1(3);

RtcDS3231 rtc;

SSD1306AsciiWire oled;

byte timeHour;
byte timeMinute;
byte timeSecond;

byte wakeHour = 12;
byte wakeMinute = 34;

float temp;

char state = 'T';

char timeString[9];
char wakeString[6];

void setup() {


  button1.begin();
  
  Serial.begin(9600);
  Serial.println("Hi");

  Wire.begin();
  oled.begin(&Adafruit128x64, 0x3C);
}

void loop() {

  while ( state == 'T') {
    drawTime();
  }
  while (state == 'W') {
    drawWake();
  }

  if (button1.pressed())
    Serial.println("1");
}
void updateTime() {

  RtcDateTime now = rtc.GetDateTime();
  timeHour = now.Hour();
  timeMinute = now.Minute();
  timeSecond = now.Second();
}

void drawTime() {

  updateTime();
  updateTemp();

  oled.setFont(lcdnums14x24);
  oled.setRow(1);
  oled.setCol(8);

  sprintf(timeString, "%02d:%02d:%02d", timeHour, timeMinute, timeSecond);
  oled.println(timeString);

  oled.setFont(lcdnums12x16);
  oled.setRow(6);
  oled.setCol(0);

  sprintf(wakeString, "%02d:%02d", wakeHour, wakeMinute);
  oled.print(wakeString);

  oled.setFont(lcdnums12x16);
  oled.setRow(6);
  oled.setCol(102);
  oled.print(int(temp));


}
void drawWake(void) {

  updateTime();
  updateTemp();

  oled.setFont(lcdnums14x24);
  oled.setRow(1);
  oled.setCol(8);

  sprintf(timeString, "%02d:%02d:%02d", timeHour, timeMinute, timeSecond);
  oled.println(timeString);

  oled.setFont(lcdnums12x16);
  oled.setRow(6);
  oled.setCol(0);

  sprintf(wakeString, "%02d:%02d", wakeHour, wakeMinute);
  oled.print(wakeString);

  /*oled.setFont(lcdnums12x16);
    oled.setRow(6);
    oled.setCol(102);
    oled.print(int(temp));
  */
}
void updateTemp() {
  RtcTemperature rtcTemp = rtc.GetTemperature();
  temp = rtcTemp.AsFloat();
}







/*oled.begin(&Adafruit128x64, 0x3C);
  oled.clear();
  oled.setFont(fixed_bold10x15);
  oled.setRow(2);
  oled.setCol(24);
  oled.print("12:34:56");
  delay(100);
*/

And here is the code for the Button example that works:

#include <Button.h>

Button button1(2); // Connect your button between pin 2 and GND
Button button2(3); // Connect your button between pin 3 and GND
Button button3(4); // Connect your button between pin 4 and GND

void setup() {
	button1.begin();
	button2.begin();
	button3.begin();
	
	while (!Serial) { }; // for Leos
	Serial.begin(9600);
}

void loop() {
	if (button1.pressed())
		Serial.println("Button 1 pressed");
	
	if (button2.released())
		Serial.println("Button 2 released");
	
	if (button3.toggled()) {
		if (button3.read() == Button::PRESSED)
			Serial.println("Button 3 has been pressed");
		else
			Serial.println("Button 3 has been released");
	}
}

'state' is never sampled/changed within 'loop' so ...

while ( state == 'T' )
{
	drawTime();
}

... has no way of ending!

lloyddean:
'state' is never sampled/changed within 'loop' so ...

while ( state == 'T' )

{
drawTime();
}




... has no way of ending!

Please explain in more detail!

while (state == "T") ..

-> this triggers the sketch to execute the drawTime() function

Within the drawTime() function there is no reading of any external event, such as the press of the button, so state remains in "T" mode and the while loop will never end ...

You need to have a button read within the while loop to do something, or forget about the while loop and go with switch/case or if/then/else.

rpt007:
while (state == “T”) …

→ this triggers the sketch to execute the drawTime() function

Within the drawTime() function there is no reading of any external event, such as the press of the button, so state remains in “T” mode and the while loop will never end …

You need to have a button read within the while loop to do something, or forget about the while loop and go with switch/case or if/then/else.

Okay, but I also tried an if-else but that just skipped the if and went to the else, even if the if was true. Is this the same thing? I want to drawTime until I press the button wich should switch to drawing drawWake until I press it again!

This just skips the if statement even if it is true and state == ‘T’ and it goes straight to else!

  if ( state == 'T') {
    drawTime();
  } else {
    drawWake();
  }

Use Serial.print statements while debugging and you will get closer where the bugs are.

rpt007:
Use Serial.print statements while debugging and you will get closer where the bugs are.

Okay, thanks for the heads up! Can you recommend any methods/tips on how to debug with Serial Monitor?

Generally you should put Serial.print statements printing the current value of a variable which is due to trigger an event (start a function, leaving a while loop, reading a button etc.).

Whenever you think your code should react on a certain value/value change, print the vaule’s content on the serial monitor and be astonished, as in buggy situations you will read different values as you thought you have programmed.

Be aware that a sketch always does what you have instructed/programmed it to do and not what your intention was :slight_smile:

rpt007:
Generally you should put Serial.print statements printing the current value of a variable which is due to trigger an event (start a function, leaving a while loop, reading a button etc.).

Whenever you think your code should react on a certain value/value change, print the vaule’s content on the serial monitor and be astonished, as in buggy situations you will read different values as you thought you have programmed.

Be aware that a sketch always does what you have instructed/programmed it to do and not what your intention was :slight_smile:

Thank you very much for the tips! I will try to debug my sketch and if I can’t solve the problem I will write here again!

Thank you!