Is this proper way of Using HIGH inputs in if/else statements?

When I was using 5V as the test HIGH input, it does not turn on the LED on this if statement. The Arduino and the 5V are different supplies with the same ground connection. Is there something missing on this code?

  #define LEDPIN 6
  #define SENSPIN 4
  
  void setup() {
    pinMode(LEDPIN, OUTPUT);
    pinMode(SENSPIN, INPUT);
    Serial.begin(9600);
  }
  
  void loop() {
    if(digitalRead(SENSPIN) == HIGH ){
    digitalWrite(LEDPIN, HIGH);
  }
  else{
    digitalWrite(LEDPIN, LOW);
  }
  }

I don't think this is a code problem, so please post your schematic.

Thanks for trying to use code tags, but something went wrong, please try to fix it.

You could simplify your code to

void loop() {
  digitalWrite(LEDPIN, digitalRead(SENSPIN));
}

(This won't fix the problem because I don't think it is a code problem)

Is this proper way of Using HIGH inputs in if/else statements?

Yes, don´t use if/else statement, instead do

digitalWrite(LEDPIN, digitalRead(SENSPIN));

Have a nice day and enjoy coding in C++.

this is the schematic, sorry for the low quality fritz

Is that a 10k resistor to the LED?

my bad, 220 ohms was the proper resistor, forgot to change the value

What happens if you change this line to...

pinMode(SENSPIN, INPUT_PULLUP);

It's not in fact a schematic. But it does appear to clearly show how you wired the components, and I can't see a problem there either.

Next I suggest some clear, bright photos of the circuit taken from above and showing every wire.

Thanks for fixing the code tags!

For a test try connecting the Uno 5V pin directly to the input pin. Are you sure you don't have one of those breadboards where the power/gnd lines are split half way and need to be linked?

Steve

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.