Hi, im a first time arduino user, thats not the issue, the lcd and alarms are working fine,the problem is that i believe the green led is not

            `Use code tags to format code for the forum`                                                                                 

/* Temperature humidity sensor */

#include <DHT.h> // Includes the DHT sensor library.

#define datapin 10 // Digital pin connected to

#define DHTTYPE DHT22

DHT dht(datapin, DHTTYPE); // Creates the dht object.

const int Fan = 8;//sets the pin number const int gen_alarm = 9;// sets the pin number
const int LOW_DC_ALARM = 5;//sets the pin number
const int HIGH_DC_ALARM = 4;//sets the pin number
const int CHARG_FAIL_ALARM = 3;//sets the pin number
const int AC_POWER_ALARM = 2;//sets the pin number
int low_dc = 0;//sets state of the switch
int high_dc = 0;//sets state of the switch
int ch_fail = 0;//sets state of the switch
int ac_power = 0;//sets state of the switch dht.begin(); // Initializing the DHT22 sensor module.
pinMode(Fan, OUTPUT);//sets the mode of the pin
digitalWrite(Fan, LOW);//sets the state of the pin

pinMode(gen_alarm, OUTPUT);//sets the mode of the pin
digitalWrite(gen_alarm, LOW);//sets the state of the pin
Serial.begin(9600); // Initialize serial communication
pinMode(LOW_DC_ALARM, INPUT);//sets the mode of the pin
pinMode(HIGH_DC_ALARM , INPUT);//sets the mode of the pin
pinMode(CHARG_FAIL_ALARM, INPUT);//sets the mode of the pin
pinMode(AC_POWER_ALARM, INPUT);//sets the mode of the pin /* Find Temperature & Humidity */
float air_temp = dht.readTemperature();
float humidity = dht.readHumidity();
if(air_temp >= 29,9){
digitalWrite(Fan, HIGH);// the fan switches on
delay(1000);//delay of 1 second
}
if(air_temp <= 26,5){
digitalWrite(Fan, LOW);//It turns the fan off

}

low_dc = digitalRead(LOW_DC_ALARM);// It reads the state of the pin
high_dc = digitalRead(HIGH_DC_ALARM);// It reads the state of the pin
ch_fail = digitalRead(CHARG_FAIL_ALARM);// It reads the state of the pin
ac_power = digitalRead(AC_POWER_ALARM);// It reads the state of the pin

if(low_dc == LOW){
digitalWrite(gen_alarm, HIGH);// HIGH gen_alarm output Pin(switches on the gen_alarm)
lcd.setCursor(11, 2);// Set Cursor position
lcd.print("LOW DC");// print warning message on display
delay(1000);//delay time of 1 second
}else{
delay(1000);//delay time of 1 second
digitalWrite(gen_alarm, LOW);//LOW gen_alarm output Pin(switches off the gen_alarm)
lcd.setCursor(11,2);//set cursor position
lcd.print("No alarm");//print message on the LCD screen
}

if( high_dc == LOW){
digitalWrite(gen_alarm, HIGH);// HIGH gen_alarm output Pin(switches on the gen_alarm)
lcd.setCursor(11, 2);// Set Cursor position
lcd.print("HIGH DC");// print warning message on display
delay(1000);//delay time of 1 second
}else{
delay(1000);//delay time of 1 second
digitalWrite(gen_alarm, LOW);//LOW gen_alarm output Pin(switches off the gen_alarm)
lcd.setCursor(11,2);//set cursor position
lcd.print("No alarm");//print message on the LCD screen
}

if( ch_fail == LOW){
digitalWrite(gen_alarm, HIGH);// HIGH gen_alarm output Pin(switches on the gen_alarm)
lcd.setCursor(11, 2);// Set Cursor position
lcd.print("CH FAIL");// print warning message on display
delay(1000);//delay time of 1 seconds
}else{
delay(1000);//delay time of 1 second
digitalWrite(gen_alarm, LOW);//LOW gen_alarm output Pin(switches off the gen_alarm)
lcd.setCursor(11,2);//set cursor position
lcd.print("No alarm");//print message on the LCD screen
}

