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?

Aparna B
  • 113
  • 4

2 Answers2

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.

Michel Keijzers
  • 13,014
  • 7
  • 41
  • 58
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("      ");
    }
}
Wirewrap
  • 341
  • 4
  • 9