LED's aren't turning on for some reason.

Here’s my code.

const int blue = 2;
const int green = 3;
const int red = 4;
const int thermister = A5;
int val;
int fahrenheit;
int counter = 1;

void setup() { Serial.begin(9600);
    pinMode(blue, OUTPUT);
    pinMode(green, OUTPUT);
    pinMode(red, OUTPUT);
}
void loop() { 
  counter++;
  delay(1000);
    val = analogRead(thermister);
    fahrenheit = (val * 2) + 11;
    if (counter > 10 ){
      
    
    Serial.print(fahrenheit);
    Serial.println(" Degrees Fahrenheit");
    counter = 1;
    
    
    if (fahrenheit <= 50 ){
      digitalWrite(blue, HIGH);
      digitalWrite(red, LOW);
      digitalWrite(green, LOW);
      
      if (fahrenheit > 50 && fahrenheit <= 70){
        digitalWrite(blue, LOW);
      digitalWrite(red, LOW);
      digitalWrite(green, HIGH);
        
        if (fahrenheit > 70){
          digitalWrite(red, HIGH);
          digitalWrite(green, LOW);
          digitalWrite(blue, LOW);
          
          }
        }
      }
    }
}

The code is basically just giving a temperature range and turning either a red, green, or blue LED on depending on the temperature. I kinda just made up the formula to find the temperature because it worked with what I know it is in my house. The Serial monitor is reading the correct temperature, but the LED’s aren’t turning on. I know that they work, so what do you think the problem is? Thanks.

You need to set the pinModes for the LEDs to OUTPUT in setup().

groundFungus:
You need to set the pinModes for the LEDs to OUTPUT in setup().

I've been doing this for about a year now and I'm still stupid enough to make that mistake. I'm disappointed in myself. Thank you.

groundFungus:
You need to set the pinModes for the LEDs to OUTPUT in setup().

I just did it and it still isn't working.

if (fahrenheit < 0 && fahrenheit <= 50 ){

Shouldn’t that be greater than 0?

What temperature is printed?

groundFungus:

if (fahrenheit < 0 && fahrenheit <= 50 ){

Shouldn’t that be greater than 0?

What temperature is printed?

It’s reading 68 right now. And I wanted it to be anything less than zero degrees or up to 50 degrees for the blue led to turn on.

And I wanted it to be anything less than zero degrees or up to 50 degrees for the blue led to turn on.

So just use less than 50 and forget the other part of the if because it is covered.

Grumpy_Mike:
So just use less than 50 and forget the other part of the if because it is covered.

Yeah I just realized that and fixed it. The issue of the LED's still remains.

That code says if the temperature is not greater than 70 then turn all the LEDs off. It is the else part of that last if statement.
Also note that

if (fahrenheit <= 50 ){
      digitalWrite(blue, HIGH);
      digitalWrite(red, LOW);
      digitalWrite(green, LOW);
      
      if (fahrenheit > 50 && fahrenheit <= 70){

That second if will always fail because it never gets to see that line unless your temperature is lower than 50 and so it can never trigger.

Grumpy_Mike:
That code says if the temperature is not greater than 70 then turn all the LEDs off. It is the else part of that last if statement.
Also note that

if (fahrenheit <= 50 ){

digitalWrite(blue, HIGH);
      digitalWrite(red, LOW);
      digitalWrite(green, LOW);
     
      if (fahrenheit > 50 && fahrenheit <= 70){



That second if will always fail because it never gets to see that line unless your temperature is lower than 50 and so it can never trigger.

I removed the else part but the LED’s still aren’t turning on.

Read post #8 again.

It is also a problem with how you have your braces.

Hint, put your cursor on an opening brace and you will see the closing brace highlighted.

Most of your code is being skipped.

Each if statement needs to have a start brace and an end brace BEFORE the next of your if statements.
Otherwise the first if is the only one that gets actioned.

Now removing posts is not very nice. This forum is also for others who are having the same problem.

Hi,
Have you had any instance with the code where the LEDs, anyone of them has come ON?

If not, write a simple code that turns them ON. Forget about the temperature stuff, write a fresh bit of code to prove your circuit.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile: