: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);
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
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.
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.