if( ac_power == LOW){
digitalWrite(gen_alarm, HIGH);// HIGH gen_alarm output Pin(switches on the gen_alarm)
lcd.setCursor(11, 2);// Set Cursor position
lcd.print("AC POWER");// print warning message on display
delay(1000);//delay time of 1 second
}else{
delay(1000);//delay time of 1 second
lcd.setCursor(11,2);//set cursor position
digitalWrite(gen_alarm, LOW);//LOW gen_alarm output Pin(switches off the gen_alarm)
lcd.print("No alarm");//print message on the LCD screen
}
delay(1000);//delay time of 1 second

  • There are posting guidelines which you failed to read.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

this is a battery charger monitoring system, i have an issue with the fan related to the temperature sensor, it should turn on at specific threshold, but instead it somehow reacts only when the alarms are not being displayed on the lcd due to a delay, i cant seem to find an option to upload a picture of the diagram presented by the lecturer

const int batteryPin = A15;                                      // Analog input pin for battery voltage
const int temperaturePin = 10;                                   // Digital pin for DS18B20 sensor
const float resistorDividerRatio = 22.0;                         // Voltage divider ratio (R2 / (R1 + R2))
const float referenceVoltage = 5.0;                              // Arduino's reference voltage (usually 5.0V)
const int currentPin = A14;                                      // Anolog input pin for current
const float currentDividerRatio = 4.0;                           //Current divider ratio(R2/(R1+R2))
#include <LiquidCrystal.h>                                       // Includes the LCD display library.
const int rs = 49, en = 45, d4 = 35, d5 = 33, d6 = 31, d7 = 29;  // Assigns all the pins to create the lcd object.
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);                       // Creates the lcd object.

/* Temperature humidity sensor */
#include <DHT.h>    // Includes the DHT sensor library.
#define datapin 10  // Digital pin connected to
#define DHTTYPE DHT22
DHT dht(datapin, DHTTYPE);  // Creates the dht object.
const int Fan = 8;          //sets the pin number


const int gen_alarm = 9;         // sets the pin number
const int LOW_DC_ALARM = 5;      //sets the pin number
const int HIGH_DC_ALARM = 4;     //sets the pin number
const int CHARG_FAIL_ALARM = 3;  //sets the pin number
const int AC_POWER_ALARM = 2;    //sets the pin number
int low_dc = 0;                  //sets state of the switch
int high_dc = 0;                 //sets state of the switch
int ch_fail = 0;                 //sets state of the switch
int ac_power = 0;                //sets state of the switch

