Combine 2 code together in a display

I try to combine 3 code but i still stuck with trying to combine this 2 code first, i wanna try to display this counter and temperature together. it works just fine when i try to run each the code only, but when i try to combine it, there's no output display on my OLED

Here is my code:

/*
 * How to use 12C Oled
 * Display Oled
 * GND --> GND
 * VCC --> 5V
 * SDA --> A5
 * SLC --> A4
 
  //* How to use the DHT-22 sensor with Arduino
//   Temperature sensor
 //  GND --> GND
 //  VCC --> 3.3V
 //  DAT -->D7

 * How to use Air Flow
 * Display Oled
 * OUT --> 2
 * GND --> GND
 * VCC --> 5V

 * How to Button
 * Count and Press
 * CLK --> 2
 * DT --> 3
 * SW --> 4
 * + --> 3V3
 * GND --> GND
 */


#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>

#include <DHT.h>;

//Constants
#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

#include <ezButton.h>  // the library to use for SW pin

#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4

#define DIRECTION_CW 0   // clockwise direction
#define DIRECTION_CCW 1  // counter-clockwise direction

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;

ezButton button(SW_PIN);  // create ezButton object that attach to pin 4

//Variables
int t; //Stores temperature value

void setup() 
{
  u8g2.begin();
  dht.begin();
  
  // configure encoder pins as inputs
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);
  button.setDebounceTime(50);  // set debounce time to 50 milliseconds

  // read the initial state of the rotary encoder's CLK pin
  prev_CLK_state = digitalRead(CLK_PIN);
}

void loop() {
  button.loop();  // MUST call the loop() function first

  // read the current state of the rotary encoder's CLK pin
  CLK_state = digitalRead(CLK_PIN);

  //Read data and store it to variables t (temperature)
  // Reading temperature takes about 250 milliseconds!
  t = dht.readTemperature();

  // If the state of CLK is changed, then pulse occurred
  // React to only the rising edge (from LOW to HIGH) to avoid double count
  if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
     // if the DT state is HIGH
    // the encoder is rotating in counter-clockwise direction => decrease the counter
    if (digitalRead(DT_PIN) == HIGH) {
      counter--;      
      direction = DIRECTION_CCW;

      if (counter <0)
      counter = 0;
      
    } else {
      // the encoder is rotating in clockwise direction => increase the counter
      counter++;
      direction = DIRECTION_CW;

      if (counter >3)
      counter = 0;

    }
    
    
  u8g2.clearBuffer();
  
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    
    u8g2.print(" | COUNTER: ");
    
    u8g2.print(counter);

  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(10,30);
  
  u8g2.print("Temperature: ");
  u8g2.print(t);
  u8g2.print(" C");

     u8g2.sendBuffer();


  }

  // save last CLK state
  prev_CLK_state = CLK_state;

  if (button.isPressed()) {

    
    u8g2.clearBuffer();
    
  
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    u8g2.print("The button is pressed");
    u8g2.sendBuffer();
  }

}

Post the three working complete sketches that you are trying to combine.

a7

Temperature

/*
 * How to use 12C Oled
 * Display Oled
 * GND --> GND
 * VCC --> 5V
 * SDA --> A5
 * SLC --> A4
 */

  //* How to use the DHT-22 sensor with Arduino
//   Temperature sensor
 //  GND --> GND
 //  VCC --> 3.3V
 //  DAT -->D7

/*
 * How to use Air Flow
 * Display Oled
 * OUT --> 2
 * GND --> GND
 * VCC --> 5V
 */
 

#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>

#include <DHT.h>;

//Constants
#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

//Variables
int t; //Stores temperature value

void setup() 
{
  u8g2.begin();
  dht.begin();
}

void loop() 
{
  //Read data and store it to variables t (temperature)
  // Reading temperature takes about 250 milliseconds!
  t = dht.readTemperature();
  
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(10,10);
  
  u8g2.print("Temperature: ");
  u8g2.print(t);
  u8g2.print(" C");

  u8g2.sendBuffer();



}

