Switch case delay problem

Hi! Can you please help me with my code? I am creating simple weather station and I have 20x4 liquid crystal LCD display where I created pages that are changing, but I have no idea how to increase time between the pages. I was trying with delay but it broke my program and when I tried millis() it done nothing. I am new to arduino so any help appreciated. Thanks..

This is my program for switching case:

// LCD pages
  switch(strana){
   case 1:
   lcd.clear();
// Reading pressure      
   float pressure = bmp280.readPressure();
   sprintf(text, "%u.%02u hPa ", (int)(pressure/100), (int)((uint32_t)pressure % 100));
   lcd.setCursor(0, 1);
   lcd.print("Pressure:");
   lcd.setCursor(9,1);
   lcd.print(text);
// Reading altitude
   float altitude_   = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast
   sprintf(text, "%u.%02u m ", (int)altitude_, (int)(altitude_ * 100)%100, 223);
   lcd.setCursor(0,0);
   lcd.print("Altitude:");
   lcd.setCursor(10,0);
   lcd.print(text);
 
   lcd.setCursor(0,3);
   lcd.print("Date:");
   lcd.setCursor(6,3);
   lcd.print(String(displayday)+"/"+String(displaymonth)+"/"+String(displayyear));

   lcd.setCursor(0,2);
   lcd.print("Mp:");
   lcd.setCursor(4,2);
   lcd.print(moonphase);
   if(millis() >= time_now + period){
        time_now += period;
        strana = 1 ;
        break;
    }
  
   
   case 2:
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Temp.In:");
   lcd.setCursor(8,0);
   lcd.print(tem1);
   lcd.setCursor(14,0);
   lcd.print((char)223 );
   lcd.setCursor(15,0);
   lcd.print("C" );

   lcd.setCursor(0,1);
   lcd.print("Humid.In:");
   lcd.setCursor(9,1);
   lcd.print(hum1);
   lcd.setCursor(13,1);
   lcd.print("%");

   lcd.setCursor(0,2);
   lcd.print("Temp.Out:");
   lcd.setCursor(9,2);
   lcd.print(tem2);
   lcd.setCursor(15,2);
   lcd.print((char)223 );
   lcd.setCursor(16,2);
   lcd.print("C" );

   lcd.setCursor(0,3);
   lcd.print("Humid.Out:");
   lcd.setCursor(10,3);
   lcd.print(hum2);
   lcd.setCursor(16,3);
   lcd.print("%");   
   if(millis() >= time_now + period){
        time_now += period;
        strana = 2 ;
        break;
   
    }
   
    Serial.print("Approx Altitude = ");
    Serial.print(altitude_);
    Serial.println(" m");
    Serial.println();
}}

There is whole code:

//--------------------------------[ Libraries ]---------------------------------->
#include <Adafruit_BMP280.h>
#include "Wire.h" 
#include "LCD.h" 
#include "LiquidCrystal_I2C.h" 
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DS3231.h>
#include "RTClib.h"
#include <TM1637Display.h>

//--------------------------------[ Variables ]---------------------------------->
#define DHTPIN1 13
#define DHTTYPE1 DHT22

#define DHTPIN2 12
#define DHTTYPE2 DHT11

#define CLK 10
#define DIO 11

char text[14]; 
String moonphase ; 

int cas = 0;
int teplota = 0;
int tlak = 0;
int vlhkost = 0;
int strana = 1 ;
int period = 5000;
unsigned long time_now = 0;


byte count = 0;

boolean PAGE1 = false;
boolean PAGE2 = false;

// Display I2C adress
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);

// Define device I2C address: 0x76 or 0x77
#define BMP280_I2C_ADDRESS  0x76  

Adafruit_BMP280 bmp280;

//--------------------------------[ Initializing sensors ]---------------------------------->

// Initialize DHT sensor
DHT dht1 = DHT(DHTPIN1, DHTTYPE1);
DHT dht2 = DHT(DHTPIN2, DHTTYPE2);

// Create rtc and display object
RTC_DS3231 rtc;
TM1637Display display = TM1637Display(CLK, DIO);

