If and else statements, If statement not working

Im making this traffic light intersection project. A button triggers the if statement and when used alone, the code works, however when the else statement is added, it does not. Don’t have many comments atm but the main focus is the if and else statement.

int ledPin = 31; // choose the pin for the LED
int inPin = 29;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
// The following values are used to identify the steppor motor sequence for counter-clockwise rotation
int Blue=45;
int Pink=44;
int Yellow=43;
int Orange=42;

// The following values are used to identify the steppor motor sequence for clockwise rotation
int Blue2=45;
int Pink2=43;
int Yellow2=44;
int Orange2=42;

// CoilTimer allows coil to energize, so it has time to rotate to each energized coils
int CoilTimer=5;
int TrainWait=1000;

const int buzzer = 39; //buzzer to arduino pin 9

int green1= 3;
int yellow1= 4;
int red1= 5;
int green2= 24;
int yellow2= 25;
int red2= 26;
int BinaryOne=6;
int BinaryTwo=7;
int BinaryThree=8;
int BinaryFour=9;
int SevenSegPower=10;
int StopLight=28;
int WalkLight=30;



void setup() {
   pinMode(green1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(SevenSegPower, OUTPUT);
pinMode(BinaryOne, OUTPUT);
pinMode(BinaryTwo, OUTPUT);
pinMode(BinaryThree, OUTPUT);
pinMode(BinaryFour, OUTPUT);
pinMode(StopLight, OUTPUT);
pinMode(WalkLight, OUTPUT);
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
   //Sends a 1/0 to energize the motor's coil to rotate (counter clockwise) the level crossing barrier by a full step
    pinMode(Blue, OUTPUT);  
    pinMode(Pink,OUTPUT);  
    pinMode(Yellow,OUTPUT); 
    pinMode(Orange,OUTPUT); 
    
    //Sends a 1/0 to energize the motor's coil to rotate (clockwise) the level crossing barrier by a full step
    pinMode(Blue2, OUTPUT); 
    pinMode(Pink2,OUTPUT); 
    pinMode(Yellow2,OUTPUT); 
    pinMode(Orange2,OUTPUT); 
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
    
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    
    digitalWrite(ledPin, HIGH);  // turn LED OFF
    for(int i=0;i<=132;i++){ 
  tone(buzzer, 1000);
  // The LOW's allow the motor to perform next full step counter-clockwise for the level crossing barrier
  //The set HIGH's allow the motor to perform next full step counter clockwise for the level crossing barrier
  //Delay Timer allows for the coils to energize; therefore, the motor has time to rotate to each energized coils
  digitalWrite(Orange,LOW); 
  digitalWrite(Blue,HIGH); 
  digitalWrite(Pink,HIGH); 
  delay(CoilTimer);
   noTone(buzzer);
  digitalWrite(Blue,LOW); 
  digitalWrite(Pink,HIGH); 
  digitalWrite(Yellow,HIGH); 
  delay(CoilTimer); 
  tone(buzzer, 1000);
  digitalWrite(Pink,LOW);
  digitalWrite(Yellow,HIGH); 
  digitalWrite(Orange,HIGH); 
  delay(CoilTimer); 
  noTone(buzzer);
  digitalWrite(Yellow,LOW); 
  digitalWrite(Orange,HIGH); 
  digitalWrite(Blue,HIGH); 
  delay(CoilTimer); 
  tone(buzzer, 1000);
  }
{
  //Delay time used to allow train to cross intersection
 for(int i=0; i<=20; i++){
  tone(buzzer, 1000);
  delay(700);
  noTone(buzzer);
 }
}
//Sets a specific duration for the motor to rotate counter clockwise approximately 90 degrees
  for(int i=0;i<=132;i++){ 
    
    //Indicates that train has passed and traffic may continue after the level barrier is back up
digitalWrite(ledPin, LOW);
    
    //Sets indicated coils LOW to perform next full step clockwise for the level crossing barrier
    //Timer allows for the coils to energize; therefore, the motor has time to rotate to each energized coils
    digitalWrite(Blue2,LOW); 
    digitalWrite(Orange2,HIGH); 
    digitalWrite(Yellow2,HIGH); 
    delay(CoilTimer); 
    digitalWrite(Orange2,LOW);
    digitalWrite(Yellow2,HIGH); 
    digitalWrite(Pink2,HIGH); 
    delay(CoilTimer); 
    digitalWrite(Yellow2,LOW);
    digitalWrite(Blue2,HIGH);
    delay(CoilTimer);
    digitalWrite(Pink2,LOW);
    digitalWrite(Blue2,HIGH);
    digitalWrite(Orange2,HIGH);
    delay(CoilTimer);
  
    //Checks to see whether there is a train coming towards the intersection
 
}
}
  else {  
 

digitalWrite(red2, HIGH);
digitalWrite(StopLight, HIGH); 

    for (int j=1; j<=10; j=j+1) { // Advance Green Blinks up to ten times indicating a left turn.
digitalWrite(green1, HIGH);       
delay(300);
digitalWrite(green1, LOW);
delay(300);
}

Serial.println("Advance Green Off"); 
Serial.println("Solid Green On");

digitalWrite(StopLight, LOW);
digitalWrite(WalkLight, HIGH);

digitalWrite(green1, HIGH);        // Advance green turns to a solid green.
delay(5000);

digitalWrite(StopLight, HIGH);
digitalWrite(WalkLight, LOW);

digitalWrite(SevenSegPower, HIGH);
Serial.println("Pedestrian Crossing Countdown Begins");
Serial.println("9");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, HIGH);

delay(1000);

Serial.println("8");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, HIGH);

