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 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

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

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

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

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

thank you very much, I don't know why are some people so offensive, I am not professional in arduino programming. I am first time here on forum and try do the best from my side. I am finishing work in office, I'll be out of computer maybe one hour, but, I try to reply to you ASAP.

Thank you for try to help me. Anyway, I am not lazy. I am working first time with this forum, arduino and wokwi. I think it is not very constructive if you are som offensive for new people here. I am not teenager, I was just in the work and can't reply immediately to your requests.

Just role with it. Offensive people can only offend you if you choose to let them. On world wide public fora, you takes your chances, but your children and pets still love you IRL. I hope.

Sometimes, it's just the proverbial straw that broke the camel's back. Something that might go by without a whisper of a complaint will be the Nth such thing over the first cup of coffee and unleash a reaction what seems out of proportion.

a7

@robertrebor
I don't have this display, and I don't have experience with the commands in this library, but in the test with your code @ WOKWI I ran, line 136 seems to be the cause of the crash.

u8g2.drawStr(0,40, "Teplota:"); // write something to the internal memory

If I comment this line, everything runs, if I remove the comment, it crashes.
I don't know what this line does, but it seems like there is something wrong with it.

Do you know what this line should do and what its syntax is?

PS:
I have changed the value 40 to 30 and run ok.

So… maybe the library is t so careful if you supply coordinates that are off the screen.

Uncritical use of such would mean scribbling in memory not owned by the display.

a7

What you exactly changed? "u8g2.drawStr(0,40, "Teplota:")" to "u8g2.drawStr(0,30, "Teplota:")"?

i tried change it, but still same situation

can you post whole modified code?

Unrelated to your current problem but
If your rtc.begin fails you will be stuck in an infinite while(1) loop

Get rid of the while(1)