Strange behaviour ; leds were working now they are not ???

Hi Folks,

Strange one this, I have a project that was working before I made some changes, i.e. changed a 16x2 for a 20x4 lcd.

now I cant remember if they stopped working when I changed lcd’s or after maybe I tweeked some code without registering.

heres my code see if you guru’s can spot anything I am missing?

any help / advise greatly appreciated

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 4 on the Arduino
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.

DeviceAddress redprobe = { 0x28, 0x47, 0xAF, 0xC8, 0x1A, 0x13, 0x01, 0x50 };
DeviceAddress blackprobe = { 0x28, 0xAA, 0x49, 0x65, 0x13, 0x13, 0x02, 0x2D };

#define redprobe_RED_LED   9
#define redprobe_GRN_LED   10
#define blackprobe_RED_LED   11
#define blackprobe_GRN_LED   12

#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int n = 1;

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

void setup(void)
{
  lcd.begin (20, 4);

  pinMode(redprobe_RED_LED, OUTPUT);
  pinMode(redprobe_GRN_LED, OUTPUT);
  pinMode(blackprobe_RED_LED, OUTPUT);
  pinMode(blackprobe_GRN_LED, OUTPUT);

  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home

  lcd.setCursor(1, 1);
  lcd.print("Environment Agency");
  lcd.setCursor(3, 2);
  lcd.print("Fish Husbandry");
  delay(4000);
  lcd.clear();
  lcd.setCursor(6, 1);
  lcd.print("Fetching");
  lcd.setCursor(4, 2);
  lcd.print("Temperatures");
  delay(2000);

  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(redprobe, 12);
  sensors.setResolution(blackprobe, 12);
}

void printTemperature(DeviceAddress deviceAddress)


{ float tempC = sensors.getTempC(redprobe);
  Serial.print(tempC);
  if (tempC < 20.0) {
    digitalWrite(redprobe_RED_LED, LOW);
    digitalWrite(redprobe_GRN_LED, HIGH);
  }
  else {
    digitalWrite(redprobe_RED_LED, HIGH);
    digitalWrite(redprobe_GRN_LED, LOW);
    delay(500);
    digitalWrite(redprobe_RED_LED, LOW);
    digitalWrite(redprobe_GRN_LED, LOW);
    delay(500);
  }
  {
    float tempC = sensors.getTempC(blackprobe);
    Serial.print(tempC);
    if (tempC < 20.0) {
      digitalWrite(blackprobe_RED_LED, LOW);
      digitalWrite(blackprobe_GRN_LED, HIGH);
    }
    else {
      digitalWrite(blackprobe_RED_LED, HIGH);
      digitalWrite(blackprobe_GRN_LED, LOW);
      delay(500);
      digitalWrite(blackprobe_RED_LED, LOW);
      digitalWrite(blackprobe_GRN_LED, LOW);
      delay(500);
    }
  }

  {
    float tempC = sensors.getTempC(deviceAddress);
    if (tempC == -127.00) {
      Serial.print("Error getting temperature");
    } else {
      Serial.print("C: ");
      Serial.print(tempC, 1);
      Serial.print(" F: ");
      Serial.print(DallasTemperature::toFahrenheit(tempC));
    }
  }
}
void loop(void)
{
  sensors.requestTemperatures();

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Red Air Line:");
  lcd.setCursor(18, 0);
  lcd.print((char)223);
  lcd.setCursor(19, 0);
  lcd.print("C");

  lcd.setCursor(0, 3);
  lcd.print("Blk Air Line:");
  lcd.setCursor(18, 3);
  lcd.print((char)223);
  lcd.setCursor(19, 3);
  lcd.print("C");
  lcd.setCursor(14, 0);
  lcd.print(sensors.getTempC(redprobe), 1);
  lcd.setCursor(14, 3);
  lcd.print(sensors.getTempC(blackprobe), 1);
  delay(500);
}
//  Serial.print("Getting temperatures...\n\r");
//  sensors.requestTemperatures();
//
//  Serial.print("Red Probe is: ");
//  printTemperature(redprobe);
//  Serial.print("\n\r");
//  Serial.print("Blk Probe is: ");
//  printTemperature(blackprobe);
//  Serial.print("\n\r");