//--------------------------------[ Void setup ]---------------------------------->
void setup(){
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
Serial.begin(9600);

// Initializing RTC
rtc.begin();

// Setting up what time is on display
if (rtc.lostPower()) {
   Serial.println("RTC lost power, lets set the time!");
// Following line sets the RTC to the date & time this sketch was compiled:
   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Set the TM1637 display brightness (0-7):
   display.setBrightness(7);
// Clear the display:
   display.clear();
  
// Check if all components are working correctly 
   Serial.println(F("Arduino + BMP280"));
  
   if (!bmp280.begin(BMP280_I2C_ADDRESS))
   {  
   Serial.println("Could not find a valid BMP280 sensor, check wiring!");
   while (1);
   }

// Initializing DHT sensors
   dht1.begin();
   dht2.begin();
  
// Set off LCD module
   lcd.begin (20,4);
   lcd.setBacklightPin(3,POSITIVE);
   lcd.setBacklight(HIGH);
// Writing "Weather Station" onto the LCD
   lcd.setCursor(3,0);
   lcd.print("Weather Station");
}

//--------------------------------[void loop]--------------------------------->
void loop(){
// Getting time from real time clock
   DateTime now = rtc.now();

// Create time format to display
   int displaytime = (now.hour() * 100) + now.minute();
   int displayday = now.day();
   int displaymonth = now.month();
   int displayyear = now.year();
  
// Display the current time in 24 hour format 
   display.showNumberDecEx(displaytime, 0b11100000, true);

// Blinking center dots
   delay(1000);
// Prints displaytime without center colon
   display.showNumberDec(displaytime, true); 
    
   delay(1000);
   
// Read the humidity in %
   float hum1 = dht1.readHumidity();
   float hum2 = dht2.readHumidity();
// Read the temperature as Celsius
   float tem1 = dht1.readTemperature();
   float tem2 = dht2.readTemperature();
// Read the temperature as Fahrenheit
   float f = dht1.readTemperature(true);
    
// Check if any reads failed and exit early (to try again):
   if (isnan(hum1) || isnan(tem1) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
// Compute heat index in Fahrenheit (default)
   float hif = dht1.computeHeatIndex(f, hum1);
// Compute heat index in Celsius
   float hic = dht1.computeHeatIndex(tem1, hum1, false);

// Printing measured units onto the Serial Monitor
   Serial.print("Hum1: ");
   Serial.print(hum1);
   Serial.print(" Hum2: ");
   Serial.print(hum2);
   Serial.print(" % ");
   Serial.print("Tem1: ");
   Serial.print(tem1);
   Serial.print(" Tem2: ");
   Serial.print(tem2);
   Serial.print(" \xC2\xB0");
   Serial.print("C | ");
   Serial.print(f);
   Serial.print(" \xC2\xB0");
   Serial.print("F ");
   Serial.print("Heat index: ");
   Serial.print(hic);
   Serial.print(" \xC2\xB0");
   Serial.print("C | ");
   Serial.print(hif);
   Serial.print(" \xC2\xB0");
   Serial.println("F");


 // Moonphase - MP  
   int led[]={2,3,4,5,6,7,8,9} ;

   int Y = now.year();
   int M = now.month();
   int D = now.day();
   int A = Y/100;
   int B = A/4;
   int C = 2-A+B;
   float E = 365.25 * (Y+4716);
   float F = 30.6001 * (M+1) ;
   float JD = C+D+E+F-1524.5 ;
   float dsn = JD - 2451549.5;
   float nm = dsn / 29.53;
   float fnm = nm-long(nm);
   float Mooncyc = fnm*29.5;
  
   if(Mooncyc >= 0 && Mooncyc < 3.27777778 ){moonphase = ("New moon");
   for(int i = 0 ;i< 8; i++ ){if (i == 0 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   else if(Mooncyc >= 3.27777778 && Mooncyc < 6.55555556  ){moonphase = "Wax cresc";
   for(int i = 0 ;i< 8; i++ ){if (i == 1 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
  
   else if(Mooncyc >= 6.55555556  && Mooncyc < 9.83333334  ){moonphase = ("1st");
   for(int i = 0 ;i< 8; i++ ){if (i == 2 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   else if(Mooncyc  >= 9.83333334  && Mooncyc < 13.1111111  ){moonphase = ("Waxing gib");
   for(int i = 0 ;i< 8; i++ ){if (i == 3 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   else if(Mooncyc >= 13.1111111  && Mooncyc < 16.3888889  ){moonphase = ("Full");
   for(int i = 0 ;i< 8; i++ ){if (i == 4 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   else if(Mooncyc  >= 16.3888889  && Mooncyc < 19.6666667 ){moonphase = ("Waning gib");
   for(int i = 0 ;i< 8; i++ ){if (i == 5 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   else if(Mooncyc >= 19.6666667 && Mooncyc < 22.9444445 ){moonphase = ("3rd");
   for(int i = 0 ;i< 8; i++ ){if (i == 6 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   else if(Mooncyc >=22.9444445  && Mooncyc < 26.2222223 ){moonphase = "Waning cresc";
   for(int i = 0 ;i< 8; i++ ){if (i == 7 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   else if(Mooncyc >= 26.2222223  && Mooncyc < 29.5  ){moonphase = "Nov";
   for(int i = 0 ;i< 8; i++ ){if (i == 0 ){digitalWrite(led[i],HIGH);}
   else{digitalWrite(led[i],LOW);}
   }}
   
// LCD pages
  switch(strana){
   case 1:
   lcd.clear();
// Reading pressure      
   float pressure = bmp280.readPressure();
   sprintf(text, "%u.%02u hPa ", (int)(pressure/100), (int)((uint32_t)pressure % 100));
   lcd.setCursor(0, 1);
   lcd.print("Pressure:");
   lcd.setCursor(9,1);
   lcd.print(text);
// Reading altitude
   float altitude_   = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast
   sprintf(text, "%u.%02u m ", (int)altitude_, (int)(altitude_ * 100)%100, 223);
   lcd.setCursor(0,0);
   lcd.print("Altitude:");
   lcd.setCursor(10,0);
   lcd.print(text);
 
   lcd.setCursor(0,3);
   lcd.print("Date:");
   lcd.setCursor(6,3);
   lcd.print(String(displayday)+"/"+String(displaymonth)+"/"+String(displayyear));

   lcd.setCursor(0,2);
   lcd.print("Mp:");
   lcd.setCursor(4,2);
   lcd.print(moonphase);
   if(millis() >= time_now + period){
        time_now += period;
        strana = 1 ;
        break;
    }
  
   
   case 2:
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Temp.In:");
   lcd.setCursor(8,0);
   lcd.print(tem1);
   lcd.setCursor(14,0);
   lcd.print((char)223 );
   lcd.setCursor(15,0);
   lcd.print("C" );

   lcd.setCursor(0,1);
   lcd.print("Humid.In:");
   lcd.setCursor(9,1);
   lcd.print(hum1);
   lcd.setCursor(13,1);
   lcd.print("%");

   lcd.setCursor(0,2);
   lcd.print("Temp.Out:");
   lcd.setCursor(9,2);
   lcd.print(tem2);
   lcd.setCursor(15,2);
   lcd.print((char)223 );
   lcd.setCursor(16,2);
   lcd.print("C" );

   lcd.setCursor(0,3);
   lcd.print("Humid.Out:");
   lcd.setCursor(10,3);
   lcd.print(hum2);
   lcd.setCursor(16,3);
   lcd.print("%");   
   if(millis() >= time_now + period){
        time_now += period;
        strana = 2 ;
        break;
   
    }
   
    Serial.print("Approx Altitude = ");
    Serial.print(altitude_);
    Serial.println(" m");
    Serial.println();
}}

3

And there is scheme:

is this not overkill?

on picture is power for RTC&LCD not present

I forgot to connect it in the scheme thank you for pointing it out, and I was trying multiple libraries and many of them weren't working I will try to fix it.

  switch (strana) {
    case 1:
...
        strana = 1 ;
        break;
    case 2:
...
        strana = 2 ;
        break;

why you overwrite with same number?
and case is erroneous written

  switch (strana) {
    case 1:
...
      if (millis() >= time_now + period) {
        time_now += period;
        strana = 1 ;
        **break;**
      **}**

change to

      if (millis() - time_now >= period) {
        time_now += period;
        //    here     print      your   needed    time
      }
        break;

not error but it writes every loop() cycle

    for (int i = 0 ; i < 8; i++ ) {
      if (i == 0 ) {
        digitalWrite(led[i], HIGH);
      }
      else {
        digitalWrite(led[i], LOW);
      }
    }

but needed is only

        digitalWrite(led[0], HIGH); // Moonpase now
        digitalWrite(led[7], LOW);  // Moonpase before
1 Like

You should be very cautious declaring variables inside a switch statement... this can cause unexpected behaviour. It has to do with the scope of the variables - you will get compiler warnings about this, if you have them turned on.

Either declare outside of the switch (as below)

float pressure  = 0.0
float altitude_   = 0.0;  

switch(strana){

Or enclose the contents of each case within {};

1 Like
//--------------------------------[ Libraries ]---------------------------------->
#include <Adafruit_BMP280.h>
#include "Wire.h"
//#include "LCD.h"
#include "LiquidCrystal_I2C.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DS3231.h>
//#include "RTClib.h"
#include <TM1637Display.h>

//--------------------------------[ Variables ]---------------------------------->
#define DHTPIN1 13
#define DHTTYPE1 DHT22

#define DHTPIN2 12
#define DHTTYPE2 DHT11

#define CLK 10
#define DIO 11

char text[14];
String moonphase ;

int cas = 0;
int teplota = 0;
int tlak = 0;
int vlhkost = 0;
int strana = 1 ;
int period = 5000;
unsigned long time_now = 0;

byte count = 0;

boolean PAGE1 = false;
boolean PAGE2 = false;

// Display I2C adress
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);

// Define device I2C address: 0x76 or 0x77
#define BMP280_I2C_ADDRESS  0x76

Adafruit_BMP280 bmp280;

//--------------------------------[ Initializing sensors ]---------------------------------->

// Initialize DHT sensor
DHT dht1 = DHT(DHTPIN1, DHTTYPE1);
DHT dht2 = DHT(DHTPIN2, DHTTYPE2);

// Create rtc and display object
RTC_DS3231 rtc;
TM1637Display display = TM1637Display(CLK, DIO);
// Moonphase - MP
int led[] = {2, 3, 4, 5, 6, 7, 8, 9} ;

//--------------------------------[ Void setup ]---------------------------------->
void setup() {
  for (byte i = 0; i < 8; i++) pinMode(i, OUTPUT);
  Serial.begin(9600);

  rtc.begin();
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // Following line sets the RTC to the date & time this sketch was compiled:
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  display.setBrightness(7);
  display.clear();

  // Check if all components are working correctly
  Serial.println(F("Arduino + BMP280"));

  if (!bmp280.begin(BMP280_I2C_ADDRESS)) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }

  dht1.begin();
  dht2.begin();

  lcd.begin (20, 4);   //lcd.init() ?
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);

  lcd.setCursor(3, 0);
  lcd.print("Weather Station");
}

//--------------------------------[void loop]--------------------------------->
void loop() {
  // Getting time from real time clock
  DateTime now = rtc.now();

  // Create time format to display
  int displaytime = (now.hour() * 100) + now.minute();
  int displayday = now.day();
  int displaymonth = now.month();
  int displayyear = now.year();

  display.showNumberDecEx(displaytime, 0b11100000, true);

  // Blinking center dots
  delay(1000);
  // Prints displaytime without center colon
  display.showNumberDec(displaytime, true);

  delay(1000);

  // Read the humidity in %
  float hum1 = dht1.readHumidity();
  float hum2 = dht2.readHumidity();
  // Read the temperature as Celsius
  float tem1 = dht1.readTemperature();
  float tem2 = dht2.readTemperature();
  // Read the temperature as Fahrenheit
  float f = dht1.readTemperature(true);

  // Check if any reads failed and exit early (to try again):
  if (isnan(hum1) || isnan(tem1) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // Compute heat index in Fahrenheit (default)
  float hif = dht1.computeHeatIndex(f, hum1);
  // Compute heat index in Celsius
  float hic = dht1.computeHeatIndex(tem1, hum1, false);

  // Printing measured units onto the Serial Monitor
  Serial.print("Hum1: ");
  Serial.print(hum1);
  Serial.print(" Hum2: ");
  Serial.print(hum2);
  Serial.print(" % ");
  Serial.print("Tem1: ");
  Serial.print(tem1);
  Serial.print(" Tem2: ");
  Serial.print(tem2);
  Serial.print(" \xC2\xB0");
  Serial.print("C | ");
  Serial.print(f);
  Serial.print(" \xC2\xB0");
  Serial.print("F ");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" \xC2\xB0");
  Serial.print("C | ");
  Serial.print(hif);
  Serial.print(" \xC2\xB0");
  Serial.println("F");

  int Y = now.year();
  int M = now.month();
  int D = now.day();
  int A = Y / 100;
  int B = A / 4;
  int C = 2 - A + B;
  float E = 365.25 * (Y + 4716);
  float F = 30.6001 * (M + 1) ;
  float JD = C + D + E + F - 1524.5 ;
  float dsn = JD - 2451549.5;
  float nm = dsn / 29.53;
  float fnm = nm - long(nm);
  float Mooncyc = fnm * 29.5;

  if ((Mooncyc >= 0 && Mooncyc < 3.27777778) || (Mooncyc >= 26.2222223  && Mooncyc < 29.5  )) {
    moonphase = ("New moon");
    digitalWrite(led[0], HIGH);
    digitalWrite(led[7], LOW);
  }
  else if (Mooncyc >= 3.27777778 && Mooncyc < 6.55555556  ) {
    moonphase = "Wax cresc";
    digitalWrite(led[1], HIGH);
    digitalWrite(led[0], LOW);
  }
  else if (Mooncyc >= 6.55555556  && Mooncyc < 9.83333334  ) {
    moonphase = ("1st");
    digitalWrite(led[2], HIGH);
    digitalWrite(led[1], LOW);
  }
  else if (Mooncyc  >= 9.83333334  && Mooncyc < 13.1111111  ) {
    moonphase = ("Waxing gib");
    digitalWrite(led[3], HIGH);
    digitalWrite(led[2], LOW);
  }
  else if (Mooncyc >= 13.1111111  && Mooncyc < 16.3888889  ) {
    moonphase = ("Full");
    digitalWrite(led[4], HIGH);
    digitalWrite(led[3], LOW);
  }
  else if (Mooncyc  >= 16.3888889  && Mooncyc < 19.6666667 ) {
    moonphase = ("Waning gib");
    digitalWrite(led[5], HIGH);
    digitalWrite(led[4], LOW);
  }
  else if (Mooncyc >= 19.6666667 && Mooncyc < 22.9444445 ) {
    moonphase = ("3rd");
    digitalWrite(led[6], HIGH);
    digitalWrite(led[5], LOW);
  }
  else if (Mooncyc >= 22.9444445  && Mooncyc < 26.2222223 ) {
    moonphase = "Waning cresc";
    digitalWrite(led[7], HIGH);
    digitalWrite(led[6], LOW);
  }

  // Reading pressure
  float pressure = bmp280.readPressure();
  // Reading altitude
  float ALTitude = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast
  Serial.print("Approx Altitude = ");
  Serial.print(ALTitude);
  Serial.println(" m");
  Serial.println();

  // LCD pages
  switch (strana) {
    case 1:
      lcd.clear();
      sprintf(text, "%u.%02u hPa ", (int)(pressure / 100), (int)((uint32_t)pressure % 100));
      lcd.setCursor(0, 1);
      lcd.print("Pressure:");
      lcd.setCursor(9, 1);
      lcd.print(text);
      sprintf(text, "%u.%02u m ", (int)ALTitude, (int)(ALTitude * 100) % 100, 223);
      lcd.setCursor(0, 0);
      lcd.print("Altitude:");
      lcd.setCursor(10, 0);
      lcd.print(text);

      lcd.setCursor(0, 3);
      lcd.print("Date:");
      lcd.setCursor(6, 3);
      lcd.print(displayday);
      lcd.print('/');
      lcd.print(displaymonth);
      lcd.print('/');
      lcd.print(displayyear);

      lcd.setCursor(0, 2);
      lcd.print("Mp: ");
      lcd.print(moonphase);
      if (millis() - time_now >= period) {
        time_now += period;
        strana = 2 ;
      }
      break;

    case 2:
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Temp.In:");
      lcd.setCursor(8, 0);
      lcd.print(tem1);
      lcd.setCursor(14, 0);
      lcd.print((char)223 );
      lcd.setCursor(15, 0);
      lcd.print("C" );

      lcd.setCursor(0, 1);
      lcd.print("Humid.In:");
      lcd.setCursor(9, 1);
      lcd.print(hum1);
      lcd.setCursor(13, 1);
      lcd.print("%");

      lcd.setCursor(0, 2);
      lcd.print("Temp.Out:");
      lcd.setCursor(9, 2);
      lcd.print(tem2);
      lcd.setCursor(15, 2);
      lcd.print((char)223 );
      lcd.setCursor(16, 2);
      lcd.print("C" );

      lcd.setCursor(0, 3);
      lcd.print("Humid.Out:");
      lcd.setCursor(10, 3);
      lcd.print(hum2);
      lcd.setCursor(16, 3);
      lcd.print("%");
      if (millis() - time_now >= period) {
        time_now += period;
        strana = 1;
      }
      break;
  }
}
1 Like

It's even worse. The UNO has no power...

1 Like

Thank you very much. Fast feedback very much appreciated.

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