Compile error

My sketch wont compile and i'm not sure whats wrong. Is there anything that will get it to compile here is my sketch

int BUTTON=2;
int LED=13;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);

}

void loop() {
if (2=HIGH);
digitalWrite(13=HIGH);
if (2, LOW);
digitalWrite(13, LOW);
}

What's wrong is that you are guessing at code syntax, instead of looking at reference and examples. If you had one mistake, I would just tell you what it is, but you have so many that you need to hit the books.

httech:
if (2=HIGH);

There are multiple problems here. You seem to think you can use the pin number 2 directly to determine the pin state, but it doesn't work like that. The number 2 will always be 2 and will never be equal to HIGH. Please spend some time studying the digitalRead() documentation:

and this tutorial:
https://www.arduino.cc/en/Tutorial/DigitalReadSerial
to learn how to correctly read a pin.

You are also using the assignment operator (=) instead of the comparison operator (==). Please read the documentation to understand the very important difference between the two:

You also are not using if correctly. Please read the documentation to understand the correct syntax:

httech:
digitalWrite(13=HIGH);

You are not using digitalWrite() correctly. Please study the documentation to learn the correct way to use it:

and the Blink tutorial:
https://www.arduino.cc/en/Tutorial/Blink

httech:
if (2, LOW);
digitalWrite(13, LOW);

You are also not using if correctly here, in the same way as above, and in a new way too! Please study the documentation to understand the correct syntax:

Amazingly enough, with all of those mistakes the only part that is a syntax error is:

13 = HIGH

The rest of it is valid syntax but semantic nonsense. What you seem to have meant loop() to do is:

void loop()
{
  if (digitalRead(BUTTON) == HIGH)
    digitalWrite(LED, HIGH);
  else
    digitalWrite(LED, LOW);
}

No, (2=HIGH) as well. But I was pointing at the nature of the thought process that created it. Semantically, it is correct, the intention is to echo the button state on the LED, which is laid out in the right order. Illegal syntax is not the same thing as syntax that fails because it does not match your semantics.

That is why, "getting it to compile" is not enough. :slight_smile: