Question: Using Arduino zero / uno display the light and temperature in the form of a bar graph from the BME 2 8 0 Sensor and

Using Arduino zero/uno display the light and temperature in the form of a bar graph from the BME280 Sensor and BH1750 sensor using SDI12 protocol. Display this information on the Arduinos build in LCD.
Code I've done so far:
#include
#include
#include
#include
#include
#include // Adjust if you're using a different display
#define BHFVI_PIN A1
#define PB_PIN 2
#define TFT_CS 10
#define TFT_MOSI 11
#define TFT_RST 6
#define TFT_DC 7
#define TFT_SCLK 13
#define DIRO 6// Assuming this is the pin for SDI-12 direction control
Adafruit_BME280 bme;
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC,TFT_MOSI,TFT_SCLK, TFT_RST);
float temperature;
int lightData;
const int arraySize =10;
float temperatureArray[arraySize];
int lightArray[arraySize];
int deviceAddress =0; // Add your device address here
void setup(){
Serial.begin(9600);
Serial1.begin(1200, SERIAL_7E1);
if (!bme.begin(0x76)){// The I2C address might be 0x77, try this if 0x76 doesn't work
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
if (!SD.begin()){
Serial.println("SD card initialization failed!");
while (1);
}
tft.initR(INITR_BLACKTAB); // Or the appropriate initialization for your display
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
pinMode(DIRO, OUTPUT);
pinMode(PB_PIN, INPUT_PULLUP);
Serial.println("Setup completed!");
}
void loop(){
if (digitalRead(PB_PIN)== LOW){
Serial.println("Button pressed, starting measurement...");
int sensorStatus = startMeasurement();
if (sensorStatus ==2){
Serial.println("Both sensors measured successfully.");
sendContinuousData();
logData();
arrayFull();
displayGraphs(1); // Display temperature graph
delay(5000);
displayGraphs(2); // Display light graph
} else {
Serial.println("Sensor reading failed");
}
}
}
int startMeasurement(){
int temperatureToggle =0;
int luxToggle =0;
temperature = bme.readTemperature();
Serial.print("Temperature: ");
Serial.println(temperature);
if (!isnan(temperature)){
temperatureToggle =1;
}
lightData = analogRead(BHFVI_PIN);
Serial.print("Light Data: ");
Serial.println(lightData);
if (lightData >0){
luxToggle =1;
}
return temperatureToggle + luxToggle;
}
void sendContinuousData(){
String dataString = String(deviceAddress)+"+"+ String(temperature,2)+"+"+ String(lightData);
Serial.print("Sending continuous data: ");
Serial.println(dataString);
SDI12Send(dataString);
}
void SDI12Send(String message){
Serial.print("SDI-12 Message: ");
Serial.println(message);
digitalWrite(DIRO, LOW);
delay(100);
Serial1.print(message +"\r
");
Serial1.flush();
Serial1.end();
Serial1.begin(1200, SERIAL_7E1);
digitalWrite(DIRO, HIGH);
}
void logData(){
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile){
// You need to implement a way to get the current time, like using a RTC module
Serial.println("Logging data...");
dataFile.print("Time: ");
dataFile.print("- Sensor: ");
dataFile.print("Temp: ");
dataFile.print(temperature);
dataFile.print(" C, Light: ");
dataFile.println(lightData);
dataFile.close();
Serial.println("Data logged successfully.");
} else {
Serial.println("Error opening datalog.txt");
}
}
void arrayFull(){
Serial.println("Shifting array data...");
for (int i =1; i < arraySize; i++){
temperatureArray[i -1]= temperatureArray[i];
lightArray[i -1]= lightArray[i];
}
temperatureArray[arraySize -1]= temperature;
lightArray[arraySize -1]= lightData;
Serial.println("Arrays updated.");
}
void displayGraphs(int sensorSelection){
tft.fillScreen(ST77XX_BLACK);
Serial.print("Displaying graph for: ");
switch (sensorSelection){
case 1:
Serial.println("Temperature");
displayBarGraph(temperatureArray,-40,85, "Temp (C)");
break;
case 2:
Serial.println("Light Data");
// Convert lightArray to float[] before displaying
float tempLightArray[arraySize];
for (int i =0; i < arraySize; i++){
tempLightArray[i]=(float)lightArray[i]; // Casting int to float

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!