Counter

/*
 * How to use 12C Oled
 * Display Oled
 * GND --> GND
 * VCC --> 5V
 * SDA --> A5
 * SLC --> A4
 
  //* How to use the DHT-22 sensor with Arduino
//   Temperature sensor
 //  GND --> GND
 //  VCC --> 3.3V
 //  DAT -->D7

 * How to use Air Flow
 * Display Oled
 * OUT --> 2
 * GND --> GND
 * VCC --> 5V

 * How to Button
 * Count and Press
 * CLK --> 2
 * DT --> 3
 * SW --> 4
 * + --> 3V3
 * GND --> GND
 */


#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>

#include <ezButton.h>  // the library to use for SW pin

#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4

#define DIRECTION_CW 0   // clockwise direction
#define DIRECTION_CCW 1  // counter-clockwise direction

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;

ezButton button(SW_PIN);  // create ezButton object that attach to pin 4

void setup() {

  u8g2.begin();
  
  // configure encoder pins as inputs
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);
  button.setDebounceTime(50);  // set debounce time to 50 milliseconds

  // read the initial state of the rotary encoder's CLK pin
  prev_CLK_state = digitalRead(CLK_PIN);
}

void loop() {
  button.loop();  // MUST call the loop() function first

  // read the current state of the rotary encoder's CLK pin
  CLK_state = digitalRead(CLK_PIN);


  // If the state of CLK is changed, then pulse occurred
  // React to only the rising edge (from LOW to HIGH) to avoid double count
  if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
     // if the DT state is HIGH
    // the encoder is rotating in counter-clockwise direction => decrease the counter
    if (digitalRead(DT_PIN) == HIGH) {
      counter--;      
      direction = DIRECTION_CCW;

      if (counter <0)
      counter = 0;
      
    } else {
      // the encoder is rotating in clockwise direction => increase the counter
      counter++;
      direction = DIRECTION_CW;

      if (counter >3)
      counter = 0;

    }
    
    
  u8g2.clearBuffer();
  
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    
    u8g2.print(" | COUNTER: ");
    
    u8g2.print(counter);

     u8g2.sendBuffer();


  }

  
   

  // save last CLK state
  prev_CLK_state = CLK_state;

  if (button.isPressed()) {

    
    u8g2.clearBuffer();
    
  
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    u8g2.print("The button is pressed");
    u8g2.sendBuffer();
  }

}

Air flow

/*
 * How to use 12C Oled
 * Display Oled
 * GND --> GND
 * VCC --> 5V
 * SDA --> A5
 * SLC --> A4

 // How to use the DHT-22 sensor with Arduino
 // Temperature sensor
 // GND --> GND
 // VCC --> 3.3V
 // DAT -->D7

 * How to use Air Flow
 * Display Oled
 * OUT --> 2
 * GND --> GND
 * VCC --> 5V
 */

#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display


const float wheelDiameter = 0.01;
volatile unsigned long rev = 0;
unsigned long oldTime = 0;
unsigned long deltaTime;
float airspeed; // Airspeed in meters per second

void isr()
{
rev++;
}

void setup()
{
  u8g2.begin();
}


void loop()
{
  
  attachInterrupt(digitalPinToInterrupt(2), isr, RISING);
delay(1000);
detachInterrupt(digitalPinToInterrupt(2));
deltaTime = millis() - oldTime;
airspeed = (2 * 3.14159265350 * (wheelDiameter / 2) * rev) / (deltaTime / 1000); //Calculate airspeed in m/s 
oldTime = millis();
rev = 0;
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(10,25);
u8g2.print("Air Speed :");
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(15,38);
u8g2.print(airspeed, 2); // Print airspeed with one decimal place
u8g2.print("m/s");

//Add conditional statement for displaying the text
if(airspeed < 6.0) 
{
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(5,55);
u8g2.print("Change CAF !");
}

else
{
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(5,55);
u8g2.print("CAF safe to use");
}

u8g2.sendBuffer();

attachInterrupt(digitalPinToInterrupt(2), isr, RISING);


}

