if else with a counter

Hello,

I have a project that I have been working on here and there for a few months now. I got it working properly, until I wanted to add more stuff into it. Here is what is going on:

I am using an arduino uno to power a linear actuator back and forth. I have a relay driver controlling it, but that doesn’t have anything to do with my code. I’m just trying to give a feel for what the project is. The linear actuator is moving a SPDT momentary switch back and forth. Then the signal from the switch lights up and LED. The amount of cycles is displayed on an LCD to keep track of how many times the switch has sent a signal. I started messing with it to test the switch. The company I work for wanted to know if it would live up to X amount of cycles.

The switch lived, and I got lucky because there was no feedback for the Uno to know if the switch had failed. What I am trying to include is an if else statement in the code that will add to the count if the signal is high, and break if the signal is low. I have included the code, and maybe someone can help me understand what I am doing wrong. I am rusty when it comes to coding, and I was trying to do my research to figure out why this isn’t working before I posted this.

Anything will help. I uploaded my code as it sits right now with me making changes and verifying.

Thanks

toggle_switch_2.ino (2.39 KB)

This is the problem

  if (switchValue1 == HIGH); {
    a ++;
    lcd.setCursor(3, 1);            //add to count
    lcd.print(a);
    delay(500);
  }
  else (switchValue1 == LOW); {
    break;
  }

You do not want those closing semicolons on your if and else. Those effectively terminate the if() statement which makes the code your think is associated with the test always execute as well as the code with the else clause.


Thank you for your reply. I changed the code based off of what you said, and am still getting the “expected ‘;’ before ‘{’ token” error message. I changed the code to look like:

if (switchValue1 == HIGH) {
a ++;
lcd.setCursor(3, 1); //add to count
lcd.print(a);
delay(500);
}
else (switchValue1 == LOW) {
break;
}

else (switchValue1 == LOW) {

should be:

else if (switchValue1 == LOW) {

Thank you,

I corrected the code and now the error message reads: break statement not within loop or switch

My code looks like:

if (switchValue1 == HIGH) {
a ++;
lcd.setCursor(3, 1); //add to count
lcd.print(a);
delay(500);
}
else if (switchValue1 == LOW) {
break;
}

That is correct. Where do you want to break to?

I actually just want it to stop so I know if the switch has failed. I changed the break to a return and it compiled. Was this the wrong thing to do?

Post the entire code.
For those on cell phones that can’t read attachments:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a =0;
//int e = 9;
//int i = 10;

const int forwards = 7;
const int backwards = 6;

int switchValue1 = analogRead(A0);
int switchValue2 = analogRead(A1);


void setup() {
  //pinMode (8, INPUT);
  pinMode (10, INPUT);
  pinMode (9, INPUT);

 lcd.begin(16, 2);
//  Print a message to the LCD.
  lcd.print("Number of Cycles");
  pinMode(9,INPUT);
  pinMode(10,INPUT);
   
pinMode(forwards, OUTPUT);
pinMode(backwards, OUTPUT);

}

void loop() {
                                     //start at center
 digitalWrite(forwards, HIGH);      
 digitalWrite(backwards, LOW);      //move forward
 delay(700); // wait .7 seconds

 digitalWrite(forwards, HIGH);      
 digitalWrite(backwards, HIGH);     //hold at forward position
 delay(5000);// wait 5 seconds
//     a ++;
//    lcd.setCursor(3, 1);            //add to count
//   lcd.print(a);
//    delay(500);

 if (switchValue1 == HIGH); {
       a ++;
    lcd.setCursor(3, 1);            //add to count
   lcd.print(a);
    delay(500);
 }
 else (switchValue1 == LOW); {
    break;
    }
 

//  else {
 //   break;
 //   }
    
 digitalWrite(forwards, LOW);
 digitalWrite(backwards, HIGH);     //return to center
 delay(700);// wait .7 seconds

 digitalWrite(forwards, HIGH);
 digitalWrite(backwards, HIGH);     //hold at center
 delay(800);// wait .8 seconds

 digitalWrite(forwards, LOW);
 digitalWrite(backwards, HIGH);     //move backwards
 delay(900);// wait .9 seconds

 digitalWrite(forwards, HIGH);
 digitalWrite(backwards, HIGH);     //hold at back position
 delay(5000);// wait 5 seconds
     a ++;
    lcd.setCursor(3, 1);            //add to count
   lcd.print(a);
    delay(500);

 digitalWrite(forwards, HIGH);
 digitalWrite(backwards, LOW);      //move forward
 delay(900);// wait .9 seconds

 digitalWrite(forwards, HIGH);
 digitalWrite(backwards, HIGH);     //hold at center
 delay(800);// wait .8 seconds


//   int switchValue1 = digitalRead(9);
//   int switchValue2 = digitalRead(10);

//if (switchValue1 == HIGH) {
 //   a ++;
 //   lcd.setCursor(3, 1);
 //  lcd.print(a);
  //  delay(500);
 // }

//else if (switchValue2 == HIGH) {
 //   a ++;
 //   lcd.setCursor(3, 1);
 //  lcd.print(a);
 //  delay(500);
 // }

//else {
//    lcd.setCursor(3, 1);
//    lcd.print(a);
//    delay(150);
//  }


}

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a =0;
//int e = 9;
//int i = 10;

const int forwards = 7;
const int backwards = 6;

int switchValue1 = analogRead(A0);
int switchValue2 = analogRead(A1);

void setup() {
//pinMode (8, INPUT);
pinMode (10, INPUT);
pinMode (9, INPUT);

lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“Number of Cycles”);
pinMode(9,INPUT);
pinMode(10,INPUT);

pinMode(forwards, OUTPUT);
pinMode(backwards, OUTPUT);

}

void loop() {
//start at center
digitalWrite(forwards, HIGH);
digitalWrite(backwards, LOW); //move forward
delay(700); // wait .7 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //hold at forward position
delay(5000);// wait 5 seconds
a ++;
lcd.setCursor(3, 1); //add to count
lcd.print(a);
delay(500);

// if (switchValue1 == HIGH) {
// a ++;
// lcd.setCursor(3, 1); //add to count
// lcd.print(a);
// delay(500);
// }
// else {
// return;
// }

// else {
// break;
// }

digitalWrite(forwards, LOW);
digitalWrite(backwards, HIGH); //return to center
delay(700);// wait .7 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //hold at center
delay(800);// wait .8 seconds

digitalWrite(forwards, LOW);
digitalWrite(backwards, HIGH); //move backwards
delay(900);// wait .9 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //hold at back position
delay(5000);// wait 5 seconds
a ++;
lcd.setCursor(3, 1); //add to count
lcd.print(a);
delay(500);

// if (switchValue2 == HIGH) {
// a ++;
// lcd.setCursor(3, 1); //add to count
// lcd.print(a);
// delay(500);
// }
// else {
// return;
// }

digitalWrite(forwards, HIGH);
digitalWrite(backwards, LOW); //move forward
delay(900);// wait .9 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //hold at center
delay(800);// wait .8 seconds

// int switchValue1 = digitalRead(9);
// int switchValue2 = digitalRead(10);

//if (switchValue1 == HIGH) {
// a ++;
// lcd.setCursor(3, 1);
// lcd.print(a);
// delay(500);
// }

//else if (switchValue2 == HIGH) {
// a ++;
// lcd.setCursor(3, 1);
// lcd.print(a);
// delay(500);
// }

//else {
// lcd.setCursor(3, 1);
// lcd.print(a);
// delay(150);
// }

}