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: