Just plain don't undrestand

:0 I'm trying to indicate when a relay closes or opens by connecting input to pin 3 and turning on or off the led connected to pin 13. Lots of error messages about syntax. Used to use Basic but this stuff is confusing. Any help would be appreciated.
Here's the code:

/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
and turns on led 13 if HIGH
This example code is in the public domain.
*/

// digital pin 2 has a relay attached to it. Give it a name:
int relay = 3;
int led = 13;

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the relay's pin an input:
pinMode(relay, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
int relayState = digitalRead(relay);
}
if (relayState) == LOW)
}
digitalWrite(led, LOW);
{
if (digitalRead(relay) == HIGH);
}
digitalWrite(led, HIGH);

}

Thanks

All code, aside from global variable declaration/initialization, needs to be inside a function. This is your loop function:

 void loop() {
  int relayState = digitalRead(relay);
 }

None of this code is inside a function:

   if (relayState) == LOW)
  }
   digitalWrite(led, LOW);
{
  if (digitalRead(relay) == HIGH);
}
digitalWrite(led, HIGH);

 }

And next time, post error messages when you get them, don't expect us to guess.

How to use this forum

Read this before posting a programming question

Use code tags, and post your error messages.

Yes the braces are closing when they should be opening, opening when they should be closing, parans in the wrong place, semi-colons in the wrong place. Better brush up on basic C syntax I think.

Here's your code in a form that at least compiles

/*
   DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor
 and turns on led 13 if HIGH
 This example code is in the public domain.
 */

// digital pin 2 has a relay attached to it. Give it a name:
int relay = 3;
int led = 13;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the relay's pin an input:
  pinMode(relay, INPUT);
  pinMode(led, OUTPUT);
}
void loop() {
  int relayState = digitalRead(relay);

  if (relayState == LOW) {
    digitalWrite(led, LOW);
  }
  if (digitalRead(relay) == HIGH) {
    digitalWrite(led, HIGH);
  }
}

But it is still seriously bad, for example why do digitalRead(relay) twice?

The entire loop() code could be replaced with this

void loop () {
    digitalWrite(led, digitalRead(relay));
}

Rob

pmlapl:

 // digital pin 2 has a relay attached to it. Give it a name:
 int relay = 3;

If the relay is on pin 2, as in the comment, why are you assigning it to pin 3?

Thanks all for the help. Rob, thank you for the corrections .The program compiled but didn't work. I gave up and used one from the tutorials that turned on an led using a switch. It works fine.

This exercise is for monitoring my AC usage. The relay turns on when the AC does and I want to write the output from the Arduino to Visual Basic and keep track of on and off times. That will be the next step.
I'm not sure if I need to use static outputs. That is, not continuously write an on or off verses do it once when the relay is closed or once when open. I may try to use "switch-case" if I need to.

So on to more learning.

Again, much thanks.

pmlapl:
Thanks all for the help. Rob, thank you for the corrections .The program compiled but didn't work. I gave up and used one from the tutorials that turned on an led using a switch. It works fine.

This exercise is for monitoring my AC usage. The relay turns on when the AC does and I want to write the output from the Arduino to Visual Basic and keep track of on and off times. That will be the next step.
I'm not sure if I need to use static outputs. That is, not continuously write an on or off verses do it once when the relay is closed or once when open. I may try to use "switch-case" if I need to.

So on to more learning.

Again, much thanks.

Sounds like you're looking to detect the signal edge. The StateChangeExample demonstrates how this is accomplished.