A quick scan shows I/O pin 2 used in the count and airflow sketches.  Didn't check for other conflicts.  Ensure that each I/O pin is used for only one purpose in the combined sketch.  Since airflow is using interrupts I'd move the count pin 2 elsewhere.  A0-A5 can also be used for digital I/O.

thanks! i will change the pin. how about my temperature sketch? when i combine it, there will be no output display but if i delete the 't = dht.readTemperature()', the output will display just fine

I successfully merge the 2 code (temperature and counter). As the airflow and temperature quite same, i try to do the same for the airflow but end up only airflow that display. Is there any other way?

#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include <DHT.h>

// Pin definitions
#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4
#define DHTPIN 8     // Pin for DHT sensor
#define DHTTYPE DHT22 // DHT 22

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // OLED display

// Variables
int counter = 0;
int direction = 0; // Clockwise direction
int CLK_state;
int prev_CLK_state;

// Airflow variables
const float wheelDiameter = 0.01; // Wheel diameter in meters
volatile unsigned long rev = 0;    // Revolution counter
unsigned long oldTime = 0;          // Last time measurement
unsigned long deltaTime;            // Time delta for airspeed calculation
float airspeed;                     // Airspeed in meters per second

// Timing for DHT readings
unsigned long previousDHTMillis = 0; // Last DHT read time
const long DHTInterval = 2000;        // Interval for reading DHT (2 seconds)

unsigned long previousAFMillis = 0; // Last DHT read time
const long AFInterval = 1000; 

void isr() {
    rev++;
}

void setup() {
    u8g2.begin();
    dht.begin();

    // Configure pins
    pinMode(CLK_PIN, INPUT);
    pinMode(DT_PIN, INPUT);

    // Attach interrupt for airflow
    attachInterrupt(digitalPinToInterrupt(7), isr, RISING);
    prev_CLK_state = digitalRead(CLK_PIN); // Read initial state of CLK
}

void loop() {
    // Read DHT sensor at specified interval
    unsigned long currentDHTMillis = millis();
    if (currentDHTMillis - previousDHTMillis >= DHTInterval) {
        previousDHTMillis = currentDHTMillis;
        readTemperature(); // Read temperature
    }

    unsigned long currentAFMillis = millis();
    if (currentAFMillis - previousAFMillis >= AFInterval) {
        previousAFMillis = currentAFMillis;
        //calculateAirspeed();
    }

    // Update the counter based on the rotary encoder
    counterS();

}

void readTemperature() {
    int t = dht.readTemperature(); // Read temperature in Celsius

    // Display temperature
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 25);
    u8g2.print("Temperature: ");
    u8g2.print(t);
    u8g2.print(" C");
    u8g2.sendBuffer();
}

void counterS() {
    // Read the current state of the rotary encoder's CLK pin
    CLK_state = digitalRead(CLK_PIN);

    // If the state of CLK is changed, then pulse occurred
    if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
        // If the DT state is HIGH, the encoder is rotating counter-clockwise
        if (digitalRead(DT_PIN) == HIGH) {
            counter--;      
            direction = 1; // CCW

            if (counter < 0) counter = 0;
        } else {
            // The encoder is rotating clockwise
            counter++;
            direction = 0; // CW

            if (counter > 3) counter = 0;
        }

        // Clear and Update counter display  
        u8g2.clearBuffer();
        u8g2.setFont(u8g2_font_ncenB08_tr);
        u8g2.setCursor(10, 10);
        u8g2.print("Counter: ");
        u8g2.print(counter);
        u8g2.sendBuffer();
    }

    // Save last CLK state
    prev_CLK_state = CLK_state;
}