void setup() {
  // Setting Up the Liquid Crystal Pins
  pinMode(53, OUTPUT);    //Vdd
  pinMode(51, OUTPUT);    //potentiometer
  pinMode(47, OUTPUT);    //RW
  pinMode(43, OUTPUT);    //D0
  pinMode(41, OUTPUT);    //D1
  pinMode(37, OUTPUT),    //D3
    pinMode(27, OUTPUT);  //CONTRAST A
  pinMode(25, OUTPUT);    //CONTRAST K

  digitalWrite(53, HIGH);  //the pin State will be set to HIGH all the times.
  digitalWrite(51, LOW);   //the pin State will be set to LOW all the times.
  digitalWrite(47, LOW);   //the pin State will be set to LOW all the times.
  digitalWrite(43, HIGH);  //the pin State will be set to HIGH all the times.
  digitalWrite(41, HIGH);  //the pin State will be set to HIGH all the times.
  digitalWrite(37, HIGH);  //the pin State will be set to HIGH all the times.
  digitalWrite(27, HIGH);  //the pin State will be set to HIGH all the times.
  digitalWrite(25, LOW);   //the pin State will be set to LOW all the times.

  //Setting Up the Display Screen and Initializing the Programme
  lcd.begin(20, 4);           // Initializing the LCD display screen.
  lcd.setCursor(3, 1);        // Sets the cursor positon on the LCD screen.
  lcd.print(" WELCOME TO ");  // Prints a message on the LCD screen.
  delay(2000);                // Time delay for 2 seconds.
  lcd.clear();                // clears the dislay screen messages and settings.

  lcd.setCursor(2, 0);             // Sets the cursor positon on the LCD screen.
  lcd.print("BATTERY CHARGER");    // Prints a message on the LCD screen.
  lcd.setCursor(2, 1);             // Sets the cursor positon on the LCD screen.
  lcd.print("MONITORING SYSTEM");  // Prints a message on the LCD screen.
  delay(3000);                     // Time delay for 3 seconds.
  lcd.clear();                     // clears the dislay screen messages and settings.

  lcd.setCursor(3, 1);         // Sets the cursor positon on the LCD screen.
  lcd.print("BY: M MAHLARE");  // Prints a message on the LCD screen.
  delay(3000);                 // Time delay of 3 seconds.
  lcd.clear();                 // clears the dislay screen messages and settings.


  dht.begin();             // Initializing the DHT22 sensor module.
  pinMode(Fan, OUTPUT);    //sets the mode of the pin
  digitalWrite(Fan, LOW);  //sets the state of the pin


  pinMode(gen_alarm, OUTPUT);        //sets the mode of the pin
  digitalWrite(gen_alarm, LOW);      //sets the state of the pin
  Serial.begin(9600);                // Initialize serial communication
  pinMode(LOW_DC_ALARM, INPUT);      //sets the mode of the pin
  pinMode(HIGH_DC_ALARM, INPUT);     //sets the mode of the pin
  pinMode(CHARG_FAIL_ALARM, INPUT);  //sets the mode of the pin
  pinMode(AC_POWER_ALARM, INPUT);    //sets the mode of the pin
}

