Switch case with multiple inputs

Hello,

I am having a bit of trouble while trying to figure out how to use a switch case in my program. In the end I intend to have 7 inputs that can produce a total of around 10 or 11 outputs, depending on combinations or inputs. Right now I am just trying to get a 3 inputs to produce 4 outputs. I had done some searching around and I found some articles that somewhat got me where I want to go, but I am still stumped.

I have attached my code below. In a basic sense, When I press the button attributed to the “ACC_Pulse_Pin” I want the serial monitor to only display “ACC Pulse”. I want the serial monitor to only display “ACC Switch” when I press the button associated with the “ACC_Switch_Pin”, and so on.

Right now, when I have everything set up and it it going through the loop, the serial monitor is cycling through each of the cases without any input. I may be misunderstanding, but my case 0 should be “When no input is enabled” in the current setup. Why are the cases cycling through when no button is pressed? When I do go and press a button, the code continues to cycle through the cases in a way that does not make sense to me.

I know this is a bad attempt at explaining the trouble I am having, but I would appreciate any input anyone has.

-Chris

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int DD_Pin = 2; //Input pin for DD, PLC output OUT6
const int AC_Pin = 3; //Input pin for AC, PLC output OUT7
const int AD_Pin = 4; //Input pin for AD, PLC output OUT8
const int Regen_Pin = 5; //Input pin for Regen, PLC output OUT5
const int Neutral_Pin = 6; //Input pin for Neutral, PLC output OUT9
const int ACC_Switch_Pin = 7; //Input pin for ACC Open by swtich, PLC output is switch attached to IN6
const int ACC_Pulse_Pin = 8; //Input pin for ACC Pulse by button, PLC output is button attached to IN7


void setup()
{
  // initialize the LCD
  lcd.begin();
  // Turn on the blacklight
  lcd.backlight();


  Serial.begin(9600);

  pinMode(DD_Pin, INPUT);
  pinMode(AC_Pin, INPUT);
  pinMode(AD_Pin, INPUT);
  pinMode(Regen_Pin, INPUT);
  pinMode(Neutral_Pin, INPUT);
  pinMode(ACC_Switch_Pin, INPUT);
  pinMode(ACC_Pulse_Pin, INPUT);

}

void loop() {


  int inputs = ((digitalRead(DD_Pin) << 2) | (digitalRead(ACC_Switch_Pin) << 1) | digitalRead(ACC_Pulse_Pin));


  switch (inputs)
  {
    case 0: //
      Serial.println("Nuetral");
      delay(1000);
    case 1:
      Serial.println("DD Pin only");
      delay(1000);
    case 2:
      Serial.println("ACC Switch only");
      delay(1000);
    case 4:
      Serial.println("ACC Pulse only");
      delay(1000);

  }
}

Each case must have a break statement.
Turn on the compiler warnings.

switch...case - Arduino Reference !!

Ahh, yes thank you. I am fairly new to all of this so thank you for the link.

I seem to be having an issue I did not know was happening. I simplified the loop because I was still having the issue of the cases switching for seemingly no reason. The code is below. I have the state of the pin being printed to the serial monitor and it seems to be switching without me doing anything at all. I posted a picture of the serial monitor below. The valve is switching between 0 and 1 without me pressing any buttons.

void loop() {


  int inputs = (digitalRead(DD_Pin));


  switch (inputs)
  {
    case 1: 
      Serial.println("DD");
      Serial.println(inputs);
      delay(1000);
      break;
    default:
      Serial.println("Neutral");
      Serial.println(inputs);
      delay(1000);
      break;

  }
}

I suspect that you will find this helpful https://forum.arduino.cc/index.php?topic=719995.0 especially the bit about pull up resistors.