The code doesn't work

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();
}```

Encoder pins and CLK & DT uses pins 2 & 3

Are this the same pins?

By the way, what's the Arduino board you use? The code looks too long and uses too much memory for Uno/Nano...
What memory usage figures does IDE show at the end of compilation?

Why you need to add "some Serial.begin's"? You need to have it once in the setup(), what do you need to do exactly?

search and read about the Arduino F-Makro and use the F Makro for all your fix texts to save SRAM.

Serial.print(F("co2Val = "));

btw: if you don't have a Serial.begin in your setup, Serial output will not work.

In general you should add one device after another. Make one after the other working and add more and more things.

Yes it is. IDE shows 17 kB

to check does code works (and just showing nothing) or interrupts somewhere

Did you means Serial.print() rather than Serial.begin ?

Please show the memory usage message as it outputs from IDE.

My bad, I meant Serial.print().

Arduino UNO

Your setup should begin like this

void setup() {
  Serial.begin(9600);
  while(1Serial); // wait for serial to be really ready
  Serial.println("Setup-Start";
  Wire.begin();
  Serial.println("Wire.begin() done";
  lcd.init();
  Serial.println("lcd.init() done";
  lcd.backlight();
  Serial.println("lcd.backlight() done";
  //ccs811.begin();
  bme.begin();
  Serial.println("bme.begin() done";
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  Serial.println("rtc.adjust done";
  // add code that print sthe time to the serial monitor
  pinMode(A0, INPUT);
  pinMode(CLK,INPUT_PULLUP);
  pinMode(DT,INPUT_PULLUP);

you use IO-pin 2 and 3 for two different things:

const byte encoderPinA = 2;
const byte encoderPinB = 3;

  attachInterrupt(digitalPinToInterrupt(encoderPinA), isrA, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoderPinB), isrB, CHANGE);

int DT=2;
int CLK=3;

  pinMode(CLK,INPUT_PULLUP);
  pinMode(DT,INPUT_PULLUP);

this can not work!

best regards Stefan

Hi, @antonchernyavskiy
Welcome to the forum.

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Did you write your code in stages?
You seem to have a lot of things going on in your code, did you write separate individual code for each thing?
Get each thing working first, individually, before adding them together one at a time, each time debugging before adding the next.

You should have separate code for each function and they should work, before making your master code.

This method helps to prevent what you have here, which is all your functions but none work, and you will have more than one problem to debug.

Can I suggest you forget your big code for the moment and write separate codes for each function. I will make things easier, less complicated and prove your codes in stages.

Tom... :grinning: :+1: :coffee: :australia:

Whew, ok, now it makes more sense. :wink:
In the code you shown us you already have many "Serial.print()" around, so I don't understand what you mean when you say (I changed "begin" with "print" to be clear):

When I add some Serial.print's() program stucks either completing with that Serial.print's() or before them

I had a look at your code and first of all IMHO is impossible it has been made by a "newbie": if you are what you said, I must suppose this is kinda "frankenstein" code, e.g. made by collecting and mixing various examples found in the net. But this very often leads to a lot of some confused coding with some hard to understand issues. Even for experienced programmers, because unless one is able to locally reproduce the problem it's kinda hard to track it down and give solutions or any advice. And it becomes harder if the description of the problem contains just generic information (like just saying"program stucks").

Anyway, does the code you posted here already show that behaviour, or you mean it happens after adding some more Serial.print() somewhere (what? where?)?

Could you show us both a code sample that works correctly and the same but with the changes that meke it stuck (together with a copy/paste of the serial monitor around the event)?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.