delay(1000);

Serial.println("7");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("6");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("5");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("4");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("3");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("2");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("1");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, LOW);

delay(2000);

Serial.println("Pedestrian Crossing Countdown Off");

digitalWrite(SevenSegPower, LOW);
delay(1000);
digitalWrite(green1, LOW);

Serial.println("Solid Green Off");    // Prints that the Green light turns off in the serial monitor
Serial.println("Yellow Light On");    // Prints that the Yellow light turns on in the serial monitor

digitalWrite(yellow1, HIGH);
delay(5000);
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
delay(2500);
digitalWrite(red2, LOW);

for (int k=1; k<=10; k=k+1) {
digitalWrite(green2, HIGH);
delay(300);
digitalWrite(green2, LOW);
delay(300);
}

digitalWrite(green2, HIGH);
delay(16000);

digitalWrite(WalkLight, LOW);
digitalWrite(StopLight, HIGH);

digitalWrite(green2, LOW);

digitalWrite(yellow2, HIGH);
delay(5000);
digitalWrite(yellow2, LOW);

digitalWrite(red2, HIGH);
delay(2500);
digitalWrite(red1, LOW);

}
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

(deleted)

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:

[code]

[color=blue]// your code is here[/color]

[/code]

Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please explain exactly what you mean by “A button triggers the if statement and when used alone, the code works, however when the else statement is added, it does not.”

My If statement works alone, and my Else statement works alone, but they don’t work together, I have even tried making them into 2 different if statements

int ledPin = 31; // choose the pin for the LED
int inPin = 29;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
// The following values are used to identify the steppor motor sequence for counter-clockwise rotation
int Blue=45;
int Pink=44;
int Yellow=43;
int Orange=42;

// The following values are used to identify the steppor motor sequence for clockwise rotation
int Blue2=45;
int Pink2=43;
int Yellow2=44;
int Orange2=42;

// CoilTimer allows coil to energize, so it has time to rotate to each energized coils
int CoilTimer=5;
int TrainWait=1000;

const int buzzer = 39; //buzzer to arduino pin 9

int green1= 3;
int yellow1= 4;
int red1= 5;
int green2= 24;
int yellow2= 25;
int red2= 26;
int BinaryOne=6;
int BinaryTwo=7;
int BinaryThree=8;
int BinaryFour=9;
int SevenSegPower=10;
int StopLight=28;
int WalkLight=30;



void setup() {
   pinMode(green1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(SevenSegPower, OUTPUT);
pinMode(BinaryOne, OUTPUT);
pinMode(BinaryTwo, OUTPUT);
pinMode(BinaryThree, OUTPUT);
pinMode(BinaryFour, OUTPUT);
pinMode(StopLight, OUTPUT);
pinMode(WalkLight, OUTPUT);
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
   //Sends a 1/0 to energize the motor's coil to rotate (counter clockwise) the level crossing barrier by a full step
    pinMode(Blue, OUTPUT);  
    pinMode(Pink,OUTPUT);  
    pinMode(Yellow,OUTPUT); 
    pinMode(Orange,OUTPUT); 
    
    //Sends a 1/0 to energize the motor's coil to rotate (clockwise) the level crossing barrier by a full step
    pinMode(Blue2, OUTPUT); 
    pinMode(Pink2,OUTPUT); 
    pinMode(Yellow2,OUTPUT); 
    pinMode(Orange2,OUTPUT); 
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
    
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    Serial.print(val);
    digitalWrite(ledPin, HIGH);  // turn LED OFF
    for(int i=0;i<=132;i++){ 
  tone(buzzer, 1000);
  // The LOW's allow the motor to perform next full step counter-clockwise for the level crossing barrier
  //The set HIGH's allow the motor to perform next full step counter clockwise for the level crossing barrier
  //Delay Timer allows for the coils to energize; therefore, the motor has time to rotate to each energized coils
  digitalWrite(Orange,LOW); 
  digitalWrite(Blue,HIGH); 
  digitalWrite(Pink,HIGH); 
  delay(CoilTimer);
   noTone(buzzer);
  digitalWrite(Blue,LOW); 
  digitalWrite(Pink,HIGH); 
  digitalWrite(Yellow,HIGH); 
  delay(CoilTimer); 
  tone(buzzer, 1000);
  digitalWrite(Pink,LOW);
  digitalWrite(Yellow,HIGH); 
  digitalWrite(Orange,HIGH); 
  delay(CoilTimer); 
  noTone(buzzer);
  digitalWrite(Yellow,LOW); 
  digitalWrite(Orange,HIGH); 
  digitalWrite(Blue,HIGH); 
  delay(CoilTimer); 
  tone(buzzer, 1000);
  }
{
  //Delay time used to allow train to cross intersection
 for(int i=0; i<=20; i++){
  tone(buzzer, 1000);
  delay(700);
  noTone(buzzer);
 }
}
//Sets a specific duration for the motor to rotate counter clockwise approximately 90 degrees
  for(int i=0;i<=132;i++){ 
    
    //Indicates that train has passed and traffic may continue after the level barrier is back up
digitalWrite(ledPin, LOW);
    
    //Sets indicated coils LOW to perform next full step clockwise for the level crossing barrier
    //Timer allows for the coils to energize; therefore, the motor has time to rotate to each energized coils
    digitalWrite(Blue2,LOW); 
    digitalWrite(Orange2,HIGH); 
    digitalWrite(Yellow2,HIGH); 
    delay(CoilTimer); 
    digitalWrite(Orange2,LOW);
    digitalWrite(Yellow2,HIGH); 
    digitalWrite(Pink2,HIGH); 
    delay(CoilTimer); 
    digitalWrite(Yellow2,LOW);
    digitalWrite(Blue2,HIGH);
    delay(CoilTimer);
    digitalWrite(Pink2,LOW);
    digitalWrite(Blue2,HIGH);
    digitalWrite(Orange2,HIGH);
    delay(CoilTimer);
  
    //Checks to see whether there is a train coming towards the intersection
 
}
}
}

Probably due to missmatched and/or misplaced {}.

Format your code and see the problem.

Paul

Paul_KD7HB: Probably due to missmatched and/or misplaced {}.

Format your code and see the problem.

Paul

I compiled it, no errors, each {} is correct.

I have an if and else statement in my code and the if statement is controlled whether the button is HIGH, if statement runs, otherwise the else code would work. However, when we put delays into the else statement, the if statement wont run if the button is HIGH.

// The following values are used to identify the steppor motor sequence for counter-clockwise rotation
int Blue=45;
int Pink=44;
int Yellow=43;
int Orange=42;

// The following values are used to identify the steppor motor sequence for clockwise rotation
int Blue2=45;
int Pink2=43;
int Yellow2=44;
int Orange2=42;

// CoilTimer allows coil to energize, so it has time to rotate to each energized coils
int CoilTimer=5;

// FlashingTimer used to delay warning lights, TrainWait halts traffic so train can cross intrsection.
int val = 0;
int ledPin= 31;
int inPin=29;
int TrainWait=1000;
int CrosswalkPower= 32;

int green1= 3;
int yellow1= 4;
int red1= 5;
int green2= 24;
int yellow2= 25;
int red2= 26;
int BinaryOne=6;
int BinaryTwo=7;
int BinaryThree=8;
int BinaryFour=9;
int SevenSegPower=10;
int StopLight=28;
int WalkLight=30;

const int buzzer = 39; //buzzer to arduino pin 9

void setup() {
  // put your setup code here, to run once:
 pinMode(green1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(SevenSegPower, OUTPUT);
pinMode(BinaryOne, OUTPUT);
pinMode(BinaryTwo, OUTPUT);
pinMode(BinaryThree, OUTPUT);
pinMode(BinaryFour, OUTPUT);
pinMode(StopLight, OUTPUT);
pinMode(WalkLight, OUTPUT);

//Button input used as a pressure plate sensor to activate train crossing  
    pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input

    //Sends a 1/0 to energize the motor's coil to rotate (counter clockwise) the level crossing barrier by a full step
    pinMode(Blue, OUTPUT);  
    pinMode(Pink,OUTPUT);  
    pinMode(Yellow,OUTPUT); 
    pinMode(Orange,OUTPUT); 

    //Sends a 1/0 to energize the motor's coil to rotate (clockwise) the level crossing barrier by a full step
    pinMode(Blue2, OUTPUT); 
    pinMode(Pink2,OUTPUT); 
    pinMode(Yellow2,OUTPUT); 
    pinMode(Orange2,OUTPUT); 



  
  Serial.begin(9600);     
                                  
}
void loop() {

  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    
    digitalWrite(ledPin, HIGH);  // turn LED OFF
    for(int i=0;i<=132;i++){ 
  tone(buzzer, 1000);
  // The LOW's allow the motor to perform next full step counter-clockwise for the level crossing barrier
  //The set HIGH's allow the motor to perform next full step counter clockwise for the level crossing barrier
  //Delay Timer allows for the coils to energize; therefore, the motor has time to rotate to each energized coils
  digitalWrite(Orange,LOW); 
  digitalWrite(Blue,HIGH); 
  digitalWrite(Pink,HIGH); 
  delay(CoilTimer);
  digitalWrite(Blue,LOW); 
  digitalWrite(Pink,HIGH); 
  digitalWrite(Yellow,HIGH); 
  delay(CoilTimer); 
  digitalWrite(Pink,LOW);
  digitalWrite(Yellow,HIGH); 
  digitalWrite(Orange,HIGH); 
  delay(CoilTimer); 
  digitalWrite(Yellow,LOW); 
  digitalWrite(Orange,HIGH); 
  digitalWrite(Blue,HIGH); 
  delay(CoilTimer); 
  noTone(buzzer);
  }
{
  //Delay time used to allow train to cross intersection
 for(int i=0; i<=20; i++){
  tone(buzzer, 1000);
  delay(700);
  noTone(buzzer);
 }
}
//Sets a specific duration for the motor to rotate counter clockwise approximately 90 degrees
  for(int i=0;i<=132;i++){ 
    
    //Indicates that train has passed and traffic may continue after the level barrier is back up
digitalWrite(ledPin, LOW);
    
    //Sets indicated coils LOW to perform next full step clockwise for the level crossing barrier
    //Timer allows for the coils to energize; therefore, the motor has time to rotate to each energized coils
    digitalWrite(Blue2,LOW); 
    digitalWrite(Orange2,HIGH); 
    digitalWrite(Yellow2,HIGH); 
    delay(CoilTimer); 
    digitalWrite(Orange2,LOW);
    digitalWrite(Yellow2,HIGH); 
    digitalWrite(Pink2,HIGH); 
    delay(CoilTimer); 
    digitalWrite(Yellow2,LOW);
    digitalWrite(Blue2,HIGH);
    delay(CoilTimer);
    digitalWrite(Pink2,LOW);
    digitalWrite(Blue2,HIGH);
    digitalWrite(Orange2,HIGH);
    delay(CoilTimer);
  
    //Checks to see whether there is a train coming towards the intersection
 
}
}
  else {  
 

digitalWrite(red2, HIGH);
digitalWrite(StopLight, HIGH); 

    for (int j=1; j<=10; j=j+1) { // Advance Green Blinks up to ten times indicating a left turn.
digitalWrite(green1, HIGH);       
delay(300);
digitalWrite(green1, LOW);
delay(300);
}

Serial.println("Advance Green Off"); 
Serial.println("Solid Green On");

digitalWrite(StopLight, LOW);
digitalWrite(WalkLight, HIGH);

digitalWrite(green1, HIGH);        // Advance green turns to a solid green.
delay(5000);

digitalWrite(StopLight, HIGH);
digitalWrite(WalkLight, LOW);

digitalWrite(SevenSegPower, HIGH);
Serial.println("Pedestrian Crossing Countdown Begins");
Serial.println("9");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, HIGH);

delay(1000);

Serial.println("8");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, HIGH);