void loop() {
  // Read the battery voltage
  int rawValue = analogRead(batteryPin);
  float voltage = (rawValue / 1023.0) * referenceVoltage * resistorDividerRatio;
  // read the value of current
  int rawCurrent = analogRead(currentPin);
  // Convert the voltageto current. Since we're using a potentiometer to simulate 0 - 5V as 0 - 20A
  //We can simply map the voltage to current.
  float current = (rawCurrent / 1023.0) * referenceVoltage * currentDividerRatio;

  /* Find Temperature & Humidity */
  float air_temp = dht.readTemperature();
  float humidity = dht.readHumidity();

  lcd.clear();            // clears the dislay screen messages and settings.
  lcd.setCursor(0, 0);    // Sets the cursor positon on the LCD screen.
  lcd.print("Battery:");  // Prints a message on the LCD screen.
  lcd.setCursor(0, 1);    // Sets the cursor positon on the LCD screen.
  lcd.print("Temp:");     // Prints a message on the LCD screen.
  lcd.setCursor(0, 2);    //sets the cursor position on LCD the screen
  lcd.print("alarms:");   //print a message on the LCD screen
  lcd.setCursor(0, 3);    //sets the cursor position on the LCD screen
  lcd.print("current:");  //print a message on the LCD screen

  // Display battery voltage, current and temperature on the LCD
  lcd.setCursor(8, 0);     // Sets the cursor positon on the LCD screen.
  lcd.print(voltage, 2);   // Display with 2 decimal places
  lcd.setCursor(5, 1);     // Sets the cursor positon on the LCD screen.
  lcd.print(air_temp, 2);  // Display with 2 decimal places
  lcd.setCursor(11, 3);    //sets the cursor position on the LCD screen
  lcd.print(current, 2);   // display with two decimal places


  if (air_temp >= 29, 9) {
    digitalWrite(Fan, HIGH);  // the fan switches on
    delay(1000);              //delay of 1 second
  }
  if (air_temp <= 26, 5) {
    digitalWrite(Fan, LOW);  //It turns the fan off
  }

  low_dc = digitalRead(LOW_DC_ALARM);       // It reads the state of the pin
  high_dc = digitalRead(HIGH_DC_ALARM);     // It reads the state of the pin
  ch_fail = digitalRead(CHARG_FAIL_ALARM);  // It reads the state of the pin
  ac_power = digitalRead(AC_POWER_ALARM);   // It reads the state of the pin

  if (low_dc == LOW) {
    digitalWrite(gen_alarm, HIGH);  // HIGH gen_alarm output Pin(switches on the gen_alarm)
    lcd.setCursor(11, 2);           // Set Cursor position
    lcd.print("LOW DC");            // print warning message on display
    delay(1000);                    //delay time of 1 second
  } else {
    delay(1000);                   //delay time of 1 second
    digitalWrite(gen_alarm, LOW);  //LOW gen_alarm output Pin(switches off the gen_alarm)
    lcd.setCursor(11, 2);          //set cursor position
    lcd.print("No alarm");         //print message on the LCD screen
  }

  if (high_dc == LOW) {
    digitalWrite(gen_alarm, HIGH);  // HIGH gen_alarm output Pin(switches on the gen_alarm)
    lcd.setCursor(11, 2);           // Set Cursor position
    lcd.print("HIGH DC");           // print warning message on display
    delay(1000);                    //delay time of 1 second
  } else {
    delay(1000);                   //delay time of 1 second
    digitalWrite(gen_alarm, LOW);  //LOW gen_alarm output Pin(switches off the gen_alarm)
    lcd.setCursor(11, 2);          //set cursor position
    lcd.print("No alarm");         //print message on the LCD screen
  }

  if (ch_fail == LOW) {
    digitalWrite(gen_alarm, HIGH);  // HIGH gen_alarm output Pin(switches on the gen_alarm)
    lcd.setCursor(11, 2);           // Set Cursor position
    lcd.print("CH FAIL");           // print warning message on display
    delay(1000);                    //delay time of 1 seconds
  } else {
    delay(1000);                   //delay time of 1 second
    digitalWrite(gen_alarm, LOW);  //LOW gen_alarm output Pin(switches off the gen_alarm)
    lcd.setCursor(11, 2);          //set cursor position
    lcd.print("No alarm");         //print message on the LCD screen
  }

  if (ac_power == LOW) {
    digitalWrite(gen_alarm, HIGH);  // HIGH gen_alarm output Pin(switches on the gen_alarm)
    lcd.setCursor(11, 2);           // Set Cursor position
    lcd.print("AC POWER");          // print warning message on display
    delay(1000);                    //delay time of 1 second
  } else {
    delay(1000);                   //delay time of 1 second
    lcd.setCursor(11, 2);          //set cursor position
    digitalWrite(gen_alarm, LOW);  //LOW gen_alarm output Pin(switches off the gen_alarm)
    lcd.print("No alarm");         //print message on the LCD screen
  }
  delay(1000);  //delay time of 1 second
}

  • Where did you see this syntax ?

if (air_temp >= 29, 9) 

I was just exploring my options, did i go about the wrong way?

  • The above is meaningless, please read the link offered you on using if( . . . )

okay, I used the link right, the led is now on even when temperature has not reached its specified threshold, and not turning off, even when I used the else statement for turning it off,

  • We need to see your changes to these lines.
  if (air_temp >= 29, 9) {
    digitalWrite(Fan, HIGH);  // the fan switches on
    delay(1000);              //delay of 1 second
  }
  if (air_temp <= 26, 5) {
    digitalWrite(Fan, LOW);  //It turns the fan off
  }

if(air_temp >= 29,9){digitalWrite(Fan, HIGH);}// the fan switches on

  • Exactly what do you want to do above ?

  • An here also ?
    if (air_temp <= 26, 5)

turn the green led on, when the threshold reaches 29,9

and turn it off when it gets to 26,6

  • Use a decimal point.

THANK YOU, it's working

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.