Green LED is just turning not slowly turning on

#define LM35 A2
#define RED 4
#define GREEN 2

float lm_value;
float tempc;
int brightness = 0;

void setup() {
    Serial.begin(9600);
    pinMode(RED, OUTPUT);
    pinMode(GREEN, OUTPUT);
}

void loop() {
    lm_value = analogRead(LM35);
    tempc = (lm_value * 500) / 1023;
    Serial.println(tempc);//Temperature in Celcius
    
    //Condition
    if (tempc > 25) {
        brightness = 0;
        digitalWrite(RED, HIGH);
        digitalWrite(GREEN, LOW);
    }
    else {
        brightness += 10;
        if(brightness < 255) {
        analogWrite(GREEN, brightness);
        }
        digitalWrite(RED, LOW);
    }
    delay(1000);
}

That is just one problem I am having right now, but my temp sensor is decresing and incresing with random values. Could it be that my sensor is broken?

Please post a schematic, just hand draw all you parts and how they are connected.

Please add three Serial.print() statements

  • the value directly from the analogRead()
  • something in the if statement if part and
  • something in the if statement else part

Just so we can see the number informing your decisions, and what branches are being followed.

a7

More likely, a connection is intermittent or broken, so you have a floating input. Double check all connections and jumper wires.

Also, best to tell the compiler you want floating point math.

tempc = (lm_value * 500) / 1023.0;


Is pin 2 a PWM pin on your unnamed platform?

@ocha29

To see that the brightness of the Green LED changes as the temperature changes, connect the LED at a PWM pin (DPin-3) and include the following code in your sketch.

analogWrite(3, map(analogRead(A2), 0, 1023, 0, 255));
delay(500);

Does anyone in the real world actually do this with map()?

First year programming, actually like 2 months. If you were to write how would you do it?

Write it?
First, I'd read the documentation.
(Like, the documentation that says analogRead returns an int, and not a float )

Thank you! fixed my LED issue.

1 Like

1. analogRead() function returns int-type data; so, ..

int lm_value = analogRead(A2);

2. To convert lm_value of Step-1 into float-type temperature --

float tempC = 100*(5.0/1023)*lm_value;  //default  Vref = 5V of the ADC  

3. To extract the integer part of tempC of Step-2 --

int temperature = (int)tempC; //known as casting to convert data-type 

4. lm_value has the following range:

0x0000 - 0x03FF in hex base = 0 - 1023 in decimal base.

5. analogWrite(arg1, arg2) function expects that the value of arg2 should be a
positive value of the range: 0x00 - 0xFF = 0 - 255.

6. So, the value of Step-4 has to be compressed from --

0 - 1023 to 0 - 255

using map() function.

byte compressedValue = map(lm_value, 0, 1023, 0, 255);

7. Now, use the compressedValue in place of arg2 of the analogWrite() function of Step-5.

analogWrite(arg1, compressedValue);

8. If the Green LED is connected with DPin-3, then --

analogWrite(3, compressedValue);

9. We have these codes:

int lm_value = analogRead(A2);
byte compressedValue = map(lm_value, 0, 1023, 0, 255);
analogWrite(3, compressedValue);

10. The three lines of codes of Step-9 can be replaced by the following single line code known as nested form.

analogWrite(3, map(analogRead(A2), 0, 1023, 0, 255));

1. Does "do this" refer to changing the brightness of Green LED of OP?
2. Does "Does anyone" refer to a Forum Member?
3. Does "real world" refer to the Arduino Forum or Industry?
4. Does "actually" refer to lab practice or industrial application?

[soapbox]
I wouldn't do that to someone learning code.
Put each on its own line to show the process so the noob can understand it.

When I see it written like that, it turns me off, shows laziness in trying to teach something or I'm better at coding than you attitude.
Okay to use it if you know the noob already knows how to code without errors, then if that was the case the noob wouldn't be here.
It won't make the code run any faster, just slow down the reading of the code by us mortals.

GreenRaw = analogRead(GreenInputPin);
GreenLevelVal = map(GreenRaw, 0, 1023, 0, 255);
analogWrite(GREENLEDPin3, GreenLevelVal);

Also easier to trouble shoot your software.
[/soapbox]

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

That as well, but my comment was about using map to divide by four, badly.

1 Like

I understand that map() compresses the quantity; whereas, divide by four discards 3/4th of the quantity. This is the reason, I do not use division process though it works well.

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