Search code examples
arduinoarduino-unoarduino-ide

What Is the default state of an arduino pin?


I have 2 input pins and want to print a specific number based on the state of the pins (HIGH/LOW).

I wrote the following program for this but it only prints error in serial port.

following is the code

int L1 = 2;
int L2 = 3;

void setup() {
  // put your setup code here, to run once:
  pinMode(L1, INPUT);
  pinMode(L2, INPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (L1 == HIGH && L2 == HIGH){
    Serial.println(10);
  }

  else if (L1 == HIGH && L2 == LOW) {
    Serial.println(20);
  }

  else if (L1 == LOW && L2 == LOW) {
    Serial.println(30);
  }

  else if (L1 == LOW && L2 == HIGH) {
    Serial.println(40);
  }

  else {
    Serial.println("error");
  }

  delay(2000);
}

Solution

  • You shouldn't have used L2==LOW/HIGH, L2 is always equal to 3, as you have defined in the beginning.

    Instead, you should use digitalRead(L2)

    For example:

        void loop() {
          
          if (digitalRead(L1)==HIGH && digitalRead(L2)==HIGH) {
            Serial.println(10);
          }
        
          else if (digitalRead(L1)==HIGH && digitalRead(L2)==LOW) {
            Serial.println(20);
          }
        
          else if (digitalRead(L1)==LOW && digitalRead(L2)==LOW) {
            Serial.println(30);
          }
        
          else if (digitalRead(L1)==LOW && digitalRead(L2)==HIGH) {
            Serial.println(40);
          }
        
          else {
            Serial.println("error");
          }
        
          delay(2000);
        }