Very simple sketch not working as expected???

Hey all,

I have an issue that has me baffled for such a simple sketch. The project is a PWM dimmer to control a set of automotive LED lights. When the output is held LOW, the lights are at full brightness, when I send them a PWM frequency, they are dimmed. I am using a pro-mini 5V/16MHz board and the lights are a pair of VisionX solo prime that have a flying lead for a dimmer application.

My code is as follows:

const int dimmer_OUT = 10;  //name pins
const int highbeam_IN = 9;

void setup() 
{
  pinMode (dimmer_OUT, OUTPUT);        // set pins as input and output
  pinMode (highbeam_IN, INPUT_PULLUP);
}

void loop() 
{
  if(digitalRead(highbeam_IN == LOW))  // check if the highbeam input has been pulled low
  {
    analogWrite(dimmer_OUT, 200);     //dim the lights if true
  }
  
  else
  {
  digitalWrite(dimmer_OUT, LOW);   // lights at full brightness if false
  }
}

When I run that, the lights are always dimmed like they are only getting the PWM signal. The output never drops when the digital input is connected to 0V. I have tried moving to several other PWM pins with the same result and added the pin 13 led to the function to verify the digital input is working properly and the led lights/goes out when I toggle the input on pin 9.

I wrote an even simpler test sketch to verify the board is OK and it works perfectly as expected without the digital read and if statement.

Test Code:

const int dimmer_OUT = 10;  //output to lamps on pin 10

void setup() 
{
  pinMode (dimmer_OUT, OUTPUT); // set pin to output
}

void loop() 
{
  analogWrite (dimmer_OUT, 200);    // set dimmer value of light
  delay (2000);                    // wait to see if it worked
  digitalWrite (dimmer_OUT, LOW);  // set lights to full brightness
  delay (2000);                    // wait to see if it worked
}

Thoughts? See something I'm doing wrong?

if(digitalRead(highbeam_IN == LOW))

change to

if(digitalRead(highbeam_IN) == LOW)

;)

And bonus tip,

Is you pin ever going to be 3056? Or -456? Aka, a byte is more suited ;)

Thanks, I'll make that change and give it a try.

As for using a byte, this is the only function this board will be running and I'm not terribly worried about using up resources. Good practice I suppose to make it habit though.

What you want is to compare the outcome of the digitalRead() to LOW. What you now do is compare the variable 'highbeam_IN' to LOW (which is a macro for 0) which is of course not true aka 0 and use that for the digitalRead() (aka, read pin 0) which is by default connected to the Tx of the Serial adapter. And guess what, the rest level of TTL serial is HIGH. Aka, the digitalRead() returns HIGH which is the same as true.

CJ_Diesel: Good practice I suppose to make it habit though.

Exactly. Small programs are the place to start good habits. Not when you're neck deep into trouble with a big program :D But the fact you use const and have a consistent naming scheme for variables is very good!

That makes sense, I appreciate the further detailed explanation. I work with PLC's and ladder logic at work and have a solid understanding of the workflow there but some of the actual writing code is still fuzzy. It's nice to get a description and explanation of why something works the way it does rather than the standard "because" answer. Thanks again for your help!