LED sequence not working; temperature related

Hello,

I’m having a bit of trouble getting 3 LEDs to light up in accordance to the temp. I’ve got 3 LEDs, and three temp ranges, which will be adjusted once this has been set up, these being 7, 8 and 9.

The low (7) and medium (8) temp works but the higher temp (9) never does. I’ve tried the code a variety of ways, use different else if/else comments but it never lights up, the middle LED (8) remains on instead of switching.

My code is below. This is from a project online which I’m using for experience and then plan to modify as I go along.

Thanks in advance

#include <LiquidCrystal.h>
#include <DHT.h>

#include "DHT.h"

#define DHTPIN A0     // what pin we're connected to

#define DHTTYPE DHT22   // DHT 22


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
  for (int DigitalPin = 7; DigitalPin <= 9; DigitalPin++) 
 {
  pinMode(DigitalPin, OUTPUT);
 }
  lcd.begin(16,2); //16 by 2 character display
  
dht.begin();
}
 
void loop()
{
  delay(1000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();


  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");   
  //lcd.print(t);
  lcd.print(t,1);
  lcd.print("\337C");
  
  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  //lcd.print(h);
  lcd.print(h,1);
  lcd.print("%");
  
  //the 3-led setup process
  if (t<=22)
  {
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>23)
  {
  digitalWrite(8, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>=25)
  {
  digitalWrite(9, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  } 
  
}

"t>23" satifies the condition of t>23 and t>=25 so the t>=25 condition never gets checked.

Tweak your tests a bit.

Blackfin:
"t>23" satifies the condition of t>23 and t>=25 so the t>=25 condition never gets checked.

Tweak your tests a bit.

Ah okay! That makes sense. I've used a bunch of different temps, bigger ranges between each one but no matter what, it never changes.

Should I adjust the code? I know this is incredibly basic but I'm going around in circles

Is it anything to do with this?

void setup()
{
  Serial.begin(9600);
  for (int DigitalPin = 7; DigitalPin <= 9; DigitalPin++) 
 {

exiledyorkie:
Ah okay! That makes sense. I’ve used a bunch of different temps, bigger ranges between each one but no matter what, it never changes.

Should I adjust the code? I know this is incredibly basic but I’m going around in circles

Try this:

.
.
.
  //the 3-led setup process
  if (t<=22)
  {
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>22 && t<25)
  {
  digitalWrite(8, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>=25)
  {
  digitalWrite(9, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  } 
.
.
.

Your pin initialization is fine though a bit unorthodox. It can be confusing for you (or someone else) to look at your code and see non-descriptive things like digitalWrite(9,HIGH).

Consider naming the pins reasonably descriptively:

#define NUM_LEDS        3
const byte pinLED_LOW = 7;
const byte pinLED_MED = 8;
const byte pinLED_HIGH = 9;

You can put those names into an array:

const byte pinArray[NUM_LEDS] = 
{
    pinLED_LOW,
    pinLED_MED,
    pinLED_HIGH
};

and then use that to set up the pinModes, like you did already but a bit more clearly:

    for( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );

Then, when assigning the states to the LEDs, use their descriptive names:

    //the 3-led setup process
    if (t<=22)
    {
        digitalWrite(pinLED_LOW, HIGH);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, LOW);
    }
    else if (t>22 && t<25)
    {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, HIGH);
        digitalWrite(pinLED_HIGH, LOW);
    }
    else if (t>=25)
    {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, HIGH);
    }//

You could get cocky and use a feature of C to let you do the conditional for each LED in one line:

    digitalWrite(pinLED_LOW, (t<=22)?HIGH:LOW );
    digitalWrite(pinLED_MED, (t>22 && t<25)?HIGH:LOW );
    digitalWrite(pinLED_HIGH, (t>=25)?HIGH:LOW );

Blackfin:
Try this:

.

.
.
  //the 3-led setup process
  if (t<=22)
  {
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>22 && t<25)
  {
  digitalWrite(8, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(9, LOW);
  }
  else if (t>=25)
  {
  digitalWrite(9, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  }
.
.
.





Your pin initialization is fine though a bit unorthodox. It can be confusing for you (or someone else) to look at your code and see non-descriptive things like digitalWrite(9,HIGH).

Consider naming the pins reasonably descriptively:



#define NUM_LEDS        3
const byte pinLED_LOW = 7;
const byte pinLED_MED = 8;
const byte pinLED_HIGH = 9;




You can put those names into an array:


const byte pinArray[NUM_LEDS] =
{
    pinLED_LOW,
    pinLED_MED,
    pinLED_HIGH
};




and then use that to set up the pinModes, like you did already but a bit more clearly:



for( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );




Then, when assigning the states to the LEDs, use their descriptive names:



//the 3-led setup process
    if (t<=22)
    {
        digitalWrite(pinLED_LOW, HIGH);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, LOW);
    }
    else if (t>22 && t<25)
    {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, HIGH);
        digitalWrite(pinLED_HIGH, LOW);
    }
    else if (t>=25)
    {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, HIGH);
    }//




You could get cocky and use a feature of C to let you do the conditional for each LED in one line:



digitalWrite(pinLED_LOW, (t<=22)?HIGH:LOW );
    digitalWrite(pinLED_MED, (t>22 && t<25)?HIGH:LOW );
    digitalWrite(pinLED_HIGH, (t>=25)?HIGH:LOW );

Thank you so much for the in-depth response. That has helped massively.

I’ve just changed the code as you have suggested and it worked perfectly. Looking at it, now it makes perfect sense, as I did not set the correct boundaries. A learning curve there.

Thank you for the extra advice with naming the leds etc. Now I know it works I will go forward with that, as I did find it a little confusing to follow. But reading some code is beginning to get a bit easier.

Again, thank you and I’ll give that a crack now.

    for( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );

I’ve copied the code across and I get the following error for the code above. No idea with this one. I get this error

Arduino: 1.8.8 (Windows 10), Board: “Arduino/Genuino Uno”

Temp_Humidity_Sensor_LCD_Display_with_LED_Indicator:24:5: error: expected unqualified-id before ‘for’

for ( int i=0; i<NUM_LEDS; i++ )

^

Temp_Humidity_Sensor_LCD_Display_with_LED_Indicator:24:20: error: ‘i’ does not name a type

for ( int i=0; i<NUM_LEDS; i++ )

^

Temp_Humidity_Sensor_LCD_Display_with_LED_Indicator:24:32: error: ‘i’ does not name a type

for ( int i=0; i<NUM_LEDS; i++ )

^

exit status 1
expected unqualified-id before ‘for’

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

Please post your code

AWOL:
Please post your code

My original is in the first post but here is the amended one. The three LEDs work accordingly now, thank you but I get an error displayed for the term ‘for’ but I can understand why.

#include <LiquidCrystal.h>
#include <DHT.h>

#include "DHT.h"

#define DHTPIN A0     // what pin we're connected to

#define DHTTYPE DHT22   // DHT 22


#define NUM_LEDS        3
const byte pinLED_LOW = 7;
const byte pinLED_MED = 8;
const byte pinLED_HIGH = 9;

const byte pinArray[NUM_LEDS] = 
{
    pinLED_LOW,
    pinLED_MED,
    pinLED_HIGH
};


    for ( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );






LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
  for (int DigitalPin = 7; DigitalPin <= 9; DigitalPin++) 
 {
  pinMode(DigitalPin, OUTPUT);
 }
  lcd.begin(16,2); //16 by 2 character display
  
dht.begin();
}
 
void loop()
{
  delay(1000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();


  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");   
  //lcd.print(t);
  lcd.print(t,1);
  lcd.print("\337C");
  
  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  //lcd.print(h);
  lcd.print(h,1);
  lcd.print("%");
  
  //the 3-led setup process
  if (t<=22)
  {
        digitalWrite(pinLED_LOW, HIGH);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>22 && t<25)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, HIGH);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>=25)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, HIGH);
    }// 
  
}

