/*
Wokwi | general
pilis 5/5/25 8:59 AM
Okay, so, how can the C code be adjusted so that if the gas sensor is
at 100 ppm, only the message "Gas Detected" appears?
https://wokwi.com/projects/429836622322713601
This project at the end, has to follow these requirements:
Setting the LED for different colors and notifying this color in an LCD message
relay activation on gas sensor activation
*/
#include <LiquidCrystal.h>
#include "HX711.h"
#include <TM1637.h>
// LCD Pins (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(15, 14, 13, 12, 11, 10);
// HX711 Pins (DT, SCK)
HX711 scale;
#define HX711_DT 6
#define HX711_SCK 5
// TM1637 7-Segment Pins
#define TM1637_CLK 2
#define TM1637_DIO 3
TM1637 display(TM1637_CLK, TM1637_DIO);
// RGB LED Pins (common cathode!)
#define RED_PIN 9
#define GREEN_PIN 8
#define BLUE_PIN 7
// Relay and Analog Gas Sensor
#define RELAY_PIN 4
#define GAS_SENSOR_ANALOG A0
int colorChangeCount = 0;
int currentColor = -1;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.print("Initializing...");
scale.begin(HX711_DT, HX711_SCK);
display.init();
display.set(BRIGHT_TYPICAL);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
lcd.clear();
}
void setColor(int colorIndex, const char* colorName) {
// Only change color if it's different from current
if (colorIndex == currentColor) return;
currentColor = colorIndex;
// Set RGB based on colorIndex: 0=RED, 1=GREEN, 2=BLUE
digitalWrite(RED_PIN, colorIndex == 0 ? HIGH : LOW);
digitalWrite(GREEN_PIN, colorIndex == 1 ? HIGH : LOW);
digitalWrite(BLUE_PIN, colorIndex == 2 ? HIGH : LOW);
// LCD Row 0: show color
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Color: ");
lcd.print(colorName);
// Increment counter and show on display
colorChangeCount++;
int count = colorChangeCount;
display.display(0, (count / 1000) % 10);
display.display(1, (count / 100) % 10);
display.display(2, (count / 10) % 10);
display.display(3, count % 10);
}
void loop() {
// Cycle through RED, GREEN, BLUE
setColor(0, "RED ");
delay(2000);
setColor(1, "GREEN");
delay(2000);
setColor(2, "BLUE ");
delay(2000);
// get weight value
float weight = scale.get_units();
// Read analog gas sensor
int analogValue = analogRead(GAS_SENSOR_ANALOG);
float ppm = (analogValue / 1023.0) * 100.0;
Serial.print("PPM: ");
Serial.println(ppm);
Serial.print("Weight value: ");
Serial.println(weight);
if (weight > 400) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("An object has");
lcd.setCursor(0, 1);
lcd.print("been placed.");
} else {
//lcd.clear();
//lcd.setCursor(0, 1);
//lcd.print("No weight");
}
delay(1000);
// LCD Row 1: Gas status
lcd.setCursor(0, 1);
if (ppm > 50) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("GAS DETECTED!");
digitalWrite(RELAY_PIN, HIGH);
} else {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("No Gas Detected");
digitalWrite(RELAY_PIN, LOW);
}
}