Why is this code not working

This code isn't working for some reason that I'm not really sure about[CTYInvention_copy.zip|attachment](upload://xbCXiGQfaTpCWFRj0ILlxoIBhym.zip) (4.3 KB)
The hardware is a sound sensor arduino mega RFID card and reader motion sensor and DHT11 sensor

Here's the code not in a zip file:
#include <Wire.h>
#include <LiquidCrystal.h>

// Define the I2C address of the LCD
#define LCD_ADDR 0x27

// Define the pins that the LCD is connected to
#define rs 7
#define enable 8
#define d4 9
#define d5 10
#define d6 11
#define d7 12

// Create the LCD object
LiquidCrystal lcd(rs, enable, d4, d5, d6, d7);

// DHT sensor library - Version: Latest
#include <dht.h>
#include <DHT_U.h>

// Define the DHT sensor type
#define DHTTYPE DHT11

// Initialize the DHT sensor
DHT dht();

// Read the temperature data from the sensor.
float temperature = dht.readTemperature();

// Read the humidity data from the sensor.
float humidity = dht.readHumidity();

// Initialize the RFID reader
MFRC522 mfrc522(SS_PIN, RST_PIN);

// Initialize the sound sensor
int soundSensor = 5;
int soundsensorValue = analogRead(A1);

void setup() {
// Set the motion sensor as an input
pinMode(Motionsensor, INPUT);

// Initialize the serial port
Serial.begin(9600);

// Initialize the LCD
lcd.begin(16, 2);

// Initialize the DHT sensor
DHT.setup(5);
}

void loop() {
// Read the value of the motion sensor
int val = digitalRead(Motionsensor);

// Check if the RFID tag is present
if (mfrc522.PICC_ReadCardSerial()) {
// Print the RFID tag data
Serial.print("UID: ");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();

// Check if the RFID tag is authorized
if (mfrc522.uid.uidByte[0] == 0x01 && mfrc522.uid.uidByte[1] == 0x02 && mfrc522.uid.uidByte[2] == 0x03 && mfrc522.uid.uidByte[3] == 0x04) {
  // If it is, print a message
  lcd.clear();
  lcd.print("Temperature: ");
  lcd.print(temperature);
  lcd.print("C");
  lcd.print("Humidity: ");
  lcd.print(humidity);
  lcd.print("%");
  lcd.print("Sound level: ");
  lcd.print(soundsensorValue);
  lcd.print("%");
  delay(10000);
  lcd.clear();
} else {
  // Do nothing
}

} else {
// Do nothing
}
}

Look here. You're missing all the relevant info and you're not posting your code in a readable way. Please take a moment to read the forum guidelines.

What do you mean by "not working". That's a pretty vague statement.

1 Like

Please edit your post to add code tags ("< code >" editor button).

Add links to the components, and a wiring diagram

1 Like
  1. You do not have a DHT pin assigned.
#define DHTPIN 5     // Digital pin connected to the DHT sensor
  1. using an outdated dht.h file (might want`#include "DHT.h"1
  2. initializing objects using both DHT and dht
  3. DHT.setup(5) is probably the outdated method to initialize the DHT (on pin 5)
  4. You have no configuration for Motionsensor
  5. You try to initialize MFRC522 without a library or instantiation.
  6. more

Here's the code in coding brackets

#include <Wire.h>
#include <LiquidCrystal.h>

// Define the I2C address of the LCD
#define LCD_ADDR 0x27

// Define the pins that the LCD is connected to
#define rs 7
#define enable 8

// Create the LCD object
LiquidCrystal lcd(rs, enable);

// DHT sensor library - Version: Latest
#include <dht.h>
#include <DHT_U.h>

// Define the DHT sensor type
#define DHTTYPE DHT11

// Initialize the DHT sensor
DHT dht();

// Read the temperature data from the sensor.
float temperature = dht.readTemperature();

// Read the humidity data from the sensor.
float humidity = dht.readHumidity();

// Initialize the RFID reader
MFRC522 mfrc522(SS_PIN, RST_PIN);

// Initialize the sound sensor
int soundSensor = 5;
int soundsensorValue = analogRead(A1);

void setup() {
  // Set the motion sensor as an input
  pinMode(Motionsensor, INPUT);

  // Initialize the serial port
  Serial.begin(9600);

  // Initialize the LCD
  lcd.begin(16, 2);

  // Initialize the DHT sensor
  DHT.setup(5);
}

void loop() {
  // Read the value of the motion sensor
  int val = digitalRead(Motionsensor);

  // Check if the RFID tag is present
  if (mfrc522.PICC_ReadCardSerial()) {
    // Print the RFID tag data
    Serial.print("UID: ");
    for (byte i = 0; i < mfrc522.uid.size; i++) {
      Serial.print(mfrc522.uid.uidByte[i], HEX);
    }
    Serial.println();

    // Check if the RFID tag is authorized
    if (mfrc522.uid.uidByte[0] == 0x01 && mfrc522.uid.uidByte[1] == 0x02 && mfrc522.uid.uidByte[2] == 0x03 && mfrc522.uid.uidByte[3] == 0x04) {
      // If it is, print a message
      lcd.clear();
      lcd.print("Temperature: ");
      lcd.print(temperature);
      lcd.print("C");
      lcd.print("Humidity: ");
      lcd.print(humidity);
      lcd.print("%");
      lcd.print("Sound level: ");
      lcd.print(soundsensorValue);
      lcd.print("%");
      delay(10000);
      lcd.clear();
    } else {
      // Do nothing
    }
  } else {
    // Do nothing
  }
}

It's giving me an error with the LCD configuration as well can you help me with that??

Also can you give me the code I posted with the fixed errors

You mixed a bunch of random code. There is no fixing it, you need to rewrite it. Make something yourself that works with each component you have.

  • seems like you need the LiquidCrystal_I2C.h not LiquidCrystal.h

  • i believe dht needs to be defined where x,t are 2 pin #s
    DHT dht(x, y);

  • while temp can be defined globally (outside any function), setting it's value using dht.readTemperature() needs to be done inside loop()

float temperature = dht.readTemperature();
  • same for humidity
float humidity = dht.readHumidity();

Motionsensor needs to be defined as some pin #

  • i believe dht.begin() needs to be called instead of dht.setup(5), and what is '5'?

these are things that need to be done just to make the code compile. i have no idea what is needed to make it do what you want.

you can just work thru the error msgs to fix these problems

Hi, @zm476

Did you write this code?

If so did you write it in stages?
Getting each stage to work before going to the next.

You should have some code that JUST tests the LCD?
If not, then forget your main code for the moment, and write code JUST for the LCD and get it working.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Alright I'll try but I'm not sure I'll be able to because I've only got another week to do this project and if I don't get this working I'll be in big trouble

Man, you better get started on writing the first stage. If you wait much later to realize that you need to write this code one thing at a time then you're not going to have enough.

Hi, @zm476

Post some images of all your hardware for a start, lets see what you actually have?
Especially the LCD unit.

What level of education is this project for?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Can you explain, what exactly you want to achieve? Your code is missing a lot of stuff, e.g. you didn´t implement the library for the rfid reader, the dht library is missing and so on. Also please let us know, what kind of hardware you want to use.
I raise you this as a general starting point:

#include <SPI.h>
#include <MFRC522.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define SS_PIN 53    // Slave Select Pin for MFRC522
#define RST_PIN 5    // Reset Pin for MFRC522
#define DHT_PIN A0   // Analog Pin for DHT11
#define DHT_TYPE DHT11

DHT dht(DHT_PIN, DHT_TYPE);

MFRC522 mfrc522(SS_PIN, RST_PIN);
Adafruit_SH1106 display(128, 64, &SPI, 4, 3, 2, 1);

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  dht.begin();
  display.begin(SH1106_SWITCHCAPVCC);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("RFID Card Reader");
  display.display();
}

void loop() {
  // Check for RFID card
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
    // Verify the card UID
    if (mfrc522.uid.uidByte[0] == 0x01 && mfrc522.uid.uidByte[1] == 0x02 &&
        mfrc522.uid.uidByte[2] == 0x03 && mfrc522.uid.uidByte[3] == 0x04) {
      // Read temperature, humidity, and sound level
      float temperature = dht.readTemperature();
      float humidity = dht.readHumidity();
      int soundLevel = analogRead(A1);

      // Display on SH1106
      display.clearDisplay();
      display.setCursor(0, 0);
      display.println("Temp: " + String(temperature) + "C");
      display.setCursor(0, 10);
      display.println("Humidity: " + String(humidity) + "%");
      display.setCursor(0, 20);
      display.println("Sound Level: " + String(soundLevel));
      display.display();
      delay(2000); // Delay to allow reading

      display.clearDisplay();
      display.setCursor(0, 0);
      display.println("RFID Card Reader");
      display.display();
    }

    mfrc522.PICC_HaltA();
  }

  mfrc522.PICC_IsNewCardPresent();
  mfrc522.PICC_ReadCardSerial();

  // Read temperature and humidity every 10 seconds
  if (millis() % 10000 == 0) {
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print(" °C, Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
  }
}

You´ll have to adjust this to your used hardware and wiring.

With a month left, you'd better have all the hardware already. Show us

  • a schematic of what you think you need to build
  • links to web info everything(all parts hopefully have useful datasheets)
  • a rudimentary plan of action
  • tell us, briefly, your experience level and how much time you can give this project in the month ahead

It's quite do-able, with the right attitude, information, and approach. Otherwise, well, good luck. Maybe start by re-reading the how to get the best from this forum, linked to earlier in this thread.

Very high

I do

no idea to which postings this is related.

well nobody can take away from you the decision how big this trouble is
and how high you will set the priority of finishing this project in time.

If your programming knowledge is rather low my estimation is
if you invest 7 days each day working 10 to 12 hours on this project there is chance to finish it within a week.

If working exclusivly on this project is causing different "big trouble"
you should cancel the party todays evening, cancel going to the beach on sunday or cancel whatever you want to do in your freetime and work today saturday and sunday as much as you can on this project
and then invest 1 hour of time on monday to explain your situation to your boss/teacher/professor that this is your actual stage of writing the code
and
that you need more time.
How much time will this be? Estimated work-60 hours

60 hours / hours you can work per day on this project

= number of days in the future your project will be finished.

We don't know you and your character. You might be a person that was just naive
under-estimating how much time it takes to realise such a project
or
you might be a person that is lazy trying to put as much work as possible on the shoulders of other people.

You should create a picture of you as a person that is willing to work hard by
presenting a first working example-code that does simply show the characters "DHT-humidity"
on the LC-Display
Then trying yourself to add reading in the DHT-sensor
and posting this sketch
combined with a specific question if something does not work.

Of course you are allowed to google for example-code that combine an LCD with a DHT-sensor

best regards Stefan

If you look at the reply there's a little circle in the upper right by the post number. That shows who is being replied to. If you click on it, then it shows what post is being replied to.