void setup() {
// put your setup code here, to run once:
pinMode(13,INPUT);
pinMode(3,OUTPUT);
}
void loop() {
int x;
int i=3;
x=digitalRead(13);
i=i+x;
if(i%2==0)
{ if(digitalRead(13)==0)
digitalWrite(3,HIGH);
else (digitalRead(13)==1)
digitalWrite(3,HIGH);
}
else if(i%2==1)
{ if(digitalRead(13)==0)
digitalWrite(3,LOW);
else(digitalRead(13)==1)
digitalWrite(3,LOW);
}
}
Hi, @achilies018
Welcome to the forum.
To add code please click this link;
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
What model Arduino are you using?
You need to check ALL your else if... statements, they need { and } .
https://www.arduino.cc/en/pmwiki.php?n=Reference/Else
Tom...
PS, Tip, Before compiling your code in the IDE hit < CTRL > < T >, it will automatically format your code and let you see where your brackets are in the code.
Not quite true but definitely advisable
An else
does not use a condition.
Hi,
if(i%2==0)
{ if(digitalRead(13)==0)
digitalWrite(3,HIGH);
else (digitalRead(13)==1)
digitalWrite(3,HIGH);
}
An if.. and few { and } missing there.
@achilies018 what era you trying to do with your code?
Tom....
gcjr
October 16, 2021, 9:19am
5
i think you intended
if (i%2==0)
{
if (digitalRead (13)==0)
digitalWrite (3,HIGH);
else if (digitalRead (13)==1)
digitalWrite (3,HIGH);
}
but if the if condition is NOT true, the else is the NOT cond
if (i%2==0)
{
if (digitalRead (13)==0)
digitalWrite (3,HIGH);
else
digitalWrite (3,HIGH);
}
but looks like both the if and else do the same thing, so you probably intended
if (i%2==0)
{
if (digitalRead (13)==0)
digitalWrite (3,HIGH);
else
digitalWrite (3,LOW);
}
but this could simple be
if (i%2==0)
digitalWrite (3, ! digitalRead (13));
1 Like
system
Closed
April 14, 2022, 9:20am
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.