Block of program after adding reading of inputs

Hello

I am new user of arduino and I have very strange problem.

I don't no why, but after adding very easy reading of inputs to my program, program is stopped. I can't find what can be problem. I am using arduino uno, tried arduino nano, tried virtual arduino on wokwi and everywhere was situation same.

my code is:

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <RTClib.h>
#include <Arduino.h>
#include <U8g2lib.h>


#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
RTC_DS3231 rtc;

//Constants
char daysOfTheWeek[7][12] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int btSet = 8;
const int btUp = 7;
const int btDown = 6;
const int buzzer = 9;

//Variables
int DD,MM,YY,H,M,S;
int btnCount = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis; 
int set;
int up;
int down;
String sDD;
String sMM;
String sYY;
String sH;
String sM;
String sS;
float T;
boolean setupScreen = false;
boolean backlightON = true;

void setup () {
  pinMode(btSet, INPUT_PULLUP);
  pinMode(btUp, INPUT_PULLUP);
  pinMode(btDown, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  Serial.begin(57600);
  u8g2.begin();

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  }

}

void loop () {
  //currentMillis = millis();
  //readBtns(); 
  getTimeDate();
  //if (!setupScreen){
  oledPrint();
  //}
  //else{
  //oledSetup();
  //} 

}

//void readBtns(){

//}



void getTimeDate(){
  if (!setupScreen){
    DateTime now = rtc.now();
    DD = now.day();
    MM = now.month();
    YY = now.year();
    H = now.hour();
    M = now.minute();
    S = now.second();
    T = rtc.getTemperature();
  }
  //Make some fixes...
  if (DD>100){ sDD = '0' + String(DD); } else { sDD = DD; }
  if (MM>100){ sMM = '0' + String(MM); } else { sMM = MM; }
  sYY=YY;
  if (H>100){ sH = '0' + String(H); } else { sH = H; }
  if (M>100){ sM = "0" + String(M); } else { sM = M; }
  if (S>100){ sS = "0" + String(S); } else { sS = S; }
  //  Serial.print(now.year(), DEC);
  //  Serial.print('/');
  //  Serial.print(now.month(), DEC);
  //  Serial.print('/');
  //  Serial.print(now.day(), DEC);
  //  Serial.print(" (");
  //  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  //  Serial.print(") ");
  //  Serial.print(now.hour(), DEC);
  //  Serial.print(':');
  //  Serial.print(now.minute(), DEC);
  //  Serial.print(':');
  //  Serial.print(now.second(), DEC);
  //  Serial.println();

  //  Serial.print("Temperature: ");
  //  Serial.print(rtc.getTemperature());
  //  Serial.println(" C");

  //  Serial.println();
  //  delay(500);
}

void oledPrint(){

  char Temp[1];
  char buff1[1];
  char buff2[1];
  char buff3[1];
  char buff4[1];

  u8g2.clearBuffer(); // clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tf);	// choose a suitable font
  u8g2.drawStr(0,10, "Cas - ");	// write something to the internal memory
  u8g2.drawStr(0,25, "Datum - ");	// write something to the internal memory 
  u8g2.drawStr(0,40, "Teplota:");	// write something to the internal memory
  u8g2.setCursor(30,10);
  sprintf(buff1, "%d :%d", H, M);
  u8g2.print(buff1);
  u8g2.setCursor(60,10);
  sprintf(buff2, " :%d", S);
  u8g2.print(buff2);
  u8g2.setCursor(45,25);
  sprintf(buff3, "%d :%d", DD, MM);
  u8g2.print(buff3);
  u8g2.setCursor(70,25);
  sprintf(buff4, " :%d", YY);
  u8g2.print(buff4);
  //u8g2.setCursor(50, 40);
  //dtostrf(T, 4, 2, Temp);
  //u8g2.print(Temp);
  u8g2.sendBuffer();					// transfer internal memory to the display
   delay(100);

  set = digitalRead(btSet); // ****WHEN I ADD THIS 3 LINES TO MY PROGRAM EVERYTHING IS BLOCKED
  up = digitalRead(btUp);
  down = digitalRead(btDown);
  //Turn backlight on/off by pressing the down button
  //if (digitalRead(btDown)==LOW){  //&& btnCount==0
  //  if (backlightON==true){
  //   u8g2.setPowerSave(1);
  //   backlightON = false;
  // }
  // else{
  //    u8g2.setPowerSave(0);
  //   backlightON = true;
  // }
  //  delay(500);
 // }
  
  if (set==LOW || up==LOW || down==LOW) {  
    digitalWrite(buzzer, HIGH);
   }  
  else {
   digitalWrite(buzzer, LOW);
  }  
} 

wiring diagram looks like:

To facilitate our help, post your project link on WOKWI.