void calculateAirspeed() {

  detachInterrupt(digitalPinToInterrupt(7));
  deltaTime = millis() - oldTime;
  airspeed = (2 * 3.14159265350 * (wheelDiameter / 2) * rev) / (deltaTime / 1000); //Calculate airspeed in m/s 
  oldTime = millis();
  rev = 0;

  u8g2.clearBuffer();

  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(10,40);
  u8g2.print("Air Speed :  ");
  u8g2.print(airspeed, 2); // Print airspeed with one decimal place
  u8g2.print("m/s");

    // Conditional message based on airspeed
    if (airspeed < 6.0) {
        u8g2.setCursor(5, 55);
        u8g2.print("Change CAF!");
    } else {
        u8g2.setCursor(5, 55);
        u8g2.print("CAF safe to use");
    }

  u8g2.sendBuffer();

  attachInterrupt(digitalPinToInterrupt(7), isr, RISING);
}

Which Arduino board is used?

UNO board

Only works for pins 2 and 3 on an UNO. All other pins have 'pin change' interrupts - which require a different setup procedure. And, those other interrupts are only 'on change' interrupts.

#include <U8g2lib.h>

#include <DHT.h>;

I am not familiar with those libraries. It's possible there's something going on inside one or both that's causing the issue.

yeah, i dont know if i can display it both together or not, maybe theres a way but im not find the way yet.

by the way thanks, i already change the pin and everything works just fine, just still cant display both temperature and air flow together.

Nano shows high use of dynamic memory. Could the 1k buffers be using up memory in run time?

Sketch uses 12996 bytes (42%) of program storage space. Maximum is 30720 bytes.
Global variables use 1733 bytes (84%) of dynamic memory, leaving 315 bytes for local variables. Maximum is 2048 bytes.
first code without empty lines
#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>

#include <DHT.h>;

#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

#include <ezButton.h>  // the library to use for SW pin

#define CLK_PIN 2
#define DT_PIN 3
#define SW_PIN 4

#define DIRECTION_CW 0   // clockwise direction
#define DIRECTION_CCW 1  // counter-clockwise direction

U8G2_SSD1306_128X64_NONAME_2_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display

int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
int t; //Stores temperature value

ezButton button(SW_PIN);  // create ezButton object that attach to pin 4

void setup() {
  u8g2.begin();
  dht.begin();
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);
  button.setDebounceTime(50);  // set debounce time to 50 milliseconds
  prev_CLK_state = digitalRead(CLK_PIN);
}

void loop() {
  button.loop();  // MUST call the loop() function first
  CLK_state = digitalRead(CLK_PIN);
  t = dht.readTemperature();
  if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
    if (digitalRead(DT_PIN) == HIGH) {
      counter--;
      direction = DIRECTION_CCW;
      if (counter < 0)
        counter = 0;
    } else {
      counter++;
      direction = DIRECTION_CW;
      if (counter > 3)
        counter = 0;
    }
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    u8g2.print(" | COUNTER: ");
    u8g2.print(counter);
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 30);
    u8g2.print("Temperature: ");
    u8g2.print(t);
    u8g2.print(" C");
    u8g2.sendBuffer();
  }

  prev_CLK_state = CLK_state;
  if (button.isPressed()) {
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    u8g2.print("The button is pressed");
    u8g2.sendBuffer();
  }
}

Sorry but my internet service provider pulled the plug on the free web space they offered and took my whole site off line and didn't even offer a web redirection service. Tech support says every time they suggest this the CEO of the company says it will cost hundreds of thousand pounds to implement.
This file

Merging_Code.zip (663.3 KB)

When un-compressed you can double click the .HTML file and it should load just that page into your default browser. Let me know if you have any problems with this. Of course none of the links off this page will work.