Please perform an Auto-Format on your code and re-post it. There are many extraneous curly braces and the indentation is all wrong, making it difficult to read.

Also please explain what is wrong. Just saying "it stopped working" is too vague. We need to know what is happening now and what you wanted to happen.

It would help if you were to tell us more about what “not working” means. What is the code supposed to do? What does the code actually do?

If it is the LCD that is not working, are you sure that the I2C address and the LCD to I2C expander pin mapping are the same on the 20x4 as the 16x2?

My apologies,

it appears my mind is running faster than i can type hence missing crucial info.

I have two red / green LED's they are connected to pins 9,10,11,12

when the temp registers above 20c they turn red else green. they were working earlier but I then replaced 16x2 for 20x4 lcd. i didn't notice them off straight away hence not sure when it happened but def working before 20x4.

I have tried putting back the 16x2 but no change
I tested the pins on Arduino with multi meter and there doesn't appear to be any signal coming from them.

I've changedd the above post's code to the formatted as requested.

apologies again for my haste.

Using the 44780 library, and I2C, I have switched between 16x2 and 20x4 without problems. Of course, the sketch must only use the first 2 rows and the first 16 columns.

The LCD appears to be fine, and working as it should..

I don't see anything wrong with the code. What are the Serial prints telling you (correct temperatures)? Is what you expect to be displayed on the LCD displayed? Is it just that the LEDs are not working that is the issue?

Using both Serial.print and D0 as digital I/O, is that okey?

Railroader, if you are referring to this:

#define Rs_pin  0

That is pin 0 on the I2C expander chip.
(got me before, too).

Strangest thing!

I just un-commented the serial part of the code so as to check the readings (the lcd displays them correctly) and uploaded and

the LED's Started to work again. v strange, I wont look a gift horse in the mouth though.

Many Thanks to all whom took the time to read and think about my issue.

it all started with wanting to add text based on the same parameters mentioned above, i.e. "water is warm" on line 2 when temps get over 20c but I had no joy in making that work (more research needed as it should be able to be done. but that's for another topic.

Thanks again folks

@gF
Yes, D0 (Rx) action during Tx working… My guess.

@TonyG-bot. Code is under control, or going back to the previosly working code and make a new push but in smaller steps?

I just un-commented the serial part of the code so as to check the readings (the lcd displays them correctly) and uploaded and

the LED’s Started to work again. v strange, I wont look a gift horse in the mouth though.

The LED management is inside the function printTemperature() and that was commented out.

Why you have LED’s turned on and off inside a function called printTemperture() is a mystery. Very sloppy coding practice.

indeed! A case of running too quickly without looking around once and a while :slight_smile:

cattledog:
The LED management is inside the function printTemperature() and that was commented out.

Why you have LED's turned on and off inside a function called printTemperture() is a mystery. Very sloppy coding practice.

I do appreciate that!! and point taken on board. I have just started my exploration into this arduino area so I am very much learning, and I guess I have a lot to learn. :slight_smile:

The function printTemperature() is a mess. Its parameter is the address of one of the temp sensors on the one wire bus. But because of the way it is written, it does not check the temperature of that given sensor, it checks both, and one of them twice over! Looks like someone suggested you make that function to make the code shorter and less repetitive, but I don't think you have really grasped the concept properly yet...

PaulRB:
The function printTemperature() is a mess. Its parameter is the address of one of the temp sensors on the one wire bus. But because of the way it is written, it does not check the temperature of that given sensor, it checks both, and one of them twice over! Looks like someone suggested you make that function to make the code shorter and less repetitive, but I don't think you have really grasped the concept properly yet...

In time as i do start to learn and appreciate the concepts better and their interactions I am sure I will get better, at this stage I am very much a beginner, due to the couple of posts I have already started to read up on how to better utilise all the elements that make up good coding practices. hopefully this forum will continue to point me in the right directions as has been done here :slight_smile:

Thanks again

Yachtsman? Looks like you're hanging onto the shrouds there. And judging by your attire, it wasn't a warm day...

yeah, it was a freezing winters day but a good days sail nonetheless :slight_smile: