Basic and noob question.

Rattan_26
Posts: 1
Joined: Sat May 25, 2024 11:12 pm

Basic and noob question.

Postby Rattan_26 » Sat May 25, 2024 11:21 pm

  1. hello everyone, i am new to esp8266 and arduino and i have a very basic question, i am using sensiron scd41 sensor to measure C02, temp and humidity and below is the example code provided by sensiron. My question is if i only want to measure CO2, how can i do that as the scd4x.readMeasurement function needs the 3 arguments( co2, temp and humidity) but how can i only pass one argument or just one or two values?
```cpp
#include <Arduino.h>
#include <SensirionI2CScd4x.h>
#include <Wire.h>

SensirionI2CScd4x scd4x;

void setup() {

Serial.begin(115200);
while (!Serial) {
delay(100);
}

Wire.begin();

uint16_t error;
char errorMessage[256];

scd4x.begin(Wire);

// Start Measurement
error = scd4x.startPeriodicMeasurement();
if (error) {
Serial.print("Error trying to execute startPeriodicMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}

Serial.println("Waiting for first measurement... (5 sec)");
}

uint16_t co2 = 0;
float temperature = 0.0f;
float humidity = 0.0f;

void loop() {
uint16_t error;
char errorMessage[256];

delay(100);

// Read Measurement

bool isDataReady = false;
error = scd4x.getDataReadyFlag(isDataReady);
if (error) {
Serial.print("Error trying to execute getDataReadyFlag(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
return;
}
if (!isDataReady) {
return;
}
error = scd4x.readMeasurement(co2, temperature, humidity);
if (error) {
Serial.print("Error trying to execute readMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else if (co2 == 0) {
Serial.println("Invalid sample detected, skipping.");
} else {
Serial.print("Co2:");
Serial.print(co2);
Serial.print("\t");
Serial.print("Temperature:");
Serial.print(temperature);
Serial.print("\t");
Serial.print("Humidity:");
Serial.println(humidity);
}
}

```

hobby_guy
Posts: 20
Joined: Sat Jan 29, 2022 3:29 pm

Re: Basic and noob question.

Postby hobby_guy » Wed Sep 04, 2024 12:25 pm

You only pass those three variables to the scd4x.readMeasurement method such that the library can fill the measurement values into them. This is passing by reference, which is a common alternative to having to return an "object" (a struct?) containing the three values. So, you call the method with the 3 variables, and you simply use the co2 variable -- disregarding the other two.

Who is online

Users browsing this forum: No registered users and 2 guests