I already try that and its not like i dont know how to merge, its just i missing the function that i need to display both temperature and airflow together. but as i research, most said its impossible to run 2 functions together as both use 2 different sensor, so thats why im trying to ask here, for suggestion or solution or recommendation. I will do the code by myself, but anyone who willing to try and error and share it, thats okey too.

thanks.

sorry, im not sure about that, is there anything i need to do?

I think you are getting confused here. You can run two functions seemingly at the same time if you write your code as a state machine. Lots of people ask about this problem, we have at least one every day.

Try looking at:-

Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0
Or Adafruit's

Or search the net for other examples.

Post your compile report.

Counter and temperature

/*
 * How to use 12C Oled
 * Display Oled
 * GND --> GND
 * VCC --> 5V
 * SDA --> A5
 * SLC --> A4
 
  //* How to use the DHT-22 sensor with Arduino
//   Temperature sensor
 //  GND --> GND
 //  VCC --> 3.3V
 //  DAT --> 4

 * How to use Air Flow
 * Display Oled
 * OUT --> 2
 * GND --> GND
 * VCC --> 5V

 * How to Button
 * Count and Press
 * CLK --> 8
 * DT --> 6
 * SW --> 7
 * + --> 3V3
 * GND --> GND
 */

#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include <ezButton.h>  // the library to use for SW pin
#include <DHT.h>;

// Pin definitions
#define CLK_PIN 8
#define DT_PIN 6
#define SW_PIN 7

#define DHTPIN 4     // Pin for DHT sensor
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // OLED display

// Variables
int t; // Stores temperature value
int counter = 0;
int direction = 0; // Clockwise direction
int CLK_state;
int prev_CLK_state;

unsigned long previousMillis = 0; // Last update time
const long interval = 2000; // Interval for reading temperature (milliseconds)

void setup() 
{
  u8g2.begin();
  dht.begin();

  // Configure encoder pins as inputs
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);

  // Read the initial state of the rotary encoder's CLK pin
  prev_CLK_state = digitalRead(CLK_PIN);
}

void loop() 
{  

  // Check if it's time to read the temperature
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis; // Save the last time we read the DHT
    readTemperature(); // Call the function to read DHT
  }

  // Update the counter based on the rotary encoder
  counterS();
}

void readTemperature() {
    // Read data and store it to variable t (temperature)
    t = dht.readTemperature();

    // Clear buffer and print temperature
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 25);
    u8g2.print("Temperature: ");
    u8g2.print(t);
    u8g2.print(" C");
    u8g2.sendBuffer();
}

void counterS() {
  // Read the current state of the rotary encoder's CLK pin
  CLK_state = digitalRead(CLK_PIN);

  // If the state of CLK is changed, then pulse occurred
  if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
    // If the DT state is HIGH, the encoder is rotating counter-clockwise
    if (digitalRead(DT_PIN) == HIGH) {
      counter--;      
      direction = 1; // CCW

      if (counter < 0) counter = 0;
    } else {
      // The encoder is rotating clockwise
      counter++;
      direction = 0; // CW

      if (counter > 3) counter = 0;
    }

    // Clear and Update counter display  
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    u8g2.print("Counter: ");
    u8g2.print(counter);
    u8g2.sendBuffer();
  }

  // Save last CLK state
  prev_CLK_state = CLK_state;
}

counter and air flow

/*
 * How to use 12C Oled
 * Display Oled
 * GND --> GND
 * VCC --> 5V
 * SDA --> A5
 * SLC --> A4
 
  //* How to use the DHT-22 sensor with Arduino
//   Temperature sensor
 //  GND --> GND
 //  VCC --> 3.3V
 //  DAT --> 4

 * How to use Air Flow
 * Display Oled
 * OUT --> 2
 * GND --> GND
 * VCC --> 5V

 * How to Button
 * Count and Press
 * CLK --> 8
 * DT --> 6
 * SW --> 7
 * + --> 3V3
 * GND --> GND
 */

#include <Arduino.h>
#include <SPI.h>
#include <U8g2lib.h>
#include <ezButton.h>  // the library to use for SW pin
#include <DHT.h>;

// Pin definitions
#define CLK_PIN 8
#define DT_PIN 6
#define SW_PIN 7

#define DHTPIN 4     // Pin for DHT sensor
#define DHTTYPE DHT22   // DHT 22
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // OLED display

// Variables
int t; // Stores temperature value
int counter = 0;
int direction = 0; // Clockwise direction
int CLK_state;
int prev_CLK_state;

unsigned long previousMillis = 0; // Last update time
const long interval = 2000; // Interval for reading temperature (milliseconds)

const float wheelDiameter = 0.01;
volatile unsigned long rev = 0;
unsigned long oldTime = 0;
unsigned long deltaTime;
float airspeed; // Airspeed in meters per second

void isr()
{
  rev++;
}

void setup() 
{
  u8g2.begin();
  dht.begin();

  // Configure encoder pins as inputs
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);

  // Read the initial state of the rotary encoder's CLK pin
  prev_CLK_state = digitalRead(CLK_PIN);
  
  attachInterrupt(digitalPinToInterrupt(2), isr, RISING);
}

void loop() 
{  

  // Read the current state of the rotary encoder's CLK pin
  CLK_state = digitalRead(CLK_PIN);

  // Check if it's time to read the temperature
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis; // Save the last time we read the DHT
    
    airFlow();
    //readTemperature(); // Call the function to read DHT
  }

  // Update the counter based on the rotary encoder
  counterS();
}

void readTemperature() {
    // Read data and store it to variable t (temperature)
    t = dht.readTemperature();

    // Clear buffer and print temperature
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 25);
    u8g2.print("Temperature: ");
    u8g2.print(t);
    u8g2.print(" C");
    u8g2.sendBuffer();
}

void counterS() {
  
  // If the state of CLK is changed, then pulse occurred
  if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
    // If the DT state is HIGH, the encoder is rotating counter-clockwise
    if (digitalRead(DT_PIN) == HIGH) {
      counter--;      
      direction = 1; // CCW

      if (counter < 0) counter = 0;
    } 
    
    else {
      // The encoder is rotating clockwise
      counter++;
      direction = 0; // CW

      if (counter > 3) counter = 0;
    }

    // Clear and Update counter display  
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(10, 10);
    u8g2.print("Counter: ");
    u8g2.print(counter);
    u8g2.sendBuffer();
  }

  // Save last CLK state
  prev_CLK_state = CLK_state;
}

void airFlow(){
  
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= 1000) {
    previousMillis = currentMillis; // Save the last time we read the DHT
    
    detachInterrupt(digitalPinToInterrupt(2));
    deltaTime = millis() - oldTime;
    airspeed = (2 * 3.14159265350 * (wheelDiameter / 2) * rev) / (deltaTime / 1000); //Calculate airspeed in m/s 
    oldTime = millis();
    rev = 0;
  }
  
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(10,25);
  u8g2.print("Air Speed :");
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(15,38);
  u8g2.print(airspeed, 2); // Print airspeed with one decimal place
  u8g2.print("m/s");

  //Add conditional statement for displaying the text
  if(airspeed < 6.0) 
  {
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(5,55);
    u8g2.print("Change CAF !");
  }

  else
  {
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.setCursor(5,55);
    u8g2.print("CAF safe to use");
  }

  u8g2.sendBuffer();

  attachInterrupt(digitalPinToInterrupt(2), isr, RISING);
}

I still try to find a way to merge it together, temperature, air flow and counter. i try to merge the air flow and temperature together but it cannot display the output together.

May be some help here: CombiningArduinoSketches - ArduinoInfo

I already refer to this, hmm

The U8g2 library allocates the buffer at compile-time, so it is included in the dynamic memory usage.

Why are you using software I2C when the comments in the sketch show the display connected to the hardware I2C pins?

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display