Cannot figure out high input

  int pinA = 2;
  int pinB = 3;
  int pinC = 4;
  int pinD = 5;
  int pinF = 6;
  int pinG = 7;
  int ledPin1 = 9;
  int ledPin2 = 10;
  int ledPin3 = 11;
  int ledPin4 = 12;

  int pinstateAD = LOW;
  int pinstateAF = LOW;
  int pinstateAG = LOW;
  int pinstateBF = LOW;
  int pinstateBG = LOW;
  int pinstateCD = LOW;
  int pinstateCG = LOW;
void setup() {
  // put your setup code here, to run once:

  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, INPUT);
  pinMode(pinF, INPUT);
  pinMode(pinG, INPUT);
  pinMode(ledPin1, OUTPUT);


}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(pinA, HIGH);
  digitalRead(pinD);
  if (pinD == HIGH);  {
    digitalWrite(ledPin1, HIGH);
  }
}



this is my code. I have a wire from digital output pin 2 jumped to digital input pin 5, and no matter what, my ledPin1 turns on. if i disconnect the wire, on. if i change "if (pinD == HIGH)" to low, led on. what am i missing?

Welcome to the forum

    digitalRead(pinD);
    if (pinD == HIGH)

You read the state of pinD then ignore it

Then, depending on whether the value (not the state) of pinD is HIGH or LOW you take actions

The value of pinD will always be 5, hence HIGH

Can you see the problem ?

In loop() you never do anything to turn ledPin1 LOW.
So if it ever goes high for any reason, it will stay high.

i have also tried like this

 digitalWrite(pinA, HIGH);
  if (digitalRead(pinD = HIGH))
    ;
  {
    pinstateAD = 1;
  }

I've also tried setup like this, but if you are saying the value of pinD is always 5, why am i reading pinD for HIGH/LOW

OH i need to set it to low before this, i see. will try and come back

if (digitalRead(pinD = HIGH))

Wrong in a different way this time

  • What is the parameter needed for digitalRead() ?
  • how do you compare two values ?
    Answer, not by using =
void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(ledPin1, LOW);
  digitalWrite(pinA, HIGH);
  if (digitalRead(pinD == HIGH));  {
    digitalWrite(ledPin1, HIGH);
  }
}

light is still on with no connection between pin 2 and 5.

if (digitalRead(pinD == HIGH));

Nearly, but the semicolon should not be there because it is the only code dependant on the result of the test

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(ledPin1, LOW);
  digitalWrite(pinA, HIGH);
  if (digitalRead(pinD == HIGH))  {
    digitalWrite(ledPin1, HIGH);
  }
}

ledPin1 is still on with no connection to pinD.. it only turns off if i change this line

  digitalWrite(ledPin1, HIGH);

to LOW

if (digitalRead(pinD == HIGH))

Still wrong

hmm.. I'm not sure what I'm missing. digitalRead(pinD
should read pin 5 for a high or low.. OH

if (digitalRead(pinD) == HIGH) 

is this it? is that what im doing, including the HIGH in the wrong area of of the statement?

OH indeed

Yes, the parameter for digitalRead() is a pin number alone

Hah, you're a good teacher. made me think. thank you.

Yes, the pinD == HIGH inside of the parentheses simplifies into:

  pinD == HIGH
  5 == 1
  false
  0

... before digitalRead(???) starts to work with it.

I am glad that you appreciate that and that you got there in the end. It would have been easy to give the answer, but maybe by giving you clues and you working it out you will remember it better.

For the benefit of anyone finding this topic at a later date please post your working sketch

1 Like

working on it.. it seems i have some other problems with the rest of the code.. i seem to be having problems with my led 3 turning on currently (with everything else commented out, i think i have the same problem across all my logic for LED's)

int pinA = 2; //pinD counterpart (signal)
int pinB = 3; //pinF counterpart (ground)
int pinC = 4; //pinG counterpart (signal)
int pinD = 5;
int pinF = 6;
int pinG = 7;
int ledPin1 = 9;   //pass
int ledPin2 = 10;  //fail signal short
int ledPin3 = 11;  //fail ground short
int ledPin4 = 12;  //no continuity

int pinstateAD = 0;
int pinstateAF = 0;
int pinstateAG = 0;
int pinstateBF = 0;
int pinstateBG = 0;
int pinstateCD = 0;
int pinstateCG = 0;



void setup() {
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, INPUT);
  pinMode(pinF, INPUT);
  pinMode(pinG, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
 
}

void loop() {
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
 

  digitalWrite(pinA, HIGH);
  if (digitalRead(pinD) == HIGH)
  {
    pinstateAD = 1;
  }
  /*if (digitalRead(pinF) == HIGH)
  {
    pinstateAF = 1;
  }*/
  if (digitalRead(pinG) == HIGH)
    
  {
   // pinstateAG = HIGH;
  }
  digitalWrite(pinA, LOW);
  digitalWrite(pinB, HIGH);
  if (digitalRead(pinF) == HIGH)
  {
    pinstateBF = 1;
  }
  if (digitalRead(pinG) == HIGH)
  {
    pinstateBG = 1;
  }
  digitalWrite(pinB, LOW);
  digitalWrite(pinC, HIGH);

  if (digitalRead(pinD) == HIGH)
  {
    pinstateCD = 1;
  }
  if (digitalRead(pinG) == HIGH)
  {
    pinstateCG = 1;
  }
  digitalWrite(pinC, LOW);

  if (pinstateAD == 1) {
    if (pinstateAG ==1) {
      digitalWrite(ledPin2, HIGH);
    }
  }
  if (pinstateAF == 1); {
    digitalWrite(ledPin3, HIGH);
    }
  /*if (pinstateAD == pinstateAG == 0); {
    digitalWrite(ledPin4, HIGH);
    }*/
  /*if (pinstateBF = 0); {
    digitalWrite(ledPin4, HIGH);
    }*/
  if (pinstateBG == 1); {
    digitalWrite(ledPin3, HIGH);
    }
  /*if (pinstateCD == pinstateCG == 1); {
    digitalWrite(ledPin2, HIGH);
    }*/
  /*if (pinstateCD == pinstateCG == 0); {
    digitalWrite(ledPin4, HIGH);
    }*/
  if ((pinstateAD != pinstateAG) && (pinstateCD != pinstateCG) && (ledPin2, ledPin3, ledPin4 = 0));{
    digitalWrite(ledPin1, HIGH);
    }
}

please bear with me, i just started trying to learn this language like 12 hours ago. :sleeping: :sleeping:

  digitalWrite(ledPin3, HIGH);

im assuming this is another syntax mistake in these lines.
pinA and pinF are not connected. Nor are pin B and G, but if comment those 2 out the led turns off.

I have had a very short look at your code and noticed this immediately

  if (pinstateBG == 1); {
    digitalWrite(ledPin3, HIGH);
    }

That semicolon should not be there. Because it is there the digitalWrite() is executed unconditionally whatever the value of pinstateBG

I did not look any further

Ah. yeah that seems to keep getting me. You are right. i've cleaned it up, and for sake of simplicity decided to cut out the extra LED's and stick with pass/fail. this is the program working now i believe.

int pinA = 2;  //pinD counterpart (signal)
int pinB = 3;  //pinF counterpart (ground)
int pinC = 4;  //pinG counterpart (signal)
int pinD = 5;
int pinF = 6;
int pinG = 7;
int ledPin1 = 9;   //pass
int ledPin2 = 10;  //fail signal short  (any fail until pin3 and 4 are written into fail modes)
//int ledPin3 = 11;  //fail ground short
//int ledPin4 = 12;  //no continuity

int pinstateAD = 0;
int pinstateAF = 0;
int pinstateAG = 0;
int pinstateBF = 0;
int pinstateBG = 0;
int pinstateCD = 0;
int pinstateCG = 0;
int passState = 1;



void setup() {
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, INPUT);
  pinMode(pinF, INPUT);
  pinMode(pinG, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  // pinMode(ledPin3, OUTPUT);
  //pinMode(ledPin4, OUTPUT);
}

void loop() {
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  //digitalWrite(ledPin3, LOW);
  //digitalWrite(ledPin4, LOW);


  digitalWrite(pinA, HIGH);
  if (digitalRead(pinD) == HIGH) {
    pinstateAD = 1;
  }
  if (digitalRead(pinF) == HIGH) {
    pinstateAF = 1;
  }
  if (digitalRead(pinG) == HIGH)

  {
    // pinstateAG = HIGH;
  }
  digitalWrite(pinA, LOW);
  digitalWrite(pinB, HIGH);
  if (digitalRead(pinF) == HIGH) {
    pinstateBF = 1;
  }
  if (digitalRead(pinG) == HIGH) {
    pinstateBG = 1;
  }
  digitalWrite(pinB, LOW);
  digitalWrite(pinC, HIGH);

  if (digitalRead(pinD) == HIGH) {
    pinstateCD = 1;
  }
  if (digitalRead(pinG) == HIGH) {
    pinstateCG = 1;
  }
  digitalWrite(pinC, LOW);

  if (pinstateAD == 1) {
    if (pinstateAG == 1) {
      digitalWrite(ledPin2, HIGH);
    }
  }
  if (pinstateAF == 1) {
    digitalWrite(ledPin2, HIGH);
    (passState = 0);
  }
  if (pinstateAD == 0) {
    if (pinstateAG == 0) {
      digitalWrite(ledPin2, HIGH);
      (passState = 0);
    }
  }
  if (pinstateBF = 0) {
    digitalWrite(ledPin2, HIGH);
  }

  if (pinstateCD == pinstateCG == 1) {
    digitalWrite(ledPin2, HIGH);
    (passState = 0);
  }
  if (pinstateCD == 0) {
    if (pinstateCG == 0) {
      digitalWrite(ledPin2, HIGH);
      (passState = 0);
    }
  }
  if (passState == 1) {
    digitalWrite(ledPin1, HIGH);
  }
}

    if (pinstateBF = 0)

Perhaps not