Go Down

Topic: Using IF statement with 2 LEDs (Read 170 times) previous topic - next topic

almutama9

I'm trying to have 1 of the LEDs light up when the condition is true, and turn off and light the other LED if it's false
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int Blue  = 8;
int Red = 9;
const int sensorPin = A0;
int reading;
float temperatureC;
void setup()
{
lcd.begin(16, 2);
pinMode(Blue, OUTPUT);
digitalWrite(Blue, LOW);
pinMode(Red, OUTPUT);
digitalWrite(Red, LOW);


pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop()
{ reading = analogRead(sensorPin);
temperatureC = (5.0 * reading* 100.0) / 1024;
Serial.print(temperatureC);
if (temperatureC >= 20){
digitalWrite(Red, HIGH);
digitalWrite(Blue, LOW);
}
else
{
  digitalWrite(Blue, HIGH);
  digitalWrite(Red, LOW);
}
lcd.setCursor(0, 0);
lcd.print("TEMP: ");
lcd.print(temperatureC);
lcd.print(" C");
delay(500);
}


Both LEDs turn on when the temperature is less than 20, and both turn off when its more than 20.

septillion

#1
Apr 25, 2018, 11:55 am Last Edit: Apr 25, 2018, 12:46 pm by septillion
Haveyoueverheartoflayout?Indentation?

pres ctrl+T and see how that looks :)

Next, how did you connect those LEDs? I see nothing wrong (at least not with that) in the code.

After some rework:
Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte BlueLedPin = 8;
const byte RedLedPin = 9;
const byte SensorPin = A0;


void setup(){
  lcd.begin(16, 2);
  pinMode(BlueLedPin, OUTPUT);
  //digitalWrite(Blue, LOW); //the default
  pinMode(RedLedPin, OUTPUT);
  //digitalWrite(Red, LOW); //the default

  //pinMode(sensorPin, INPUT); //only applies to the digital mode
  Serial.begin(9600);
}
void loop(){
  unsigned int reading = analogRead(sensorPin);
  float temperatureC = (5.0 * reading * 100.0) / 1024;
  Serial.print(temperatureC);
 
  if (temperatureC >= 20) {
    digitalWrite(RedLedPin, HIGH);
    digitalWrite(BlueLedPin, LOW);
  }
  else{
    digitalWrite(BlueLedPin, HIGH);
    digitalWrite(RedLedPin, LOW);
  }
 
  lcd.setCursor(0, 0);
  lcd.print("TEMP: ");
  lcd.print(temperatureC);
  lcd.print(" C");
  delay(500);
}


Also note, a float does NOT simply mean a decimal point and is pretty hard for a Uno to do.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

almutama9

I connected longer part of the LED to a resistor, then the pin, and the shorter part to the ground. The rework still has the same problem. Thanks for helping out btw.

septillion

The rework had nothing to do with the problem :)

But at all times, only one of the two leds is lit?
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Go Up