so i'm trying to make this code and i'm a begginer so i need help
what I want it to do is that when the state of the switch is low for it to do nothing:
look im trying to learn im 13 rn i know very little and i have 3 more days to finish this i dont really have time to learn more i will try after this but pls rn i rlly need help
[code]
const int switchPin1 = 2;
const int switchPin2 = 3;
const int relayPin = 4;
void setup()
{
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
}
void loop()
{
if (digitalRead(switchPin1) == HIGH) /*this should be LOW also */
{
digitalWrite(relayPin, HIGH);
}
if (digitalRead(switchPin2) == HIGH) /* this also should be LOW */
{
digitalWrite(relayPin, LOW);
}
}
[/code]
Sentences that begin with directives will probably not get you the help that you'd like.
On the contrary,
So, let's do something positive with that time. This forum has help people though larger problems in that same time, but the key to success was hard work on the part of the user asking the question. If you want help, it's sitting in front of you. It might take a few hours of reading and working on examples, but it will happen if you put in the effort, and when it's clear to others that you are trying, help will be much more forthcoming.
Technically correct, but absolutely wrong in the current climate. Let him understand the concepts before optimizing them away. But that's just my opinion.
in C, and C++, the IF statement is constructed as follows:
IF (CONDITION) {STATEMENT; STATEMENT; STATEMENT;...}
The curly braces are optional, and are only required if you want more than one statement to be treated as a block by the IF. Note that the semicolon marks the end of a statement.
Your code contains the following:
if (digitalRead(switchPin1) == HIGH);
so, the condition is (If digitalRead(switchPin1) == HIGH) . The very next character is a semicolon, marking the end of a statement. Since there wasn't actually a statement there, the compiler assumes a null statement, that does nothing. The digitalWrite() on the next line isn't actually inside the IF block, and thus always executes.
Some of the replies are a bit harsh, especially to a newbie 13 year old, so I've removed them. Some I've left in, not everyone will agree with my choices on which to delete and which to leave, but it's not always easy to select them.
Now please help or remain silent.
@anarduinobegginer ,
There is a flag to moderator at the bottom of each reply, I can't guarantee that the mods will always agree with the flags, but we will look.
i tried it worked just fine im rlly happy ive been trying to make it in a long a time and as i didnt know to much ive been searching and doing stuff thats why i asked this here i couldnt find any other place which would tell me how to do it