1

I wrote a program to read digital input from pins 36, 37, 38 and print on LCD when they go HIGH. I've connected those pins to a switch to test.

The program looks like this:

#include "LiquidCrystal.h"

// initialize the library by providing the nuber of pins to it
LiquidCrystal lcd(8,9,4,5,6,7);
int vswr,th,od,a;

void setup() {
  pinMode(36,INPUT);//SENSING VSWR
  pinMode(37,INPUT);//SENSING THERMAL
  pinMode(38,INPUT);//SENSING OVER DRIVE
  pinMode(35,OUTPUT);//BYTE_CTRL SIGNAL TO SHUTDOWN 
  digitalWrite(35,LOW);
}

void loop() {
  vswr = digitalRead(36);
  th= digitalRead(37);
  od = digitalRead(38);
  lcd.begin(16,2);
  // set cursor position to start of first line on the LCD
  lcd.setCursor(0,0);
  if (vswr==1) {
    lcd.write("vswr");
    digitalWrite(35,HIGH);
    delay(2000);
  }
  if (th==1) {
    lcd.write("thermal");
    digitalWrite(35,HIGH);
    delay(2000);
  }
  if (od==1) {
    lcd.write("over drive");
    digitalWrite(35,HIGH);
    delay(2000);
  }
}

When I run this, the LCD continuously displays:**

vswrthermaloverdrive

Even though none of the pins are high.

When I use Pins 11, 12, 13 instead of 36, 37, 38 it works fine. I already used pins 11,12,13 for some other purpose. Why is this happening? And how do I solve this problem?

  • Please edit your question to specify what you have connected to the input pins. – per1234 Dec 18 '17 at 10:53
  • 1
    The pin needs to be pulled high or pulled low. Either with a pullup or pulldown resistor or a switch. When a input pin is not connected to something, the digitalRead can return HIGH or LOW, it can be anything. The digital pins 11, 12, 13, 36, 37, 38 are all the same: pighixxx.com/test/portfolio-items/mega – Jot Dec 18 '17 at 12:31
1

I expect the problem is in the circuit. To be sure, put a serial print statement directly after reading the inputs, to be sure the input values are read correctly.

See the remark of using INPUT or INPUT_PULLUP from Jot below.

The following is just a more compact way of writing your loop (and does not fix your problem):

void process(int pin, char* text)
{
    if (digitalRead(pin) == HIGH) // Use HIGH instead of 1
    {
       lcd.write(text);
       digitalWrite(35, HIGH);
       delay(2000);
    }
}

void loop()
{
    lcd.begin(16,2);
    // set cursor position to start of first line on the LCD
    lcd.setCursor(0,0);

    process(36, "vswr");
    process(37, "thermal");
    process(38, "overdrive");
}

Also, I would add the delay at the end (after the three calls of process). And instead of hard coded values 36, 37 and 38 use 3 constants/defines.

  • 1
    He never write LOW on pin 35 ... – user31481 Dec 18 '17 at 11:41
  • I will add it in the answer .. THAT will be the fix high likely – Michel Keijzers Dec 18 '17 at 11:48
  • @Michel Keijzers: thanks for simplifying the program. I'm new to programming. Anyways I want to know why those input are not sensed properly. – aparna Dec 18 '17 at 12:10
  • 1
    @MichelKeijzers, don't write an input LOW. That was used many years ago for the internal pullup resistor, but now the INPUT is specifically a INPUT without a pullup resistor, and INPUT_PULLUP is with internal pullup resistor. Do you have a reference to that wrong use of writing LOW an input? You also can't reduce a pin as OUTPUT in a loop and make it INPUT. – Jot Dec 18 '17 at 12:30
  • 1
    @MichelKeijzers : pull down resistor solved my problem. – aparna Dec 19 '17 at 8:15
0

Another thing, only pin == HIGH writes something to the LCD, the message does not magically go away when pin == LOW

Also, about the logic for pin 35, what should happen when alarm goes away? Maybe pin 35 should be low again?

Code suggestion:

void process(int pin, char* text)
{
    if (digitalRead(pin) == HIGH) // Use HIGH instead of 1
    {
       lcd.write(text);
       digitalWrite(35, HIGH);
    }
    else
    {
       lcd.write("      ");
    }
}
  • pin 35 should be low when alarm goes away and I tried your code. I still need to use pull down – aparna Dec 18 '17 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.