delay(1000);

Serial.println("7");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("6");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("5");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("4");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, HIGH);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("3");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("2");

digitalWrite(BinaryOne, LOW);
digitalWrite(BinaryTwo, HIGH);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, LOW);

delay(1000);

Serial.println("1");

digitalWrite(BinaryOne, HIGH);
digitalWrite(BinaryTwo, LOW);
digitalWrite(BinaryThree, LOW);
digitalWrite(BinaryFour, LOW);

delay(2000);

Serial.println("Pedestrian Crossing Countdown Off");

digitalWrite(SevenSegPower, LOW);
delay(1000);
digitalWrite(green1, LOW);

Serial.println("Solid Green Off");    // Prints that the Green light turns off in the serial monitor
Serial.println("Yellow Light On");    // Prints that the Yellow light turns on in the serial monitor

digitalWrite(yellow1, HIGH);
delay(5000);
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
delay(2500);
digitalWrite(red2, LOW);

for (int k=1; k<=10; k=k+1) {
digitalWrite(green2, HIGH);
delay(300);
digitalWrite(green2, LOW);
delay(300);
}

digitalWrite(green2, HIGH);
delay(16000);

digitalWrite(WalkLight, LOW);
digitalWrite(StopLight, HIGH);

digitalWrite(green2, LOW);

digitalWrite(yellow2, HIGH);
delay(5000);
digitalWrite(yellow2, LOW);

digitalWrite(red2, HIGH);
delay(2500);
digitalWrite(red1, LOW);

}
}

Cross-post number#2 reported...

@richardphan01, please do not cross-post. Threads merged.

Merge #2 performed.

Make a simple program containing just a few lines, and test the button, the if and the else statements with Serial.prints. Example:

void loop() {
if (digitalRead(buttonpin)==HIGH) {
  Serial.println("HIGH");
  }
else {
   Serial.println("LOW");
   }
}

If that works, add a delay(). Get back to us if that doesn’t work.

jremington: Make a simple program containing just a few lines, and test the button, the if and the else statements with Serial.prints. Example:

void loop() {
if (digitalRead(buttonpin)==HIGH) {
  Serial.println("HIGH");
  }
else {
   Serial.println("LOW");
   }
}

If that works, add a delay(). Get back to us if that doesn't work.

Doesn't Work with delay

Doesn't Work with delay

Explain exactly what "doesn't work" means.

Post YOUR simple code, both "working" and "nonworking" versions, using code tags.

PS: it works for me!

There are no mismatched {} in the original code you posted. Do you have all the LED's hooked up? Instead of pushing a button just emulate it FOR testing purposes. You need to be more specific as far as what , in what point the code fails. There r is nothing obvious in plain if / else conditions to fail.

Set val = 1; AFTER you check if button is pushed and observe the lights - do they behave as expected? You may want to add infinite loop - for(;;); at THE END of the if (true){} if you do not want repeat the val = 1 process. When you are happy with that - set the val = 0 and repeat. Good luck in finding the bug.

Personally I wonder how you coded the whole process with very few comments what it does. I would not choose to name pin"BinaryOne" , but that is your business.

richardphan01: I compiled it, no errors, each {} is correct.

No compile errors just means that the {} are matching, not that they're at the correct spot.

The terrible indentation of your code makes it very hard to spot what { belongs to what }, and thus your if/else logic.