Move the for loop:

    for ( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );

into setup(). It replaces your existing loop:

for (int DigitalPin = 7; DigitalPin <= 9; DigitalPin++) 
 {
  pinMode(DigitalPin, OUTPUT);
 }

Blackfin:
Move the for loop:

    for ( int i=0; i<NUM_LEDS; i++ )

pinMode( pinArray[i], OUTPUT );




into setup(). It replaces your existing loop:



for (int DigitalPin = 7; DigitalPin <= 9; DigitalPin++)
{
  pinMode(DigitalPin, OUTPUT);
}

Apologies, I do not understand. Remove the original section of ‘DigitalPin’ ? as I’ve done what I thought you meant and it doesn’t work, so I’ve misunderstood.

Try this:

#include <LiquidCrystal.h>
#include <DHT.h>

#include "DHT.h"

#define DHTPIN A0     // what pin we're connected to

#define DHTTYPE DHT22   // DHT 22


#define NUM_LEDS        3
const byte pinLED_LOW = 7;
const byte pinLED_MED = 8;
const byte pinLED_HIGH = 9;

const byte pinArray[NUM_LEDS] = 
{
    pinLED_LOW,
    pinLED_MED,
    pinLED_HIGH
};

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
    Serial.begin(9600);
    for ( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );
    
    lcd.begin(16,2); //16 by 2 character display
  
    dht.begin();
}
 
void loop()
{
  delay(1000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();


  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");   
  //lcd.print(t);
  lcd.print(t,1);
  lcd.print("\337C");
  
  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  //lcd.print(h);
  lcd.print(h,1);
  lcd.print("%");
  
  //the 3-led setup process
  if (t<=22)
  {
        digitalWrite(pinLED_LOW, HIGH);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>22 && t<25)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, HIGH);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>=25)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, HIGH);
    }// 
  
}

Blackfin:
Try this:

#include <LiquidCrystal.h>

#include <DHT.h>

#include “DHT.h”

#define DHTPIN A0    // what pin we’re connected to

#define DHTTYPE DHT22  // DHT 22

#define NUM_LEDS        3
const byte pinLED_LOW = 7;
const byte pinLED_MED = 8;
const byte pinLED_HIGH = 9;

const byte pinArray[NUM_LEDS] =
{
    pinLED_LOW,
    pinLED_MED,
    pinLED_HIGH
};

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
    Serial.begin(9600);
    for ( int i=0; i<NUM_LEDS; i++ )
        pinMode( pinArray[i], OUTPUT );
   
    lcd.begin(16,2); //16 by 2 character display
 
    dht.begin();
}

void loop()
{
  delay(1000);
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(“Temp: “); 
  //lcd.print(t);
  lcd.print(t,1);
  lcd.print(”\337C”);
 
  lcd.setCursor(0,1);
  lcd.print(“Humid: “);
  //lcd.print(h);
  lcd.print(h,1);
  lcd.print(”%”);
 
  //the 3-led setup process
  if (t<=22)
  {
        digitalWrite(pinLED_LOW, HIGH);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>22 && t<25)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, HIGH);
        digitalWrite(pinLED_HIGH, LOW);
  }
  else if (t>=25)
  {
        digitalWrite(pinLED_LOW, LOW);
        digitalWrite(pinLED_MED, LOW);
        digitalWrite(pinLED_HIGH, HIGH);
    }//
 
}

Again, thank you! That worked. I just required some spoon feeding. Brilliant!