SENSORS ON ANALOG PINS IN UNO

Hi friends,

I have a FSR [Force Sensitive Resistor] connected to analog pin 3,
A LM35 temperature sensor connected to analog pin 0,
and a LED connected to digital pin 2,

Objective : I want to use both the sensors and light up the LED whenever the values from the sensors cross a defined value.

I wrote a code for the LM35, using the same setup, I get satisfactory results.

But after writing a code for both the sensors and checking the output, I'm getting 50% of output, i.e., when I press the FSR the LED lights up correctly but when I hold the LM35 to increase the temperature after the set threshold the LED lights up but only partially, its dim.

I'm really confused, please HELP ME!!

Here's the actual code, please let me know what changes have to be made to attain the Objective

const int  fsrAnalogPin = A3; 
const int LM35 = A0; // analog pin
int  fsrReading;  
int LM35Reading;
const int LED = 2;

void setup()
{
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  pinMode(LM35,INPUT);
  pinMode(fsrAnalogPin,INPUT);
}
void loop()
{
  
  fsrReading = analogRead(fsrAnalogPin);      // Store the readings from the FSR into the variable
  LM35Reading = analogRead(LM35);
 
  if(fsrReading > 500)                        // Set the threshold value
    {
      digitalWrite(LED,  HIGH);            // Activate LED   
    }
  
  else
  {
     digitalWrite(LED,  LOW);        // Deactivate the LEDs
     delay(100);
                             
  }
  

if(LM35Reading >= 60)  //value coming from the LM35
  {
    digitalWrite(LED,HIGH);
  }
else
  {
    digitalWrite(LED, LOW);
  }
  
}
const int  fsrAnalogPin = A3; 
const int LM35 = A0; // analog pin

Use of Pin in all names that refer to pin numbers is good.

  Serial.begin(9600);

But, no Serial.print() statements. Why not?

How do you KNOW what value you are reading from the analog pins?

One block of code has a delay() in one branch. The other does not. I think you might see different results if all 4 branches had delay() calls.

i separated the output of the two LEDs, taking one to the onboard LED on pin13. Just to debug

moved the delay so you can see what's outputting

then added some Serial.print() commands so you can see what your sensors are reporting

I changed the transition values (hysteresis)

untested:

const int  fsrAnalogPin = A3; 
const int LM35 = A0; // analog pin
int  fsrReading;  
int LM35Reading;
const int LED = 2;
const int LED2 = 13;//onboard led

void setup()
{
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  pinMode(LM35,INPUT);
  pinMode(fsrAnalogPin,INPUT);
}
void loop()
{
  fsrReading = analogRead(fsrAnalogPin);      // Store the readings from the FSR into the variable
  Serial.print("FSR reading: ");
  Serial.println(fsrReading);
  if(fsrReading > 500)                        // Set the threshold value
  {
    digitalWrite(LED,  HIGH);            // Activate LED   
  }
  else if (LM35Reading < 490)//some hysteresis
  {
    digitalWrite(LED,  LOW);        // Deactivate the LEDs                       
  }
  //
  LM35Reading = analogRead(LM35);
  Serial.print("LM35 reading: ");
  Serial.println(LM35Reading);
  if(LM35Reading >= 60)  //value coming from the LM35
  {
    digitalWrite(LED2,HIGH);
  }
  else if (LM35Reading < 55) //some hysteresis
  {
    digitalWrite(LED2, LOW);
  }
  delay(100);
}

I suspect it's switching the LED on and off again too soon and so looking like it's only half-on.

BL's idea of using 2x LEDs makes sense: it makes no sense at all to me to use 1x LED when you're checking 2x independent inputs more or less at the same time.

@BulldogLowell

Thanks for the reply, but yet the problem is unsolved...
the same problem persists...

@ JimboZA

No my idea is to trigger the same LED from both of the sensors, its done for a specific purpose as an alarm...

shriramstrong:
@BulldogLowell

Thanks for the reply, but yet the problem is unsolved...
the same problem persists...

perhaps you could share the output from the sketch I took the time to help you with?

and you need to separate the outputs for debug, else you may be chasing your tail for a long time.

shriramstrong:
No my idea is to trigger the same LED from both of the sensors, its done for a specific purpose as an alarm...

Then you should perhaps put them in one "if":

pseudo-code:
if (sensor a over threshold) and (sensor b over threshold)
{
.... alarm
}

JimboZA:

shriramstrong:
@ JimboZA

No my idea is to trigger the same LED from both of the sensors, its done for a specific purpose as an alarm...

where is PaulS when you really need/want him?!?

You need to decide if the LED is to light when both conditions are true (use &&)
or if one or more are true (use ||).

I suspect you want to have the LED notify you of either condition? If so
use || (or).

void loop()
{
  fsrReading = analogRead(fsrAnalogPin);
  LM35Reading = analogRead(LM35);
 
  if (fsrReading > 500 || LM35Reading >= 60) 
  {
      digitalWrite(LED,  HIGH);            // Activate LED   
  }
  else
  {
     digitalWrite(LED,  LOW);        // Deactivate the LEDs
     delay(100);                           
  }
  
}

where is PaulS when you really need/want him?!?

Re-roofing a shed.