PS:

Show this add (lines numbers at code ).

You made room for one character here in buff1, and soon enough you are trying to put way more than one character in there. Same same buff2 &c.

Not to mention that character arrays shoukd be one larger than the number of printable characters in order to accommodate the null terminating character that is at the end.

Figure out how much stuff you wanna cram in there, then make the arrays a comfortable margin larger than that.

Anything can happen because of that error, and "it worked before" if it did was a matter of luck.

Can't say it's the only problem, but it is enough of one. Literally nothing would surprise me about your sketch's behaviour with that error in the code.

a7

thanks for quick answer. I tried extend buffer to 10 but didn't help. When i blocked part of program for sending text to display, my program was unblocked.
I blocked this:

void oledPrint(){

  char Temp[10];
  char buff1[10];
  char buff2[10];
  char buff3[10];
  char buff4[8];

  u8g2.clearBuffer(); // clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tf);	// choose a suitable font
  u8g2.drawStr(0,10, "Cas - ");	// write something to the internal memory
  u8g2.drawStr(0,25, "Datum - ");	// write something to the internal memory 
  u8g2.drawStr(0,40, "Teplota:");	// write something to the internal memory
//  u8g2.setCursor(30,10);
//  sprintf(buff1, "%d :%d", H, M);
//  u8g2.print(buff1);
//  u8g2.setCursor(60,10);
//  sprintf(buff2, " :%d", S);
//  u8g2.print(buff2);
//  u8g2.setCursor(45,25);
//  sprintf(buff3, "%d :%d", DD, MM);
//  u8g2.print(buff3);
//  u8g2.setCursor(70,25);
// sprintf(buff4, " :%d", YY);
//  u8g2.print(buff4);
//  u8g2.setCursor(50, 40);
//  dtostrf(T, 4, 2, Temp);
//  u8g2.print(Temp);
  u8g2.sendBuffer();					// transfer internal memory to the display
   delay(100);

Please, not image, but WOKWI link.

I think you have a wiring issue. Why do you have three buttons connected to a single wire going to three different pins?

Does VCC from the screen really connect to both Vin and 5V? That's a recipe for disaster.

Your program mentions a buzzer? Where is that in the schematic?

I am sending link to wokwi
https://wokwi.com/projects/386723632736905217

wiring is ok

I replaced buzzer with LED only due to the better simulation in wokwi, in reality I have buzzer.

No, it's lazy layout in the wokwi, see those wires separate as they enter the Arduino.

Maybe yes, maybe no. The wokwi is competent but not fully good at analog stuff. The KEDs never burn out, so we get in the habit of not bothering with the resistor, for example.

Nevertheless, it would be best to wire it like it was more real.

a7

But they all come together at the buttons. That's definitely not right. All three buttons are connected to all three pins at the same time. That can't be right.

As I wrote few posts above, wiring is ok, it was just vsualisation problem. you can find link to wokwi, I fixed visualisation there.

They might. But they could look like that if you were lazy when you drew out the wire.

Ask me how I know. :expressionless:

a7

No, it isn't. Or it isn't the same as what you've shown. If we're working from the wrong schematic then that's a problem isn't it? Do you want help with what you really have or with something thta doesn't match what you have?

So your schematic just isn't reality? So we're working on a fantasy? Why don't you show us the reality? Don't you think it would be easier to help if we could see the reality?

Then why doesn't OP give us an accurate drawing? Do they not care at all? Don't they want help with their problem?

Not if it looks like your picture it isn't.

Please check link to wokwi now, i just add colors for wires and moves these wires for better visaluzation. wokwi is created exactly as real project. buzzer or led is not important it is only digital output for signaling that button is pressed

Ok. Hopefully someone else helps you out. If you don't care to bother to put an accurate picture then I don't know why you think anyone should bother to help you.

I am sorry, but I don't understand what is problem right now. Here is new picture + link to wokwi, I changed nothing in wiring, I just change visaulization

https://wokwi.com/projects/386723632736905217

The problem is that you want people to help you with something but not let them see it. I don't want to have to go to Wowki to try to help you. There's absolutely no reason that you can't put a proper diagram here if you really want help.

If you're not capable of looking at that and understanding why it would be confusing, then why should anyone think that you'd ever be up to understanding a piece of code?

What you did with part of the code is called "commenting out", not blocking.
Doing that you simply threw this code out of the program. The fact that the program worked after this indicates that the error was precisely in this part, as @alto777 said

There is something going on in the commented out code, definitely probably (!). I wonder if the cursor gets placed off the display if the library handles that as it should gracefully.

At this point, I want to run the code and that will only be when the wokwi is linked, I haven't energy nor time to do it myself.

add: OIC it is linked. As soon as I am not moving, I take a look.

a7