Digital Read

HI

I am trying to read 24 digital inputs as part of a project I am working on. Also to teach me more about coding.

The problem is that for some reason my Arduino Mega does not latch the reading when it is high.

I have started small, only 2 channels with the hopes of making them work and then add more channels as I go, and also learn better ways to do the code.

At the moment, when I remove the jumper from the 5V of the Arduino to PIN22 it displays the Channel number in the right line on the LCD.

But when I put it back in 5V pin it simply bounces between HIGH and LOW on the LCD display, as if it reads it HIGH and LOW at the same time.

I have tried many combinations of code and simply cannot get a stable HIGH reading...

PLEASE can anyone assist in showing me my mistake?

As you can see from PHOTO, the 22 is dimm as it is jumping HIGH and LOW

RippleReader2.ino (1.28 KB)

Please post code in your post (using code tags) instead of attaching it (if size allows; 9000 byte limit).

OP's code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 20, 4);

const int RippleChannel[] = {22, 23};
int channelstate = 0;


void setup() {

  pinMode (RippleChannel[0], INPUT);
  pinMode (RippleChannel[1], INPUT);
  digitalWrite(RippleChannel[0], HIGH);
  digitalWrite(RippleChannel[1], HIGH);


  lcd.init();
  lcd.backlight();
  Serial.begin(9600);


  lcd.setCursor(0, 0);
  lcd.print("Channels ON");
  lcd.setCursor(0, 2);
  lcd.print("Channels OFF");



}



void loop() {

  channelstate = digitalRead(RippleChannel[0]);

  if (digitalRead(RippleChannel[0]) == HIGH )
  {


    lcd.setCursor(0, 1);
    lcd.print(RippleChannel[0]);
    lcd.setCursor(0, 3);
    lcd.print("  ");
    delay(100);

  }

  else (digitalRead(RippleChannel[0]) == LOW);

  {

    lcd.setCursor(0, 1);
    lcd.print("  ");
    lcd.setCursor(0, 3);
    lcd.print(RippleChannel[0]);
    delay(100);


  }

  channelstate = digitalRead(RippleChannel[1]);

  if (channelstate == HIGH)
  {

    lcd.setCursor(3, 1);
    lcd.print(RippleChannel[1]);
    lcd.setCursor(3, 3);
    lcd.print("  ");
    delay(100);

  }

  else if (channelstate == LOW);

  {

    lcd.setCursor(3, 1);
    lcd.print("  ");
    lcd.setCursor(3, 3);
    lcd.print(RippleChannel[1]);
    delay(100);


  }
  return (0);
}

You have configured your inputs to use the internal pullup resistor; in which case the jumper / switch must be wired between pin and GND. So I do not understand what you mean by At the moment, when I remove the jumper from the 5V of the Arduino

The below should just be a else; else does not take a condition.

  else (digitalRead(RippleChannel[0]) == LOW);

And why is there a semicolon at the end?

channelstate = digitalRead(RippleChannel[0]);

  if (digitalRead(RippleChannel[0]) == HIGH )
  {

Why read a value into channelstate and then ignore that and repeat the digitalRead()s?

else (digitalRead(RippleChannel[0])== LOW);

I'm not sure exactly what you intended here. Did you just leave the if out? And the ; almost certainly shouldn't be there.

Steve