A project i have to do

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:

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); 
    digitalWrite(relayPin, HIGH);
    }  {
             
  if (digitalRead(switchPin2) == HIGH);
    digitalWrite(relayPin, LOW);}
}

You can also see here that i didnt write anything about the LOW states as i want them to not be considered i want them to do nothing

please look at the syntax of the if operator

whats wrong with it

The brackets are in the wrong position.
if( ... ) {
//code
}

This page might help you:

Tysm and now if u can help me with the code also

The forum is not a place where we write codes on demand....

You have very little knowledge of the syntax of the language, how are you going to write anything in this case?

This is a mess:

Braces are in the wrong place, the ; after condition is superficial

i just need help with that

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

What's 'rn'? I deciphered the rest but couldn't convert rn.

[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]

check the difference ! :face_with_open_eyes_and_hand_over_mouth:

actually, you can skip the == HIGH and just write if(digitalRead(switchPin))
if you want to check LOW then use if(!digitalRead(switchPin))

What happens when you change the
if (digitalRead(switchPinx) == HIGH)
with
if (digitalRead(switchPinx) == LOW)

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.

1 Like

You specified your inputs to be INPUT_PULLUP. Your digitalRead() will always == HIGH until you press a button.

1 Like

i rewrote the code i read more of arduino havent taken my eyes of the pc for 4 hours straight
can u check it?

const int switchPin1 = 0;                   
const int switchPin2 = 1;                   
const int relayPin = 2; 
}                    
void setup() {
  pinMode(switchPin1, INPUT_PULLUP);        
  pinMode(switchPin2, INPUT_PULLUP);        
  pinMode(relayPin, OUTPUT);                
}
void loop() {
{
   if (digitalRead(switchPin1) == HIGH)     
   if  (digitalRead(switchPin2) == LOW)     
       digitalWrite(relayPin, HIGH);       
   else if (digitalRead(switchPin2) == HIGH)
   if (digitalRead(switchPin1) == LOW)      
  digitalWrite(relayPin, LOW);}            
};

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.

Good luck with your project.

I thank you.

1 Like

It should only take you seconds to test it yourself.

When you do, does it work the way you need :thinking: .


const int switchPin1 = 0;                   
const int switchPin2 = 1;                   
const int relayPin   = 2;                 

void setup() 
{
  pinMode(switchPin1, INPUT_PULLUP);        
  pinMode(switchPin2, INPUT_PULLUP);        
  pinMode(relayPin, OUTPUT);                
  
}


void loop() 
{
   if (digitalRead(switchPin1) == HIGH && digitalRead(switchPin2) == LOW)
   {
       digitalWrite(relayPin, HIGH);
   }	   
   
   else if (digitalRead(switchPin2) == HIGH && digitalRead(switchPin1) == LOW)
  {
       digitalWrite(relayPin, LOW);
  } 
  
}

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

1 Like

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

1 Like