1

I want to add counter in multiple-buttons-to-control-relays:

int relay1 = 23;
int relay2 = 25;
int relay3 = 27;
int relay4 = 29;
int relay5 = 31;
int relay6 = 33;
int relay7 = 35;
int relay8 = 37;
////////////int mode =  41;      // select mode AUTO/MANUAL if HIGH=AUTO else LOW=MANUAL
//////////int start = 43;      // select START/STOP auto mode
//////////int tool =  13;       // tool Sw
int sw1 = 2;
int sw2 = 4;
int sw3 = 6;
int sw4 = 8;

int sw5 = 3;  //additional 
int sw6 = 5;
int sw7 = 7;
int sw8 = 9;
 ////////int modeState =  1;  //modelState for //start stop and tool
/////////int startState = 1; //start1State
/////////int toolState =  1;
int sw1Status, sw2Status, sw3Status, sw4Status,sw5Status, sw6Status, sw7Status, sw8Status;
int order = 1; // default is to set all relays off.
/// for counter//////////////////////////////////////////////////
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;
//// for counter ////////////////////////////////////////////////
void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);

  // input pull-up, so default state of buttons is HIGH.
  pinMode(sw1, INPUT_PULLUP);
  pinMode(sw2, INPUT_PULLUP);
  pinMode(sw3, INPUT_PULLUP);
  pinMode(sw4, INPUT_PULLUP);
  pinMode(sw5, INPUT_PULLUP);  ///additional
  pinMode(sw6, INPUT_PULLUP);
  pinMode(sw7, INPUT_PULLUP);
  pinMode(sw8, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  // read the buttons status
  sw1Status = digitalRead(sw1);
  sw2Status = digitalRead(sw2);
  sw3Status = digitalRead(sw3);
  sw4Status = digitalRead(sw4);
  sw5Status = digitalRead(sw5);
  sw6Status = digitalRead(sw6);
  sw7Status = digitalRead(sw7);
  sw8Status = digitalRead(sw8);
  ///// tool state example 
  ////if (sw1Status == LOW && sw2Status == LOW && sw3Status == LOW && sw4Status == LOW && toolState == HIGH) { //

  if (sw1Status == LOW && sw2Status == LOW && sw3Status == LOW && sw4Status == LOW) { // if button 1 pressed
    order = 2; // go to case 2 start up state
    Serial.println("all switch off follow order 2");
  }
  if (sw1Status == HIGH && sw2Status == LOW && sw3Status == LOW && sw4Status == LOW) { // if button 2 pressed
    order = 3;// go to case 3
     Serial.println("order 3 sw 1 relay 1 on");
  }
  if (sw1Status == HIGH && sw2Status == LOW && sw3Status == LOW && sw4Status == HIGH) { // if button 3 pressed
     //// possible if (order != 5) { // if order is not equal 5 turn on relay 1 & 4
    order = 4;// go to case 4
    Serial.println(" order 4 sw 1,4 relay 1,7,8 on");

  }
  if (sw1Status == HIGH && sw2Status == LOW && sw3Status == HIGH && sw4Status == LOW) { // if button 3 pressed
    order = 5;// go to case 5
    Serial.println(" order 5 sw 2 3 on relay 1on now right down");
  }
  if (sw1Status == LOW && sw2Status == HIGH && sw3Status == HIGH && sw4Status == LOW) { // if button 3 pressed
    order = 6;// go to case 6
    Serial.println(" order 6 sw 3 and 2 on");
  }
  if (sw1Status == LOW && sw2Status == HIGH && sw3Status == LOW && sw4Status == HIGH) { // if button 2 pressed
    order = 7;// go to case 
     Serial.println(" order 7 ");
  }
  if (sw1Status == LOW && sw2Status == HIGH && sw3Status == HIGH && sw4Status == LOW && sw7Status == LOW) { // 
    order = 8;// go to case 
    Serial.println("orde 8 left side ");
  }
  if (sw1Status == LOW && sw2Status == LOW && sw3Status == LOW && sw8Status == HIGH && sw8Status == LOW) { // 
    order = 9;// go to case 
    Serial.println(" spare order 9 sw 4 high");
  }

  if (sw1Status == LOW && sw2Status == LOW && sw3Status == LOW && sw4Status == LOW) { // button 4 toggle
    if (order != 10) { // if order is not equal
      Serial.println("  spare order 10 all off ");

      order = 10;
    }
    else { // else go to case 1, which will turns off all relays
      order = 1;
    }
  }

  switch (order) {
    case 1: // if order equals 1
    delay(200);
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, HIGH);
      digitalWrite(relay8, HIGH);
      Serial.println("default start up ");

      break;

    case 2: // if order equals 2
    delay(1000);
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, HIGH);
      digitalWrite(relay8, HIGH);
      Serial.println("move up case 2");

      break;

    case 3: // if order equals 3
      delay(1000);
      digitalWrite(relay1, HIGH);
      digitalWrite(relay2, LOW);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, HIGH);
      digitalWrite(relay8, HIGH);
      Serial.println("move right case 3");

      break;{

    case 4: // if order equals 4
      delay(1000);
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, LOW);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, LOW);
      digitalWrite(relay8, LOW);
      Serial.println("move down case 4");
      break;}

    case 5: // if order equals 5
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, LOW);
      digitalWrite(relay8, LOW);
      Serial.println("move down  case 5");
      break;
      case 6: // if order equals 6
      delay(1000);
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, HIGH);
      digitalWrite(relay8, HIGH);
      Serial.println("move up case 6");
      break;
          case 7: // if order equals 
          delay(1000);
          val = digitalRead(sw2Status); // read input value
          if (val == HIGH)  // check if the input is HIGH (button released)
            currentState = 1;
            if(currentState != previousState)
            if(currentState == 1)
              counter = counter + 1;
              else
              case 10: // if order equals
            Serial.println(counter);

      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, HIGH);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, HIGH);
      digitalWrite(relay8, HIGH);

      Serial.println("move up rly 1");
      break;

    case 8: // if order equals
    delay(1000);
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, LOW);
      digitalWrite(relay4, HIGH);
      digitalWrite(relay5, HIGH);
      digitalWrite(relay6, HIGH);
      digitalWrite(relay7, HIGH);
      digitalWrite(relay8, HIGH);
      Serial.println("new left side");
      break;
      case 9: // if order 
      delay(1000);
      digitalWrite(relay1, HIGH);
      digitalWrite(relay2, LOW);
      digitalWrite(relay3, HIGH);
      digitalWrite(relay4, LOW);
      digitalWrite(relay5, LOW);
      digitalWrite(relay6, LOW);
      digitalWrite(relay7, HIGH);
      digitalWrite(relay8, HIGH);
      Serial.println(" on right side");
      break;

    default: // if none of above cases is valid go to case 1
      order = 1;
      break;
  }
}

multiple button switch case for multiple relays

2
  • 1
    Align your code by selecting it and press ctrl-k You added the counter already in your code. So I guess the counter does not count what you want, and we cannot guess. Describe what you expect counter to count, and what you see now. May 15 '20 at 8:27
  • 1
    I'm confused. Your buttons are set up using INPUT_PULLUP, so reading a low from a button should indicate that it's pressed. You have code like this: if (sw1Status == LOW && sw2Status == LOW && sw3Status == LOW && sw4Status == LOW) with a comment of "if button 1 pressed". I read that code to say "if switches 1, 2, 3, and 4 are all pressed...". Why do you interrogate the state of all your buttons to determine which case to execute in your switch statement? Is it valid for the user to press multiple buttons at the same time to get to different relay states?
    – Duncan C
    May 15 '20 at 15:56

Browse other questions tagged or ask your own question.