Hello! I'm newbie. I have written my first more or less project, not everything is finished but the main idea that functions in loop don't work. When I add some Serial.begin's() program stucks either completing with that Serial.begin's() or before them. Tried to add them in setup() but didn't help. All hardware are ok. Asking for your advice. Thanks.
#include "SparkFunCCS811.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "RTClib.h"
#include <BfButton.h>
#include <LiquidCrystal_I2C.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define ONE_WIRE_BUS 5 // ДАТЧИК ПЕРЕПОДКЛЮЧИТЬ В СХЕМЕ
#define SEALEVELPRESSURE_HPA (1013.25)
#define CCS811_ADDR 0x5B
#define readA bitRead(PIND,2)//faster than digitalRead()
#define readB bitRead(PIND,3)//faster than digitalRead()
int btnPin=4;
int DT=2;
int CLK=3;
uint8_t LT[8] = {0b00111, 0b01111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
uint8_t UB[8] = {0b11111, 0b11111, 0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000};
uint8_t RT[8] = {0b11100, 0b11110, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111};
uint8_t LL[8] = {0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b01111, 0b00111};
uint8_t LB[8] = {0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111};
uint8_t LR[8] = {0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11111, 0b11110, 0b11100};
uint8_t UMB[8] = {0b11111, 0b11111, 0b11111, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111};
uint8_t LMB[8] = {0b11111, 0b00000, 0b00000, 0b00000, 0b00000, 0b11111, 0b11111, 0b11111};
CCS811 ccs811(CCS811_ADDR);
Adafruit_BME280 bme;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature oTemp(&oneWire);
RTC_DS3231 rtc;
BfButton btn(BfButton::STANDALONE_DIGITAL, btnPin, true, LOW);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int co2Val, inTemp, inHum, pres, light, outTemp;
int nYear, nMonth, nDay, nHour, nMin, nSec, oldMIN = nMin;
String nWDay;
volatile int counter = 0;
int aState;
int aLastState;
int prev_counter = 0;
int timeCount;
int activeScreen = 0;
bool state = 0;
const byte encoderPinA = 2;
const byte encoderPinB = 3;
int index;
int maxIndex;
String indexNames[6];
char daysOfTheWeek[7][12] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
/*void menuItems() {
index = 0;
maxIndex = 6;
indexNames = new String[maxIndex+1];
setupNames();
}*/
void setupNames() {
indexNames[0] = "hour:";
indexNames[1] = "min:";
indexNames[2] = "day:";
indexNames[3] = "month:";
indexNames[4] = "year:";
indexNames[5] = "delta:";
indexNames[6] = "Save";
}
void moveUp() {
index++;
if(index > maxIndex) index = 0;
}
void moveDown() {
index--;
if(index < 0) index = maxIndex;
}
int getIndex() {
return index;
}
void resIndex() {
index = 0;
}
void pressHandler (BfButton *btn, BfButton::press_pattern_t pattern) {
switch (pattern) {
case BfButton::SINGLE_PRESS:
Serial.println("Single push");
counter = 255;
Serial.println(counter);
break;
case BfButton::DOUBLE_PRESS:
Serial.println("Double push");
counter = 70;
Serial.println(counter);
break;
case BfButton::LONG_PRESS:
Serial.println("Long push");
counter = 0;
Serial.println(counter);
break;
}
}
void readData() {
co2Val = ccs811.getCO2();
inTemp = bme.readTemperature();
inHum = bme.readHumidity();
pres = (bme.readPressure()/100);
light = analogRead(A0);
oTemp.requestTemperatures();
outTemp = oTemp.getTempCByIndex(0);
Serial.print("co2Val = ");
Serial.println(co2Val);
Serial.print("inTemp = ");
Serial.println(inTemp);
Serial.print("inHum = ");
Serial.println(inHum);
Serial.print("pres = ");
Serial.println(pres);
Serial.print("light = ");
Serial.println(light);
Serial.print("outTemp = ");
Serial.println(outTemp);
}
void setTime() {
DateTime now = rtc.now();
nYear = now.year();
nMonth = now.month();
nDay = now.day();
nWDay = (daysOfTheWeek[now.dayOfTheWeek()]);
nHour = now.hour();
nMin = now.minute();
nSec = now.second();
}
void isrA() {
if(bitRead(PIND,3) != bitRead(PIND,2)) counter++;
else counter--;
}
void isrB() {
if(bitRead(PIND,2) == bitRead(PIND,3)) counter++;
else counter--;
}
void changeLight() {
if(state == 1) lcd.noBacklight();
else lcd.backlight();
state = !state;
}
void changeSettings(int i, int d = 0) {
}
void buttonFun() {
if(BfButton::LONG_PRESS && activeScreen != 3) changeLight();
if(counter > prev_counter && activeScreen != 3) {
activeScreen++;
if(activeScreen > 3) activeScreen = 0;
prev_counter = counter;
}
if(counter < prev_counter && activeScreen != 3) {
activeScreen--;
if(activeScreen < 0) {
activeScreen = 3;
} prev_counter = counter;
}
if(BfButton::SINGLE_PRESS && activeScreen == 3) {
moveUp();
prev_counter = counter;
}
if(counter < prev_counter && activeScreen == 3) {
/*int i = getIndex();
activeScreen = changeSettings(i, -1);
if(activeScreen != 3) {
rtc.hour = nHour;
rtc.min = nMin;
rtc.day = nDay;
rtc.mon = nMonth;
rtc.year = nYear;*/
prev_counter = counter;
}
if(counter > prev_counter && activeScreen == 3) {
prev_counter = counter;
}
}
void showScreen() {
switch(activeScreen) {
case 0:
mainScreen();
break;
case 1:
dataScreen();
break;
case 2:
screen2();
break;
case 3:
setupScreen();
break;
}
}
void clrscr() {
lcd.clear();
}
void setupScreen() {
}
void mainScreen() {
if(oldMIN != nMin) clrscr();
oldMIN = nMin;
showNumber(nHour, 0);
lcd.setCursor(6,0);
lcd.write(0xCD);
lcd.setCursor(6,1);
lcd.write(0xCD);
showNumber(nMin, 1);
lcd.setCursor(14,0);
if(nDay > 9) lcd.print(nDay);
else {
lcd.print("0");
lcd.print(nDay);
} lcd.print(".");
if(nMonth > 9) lcd.print(nMonth);
else {
lcd.print("0");
lcd.print(nMonth);
} lcd.setCursor(15,1);
lcd.print(nWDay);
lcd.setCursor(1,2);
lcd.print(inTemp);
lcd.write(0xB0);
lcd.setCursor(8,2);
lcd.print(inHum);
lcd.write("%");
lcd.setCursor(15,2);
lcd.print(outTemp);
lcd.print(0xB0);
lcd.setCursor(2,3);
lcd.print(pres);
lcd.print("hPa");
lcd.setCursor(10,3);
lcd.print(co2Val);
lcd.print("ppm");
}
void dataScreen() {
clrscr();
lcd.setCursor(1,0);
lcd.print(nHour);
lcd.print(":");
lcd.print(nMin);
lcd.print(" ");
lcd.print(nDay);
lcd.print(".");
lcd.print(nMonth);
lcd.print(".");
lcd.print((nYear%100));
lcd.print(".");
lcd.setCursor(0,1);
lcd.print("Analog 0: ");
lcd.print(light);
lcd.setCursor(0,2);
lcd.print("Relay status: ");
lcd.print(digitalRead(6));
}
void screen2() {
clrscr();
lcd.print("ヽ(•‿•)ノ");
}
void showNumber(int number, int pos) {
int hrs_dig_1 = number/10;
int hrs_dig_2 = number%10;
if(pos == 0) {
showDigit(hrs_dig_1, 0);
showDigit(hrs_dig_2, 1);
} else {
showDigit(hrs_dig_1, 2);
showDigit(hrs_dig_2, 3);
}
}
void showTime() {
int nHour = timeCount/3600;
int nMin = (timeCount%3600)/60;
showNumber(nHour, 0);
lcd.setCursor(6,0);
lcd.write(char(165));
lcd.setCursor(6,1);
lcd.write(char(165));
showNumber(nMin, 1);
}
void clockSymbols() {
lcd.createChar(0, LT);
lcd.createChar(1, UB);
lcd.createChar(2, RT);
lcd.createChar(3, LL);
lcd.createChar(4, LB);
lcd.createChar(5, LR);
lcd.createChar(6, UMB);
lcd.createChar(7, LMB);
}
void showDigit(int number, int pos) {
clockSymbols();
if(pos > 1) pos = pos*3+1;
else pos*=3;
switch(number) {
case 1:
lcd.setCursor(1+pos,0);
lcd.write(UB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(2+pos,1);
lcd.write(LL);
break;
case 2:
lcd.setCursor(0+pos,0);
lcd.write(UMB);
lcd.setCursor(1+pos,0);
lcd.write(UMB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(0+pos,1);
lcd.write(LL);
lcd.setCursor(1+pos,1);
lcd.write(LMB);
lcd.setCursor(2+pos,1);
lcd.write(LMB);
break;
case 3:
lcd.setCursor(0+pos,0);
lcd.write(UMB);
lcd.setCursor(1+pos,0);
lcd.write(UMB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(0+pos,1);
lcd.write(LMB);
lcd.setCursor(1+pos,1);
lcd.write(LMB);
lcd.setCursor(2+pos,1);
lcd.write(LR);
break;
case 4:
lcd.setCursor(0+pos,0);
lcd.write(LL);
lcd.setCursor(1+pos,0);
lcd.write(LB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(2+pos,1);
lcd.write(LR);
break;
case 5:
lcd.setCursor(0+pos,0);
lcd.write(LT);
lcd.setCursor(1+pos,0);
lcd.write(UMB);
lcd.setCursor(2+pos,0);
lcd.write(UMB);
lcd.setCursor(0+pos,1);
lcd.write(LMB);
lcd.setCursor(1+pos,1);
lcd.write(LMB);
lcd.setCursor(2+pos,1);
lcd.write(LR);
break;
case 6:
lcd.setCursor(0+pos,0);
lcd.write(LT);
lcd.setCursor(1+pos,0);
lcd.write(UMB);
lcd.setCursor(2+pos,0);
lcd.write(UMB);
lcd.setCursor(0+pos,1);
lcd.write(LL);
lcd.setCursor(1+pos,1);
lcd.write(LMB);
lcd.setCursor(2+pos,1);
lcd.write(LR);
break;
case 7:
lcd.setCursor(0+pos,0);
lcd.write(UB);
lcd.setCursor(1+pos,0);
lcd.write(UB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(1+pos,1);
lcd.write(LT);
break;
case 8:
lcd.setCursor(0+pos,0);
lcd.write(LT);
lcd.setCursor(1+pos,0);
lcd.write(UMB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(0+pos,1);
lcd.write(LL);
lcd.setCursor(1+pos,1);
lcd.write(LMB);
lcd.setCursor(2+pos,1);
lcd.write(LR);
break;
case 9:
lcd.setCursor(0+pos,0);
lcd.write(LT);
lcd.setCursor(1+pos,0);
lcd.write(UMB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(1+pos,1);
lcd.write(LB);
lcd.setCursor(2+pos,1);
lcd.write(LR);
break;
case 0:
lcd.setCursor(0+pos,0);
lcd.write(LT);
lcd.setCursor(1+pos,0);
lcd.write(UB);
lcd.setCursor(2+pos,0);
lcd.write(RT);
lcd.setCursor(0+pos,1);
lcd.write(LL);
lcd.setCursor(1+pos,1);
lcd.write(LB);
lcd.setCursor(2+pos,1);
lcd.write(LR);
break;
}
}
void setup() {
Wire.begin();
lcd.init();
lcd.backlight();
//ccs811.begin();
bme.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(CLK,INPUT_PULLUP);
pinMode(DT,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderPinA), isrA, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderPinB), isrB, CHANGE);
//Button settings
btn.onPress(pressHandler)
.onDoublePress(pressHandler) // default timeout
.onPressFor(pressHandler, 1000); // custom timeout for 1 second
}
void loop() {
setTime();
readData();
showScreen();
buttonFun();
readData();
}```