5 pin DIP switch Logic

I am trying to find help on the forum with a project without luck. I am hoping I can get this going and then worry about packaging later. the DIP switch is basically the range sensor on the gear shifter. I want the out put to turn on a relay (supporting hardware of course) but just the output to turn on a led would be a solid start!

here is the diagram of the logic

this is what I gathered and put together which did'nt work. I have no experience with coding which is why it doesn't comppile successfully.. lol, but is so fascinating.

while searching, also trying to figure out switch case.. probably a better for this project?


void setup() {
// constants (is this the right label to use) labeled according to schematic
const int L1 = 2;               // the number of the L#  pin
const int L2 = 3;
const int L3 = 4;
const int L4 = 5;
const int L5 = 6;
const int RevOutput =  13;      // to identify when in reverse
const int StartOk =  12;          // to identify when in park



 // initialize the pins as an output:
 pinMode(RevOutput, OUTPUT);
 pinMode(StartOk, OUTPUT);  
 
 // initialize the pins as an input:
 pinMode(L1, INPUT);
 pinMode(L2, INPUT);
 pinMode(L3, INPUT);
 pinMode(L4, INPUT);
 pinMode(L5, INPUT);
}

void loop() {
 
 
 if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW)) 
   // turn RevOutput on:
   digitalWrite(RevOutput, HIGH);

 if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))
   // turn StartOk on:
   digitalWrite(StartOk, HIGH);

 if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == LOW) && (L4 == HIGH) && (L5 == LOW)) {
   // turn StartOk on:
   digitalWrite(StartOk, HIGH);


 } else {
   // turn all Outputs off:
   digitalWrite(RevOutput, LOW);
   digitalWrite(StartOk, LOW);
   
 }
}

I am only interested in the three states, Park, Reverse and Neutral

Any links or guidance you are willing to share, I'd appreciate it!
Thank you,
Clint

modularfox:
Any links or guidance you are willing to share, I'd appreciate it!

You have not told us what the problem is. For example, what happens when your run your program?

And you need to provide a drawing showing how the dip switch is wired to your Arduino.

Do you want to use a separate element of the DIP switch for each of Park, Reverse and Neutral?

And what about Drive?

...R

If the code will not compile post the error message(s). Post the entire error message, do not paraphrase. Use the "copy error message" button in the IDE.

Read the forum guidelines to see how to properly post code.

Are the inputs floating when the switches are in the 0 position?

What is the 1 level? 5V?

Robin2:
You have not told us what the problem is. For example, what happens when your run your program?

error when compiling

Arduino: 1.8.12 (Windows 7), Board: "Arduino Uno"


C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino: In function 'void loop()':

TransSwitch_logic2.ino:28:10: error: 'L1' was not declared in this scope

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

     ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:28:10: note: suggested alternative: 'A1'

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

     ^~

     A1

TransSwitch_logic2.ino:28:25: error: 'L2' was not declared in this scope

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                    ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:28:25: note: suggested alternative: 'A2'

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                    ^~

                    A2

TransSwitch_logic2.ino:28:40: error: 'L3' was not declared in this scope

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                                   ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:28:40: note: suggested alternative: 'A3'

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                                   ^~

                                   A3

TransSwitch_logic2.ino:28:56: error: 'L4' was not declared in this scope

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                                                   ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:28:56: note: suggested alternative: 'A4'

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                                                   ^~

                                                   A4

TransSwitch_logic2.ino:28:72: error: 'L5' was not declared in this scope

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                                                                   ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:28:72: note: suggested alternative: 'A5'

if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))

                                                                   ^~

                                                                   A5

TransSwitch_logic2.ino:30:18: error: 'RevOutput' was not declared in this scope

digitalWrite(RevOutput, HIGH);

             ^~~~~~~~~

TransSwitch_logic2.ino:32:10: error: 'L1' was not declared in this scope

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

     ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:32:10: note: suggested alternative: 'A1'

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

     ^~

     A1

TransSwitch_logic2.ino:32:26: error: 'L2' was not declared in this scope

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                     ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:32:26: note: suggested alternative: 'A2'

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                     ^~

                     A2

TransSwitch_logic2.ino:32:42: error: 'L3' was not declared in this scope

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                                     ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:32:42: note: suggested alternative: 'A3'

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                                     ^~

                                     A3

TransSwitch_logic2.ino:32:58: error: 'L4' was not declared in this scope

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                                                     ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:32:58: note: suggested alternative: 'A4'

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                                                     ^~

                                                     A4

TransSwitch_logic2.ino:32:73: error: 'L5' was not declared in this scope

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                                                                    ^~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:32:73: note: suggested alternative: 'A5'

if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))

                                                                    ^~

                                                                    A5

TransSwitch_logic2.ino:34:18: error: 'StartOk' was not declared in this scope

digitalWrite(StartOk, HIGH);

             ^~~~~~~

C:\Users\Dell\Documents\Arduino\TransSwitch_logic\TransSwitch_logic2.ino\TransSwitch_logic2.ino.ino:34:18: note: suggested alternative: 'strtok'

digitalWrite(StartOk, HIGH);

             ^~~~~~~

             strtok

sorry, it is a bit paraphrased, it was over 9000 characters and could not post. i am researching the not delcared in the scope and once sorted, i can re run it and try again.

Robin2:
And you need to provide a drawing showing how the dip switch is wired to your Arduino.

schematic I sketched up in tinkercad

led on the end is just to see if it powered on .. thats it

Robin2:
Do you want to use a separate element of the DIP switch for each of Park, Reverse and Neutral?

Robin2:
And what about Drive?

not needing any other positions to be read, end result once led lights up when dip switch set correctly, replace led with a circuit capable of activating a relay

groundFungus:
Are the inputs floating when the switches are in the 0 position?

I don't know understand the what inputs floating means.
All switches need be in correct position for the gear for the light to illuminate or it doesn't do anything at all

groundFungus:
What is the 1 level? 5V?

factory gear shfiter is 12v in, through switch to transmission. Im not using the transmission, so the input to the switch could be the 5v output from the Arduino?

I think i put the

void setup() {

in the wrong spot, when comparing to the Button example in the Arduino program. will reply with new code.

also don't know how to identify this code below into mine if needed

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

for this...

when i put the void setup in maybe what is the proper spot it compiles corectly but in the tinkercad it doesnt turn on the led when park is set, L1 high, L2 high, L3high, L4 low, L5 low

// constants (is this the right label to use) labeled according to schematic
const int L1 = 2;               // the number of the L#  pin
const int L2 = 3;
const int L3 = 4;
const int L4 = 5;
const int L5 = 6;
const int RevOutput = 13;      // to identify when in reverse
const int StartOk = 12;        // to identify when in park


// variables will change:
// int buttonState = 0;         // variable for reading the pushbutton status not sure if this is one of my problems


void setup() {
  // initialize the pins as an output:
  pinMode(RevOutput, OUTPUT);
  pinMode(StartOk, OUTPUT); 
 
  // initialize the pins as an input:
  pinMode(L1, INPUT);
  pinMode(L2, INPUT);
  pinMode(L3, INPUT);
  pinMode(L4, INPUT);
  pinMode(L5, INPUT);
}

void loop() {
 
 
  if ( ( L1 == LOW) && (L2 == LOW) && (L3 == HIGH) && (L4 == HIGH) && (L5 == LOW))
    // turn RevOutput on:
    digitalWrite(RevOutput, HIGH);

  if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == HIGH) && (L4 == LOW) && (L5 == LOW))
    // turn StartOk on:
    digitalWrite(StartOk, HIGH);

  if ( ( L1 == HIGH) && (L2 == HIGH) && (L3 == LOW) && (L4 == HIGH) && (L5 == LOW)) {
    // turn StartOk on:
    digitalWrite(StartOk, HIGH);


  } else {
    // turn all Outputs off:
    digitalWrite(RevOutput, LOW);
    digitalWrite(StartOk, LOW);
   
  }
}

If you want your error output to make sense, you need to put it into "code" tags. :roll_eyes:

From the code in Reply #5, this is wrong

if ( ( L1 == LOW) && (L2 == LOW)

Your variable L1 has the number of the pin - in this case 2 so you are testing is the number 2 == HIGH. That's not what you want.

You need another set of variables to hold the states of the pins. For example

byte L1state;
byte L2state;
// etc

and then you need a series of digitalRead() statements to check the states of the pins

L1state = digitalRead(L1);
L2state = digitalRead(L2);
// etc

and then your IF line should be

if ( ( L1state == LOW) && (L2state == LOW)

Quite separately it might make the program easier to understand if you use meaningful names - for example
parkPin
parkPinState
reversePin
reversePinState

...R

Hii,
OPs test circuit;


You are using the DIP switches to pull the inputs HIGH.
You need 10K resistors on each of the inputs to gnd so the inputs will go low when the DIP switch is open.

Tom... :slight_smile:

I dont under stand why

  if (L1state == HIGH && L2state == HIGH && L3state == HIGH){
    // turn reversePin LED on:
    digitalWrite(reversePin, HIGH);

when any switch 1 , 2 or 3 is on/high LED turns on. I expected all have to be HIGH for statement to be true then turn on pin labled reversePin
I thought at one point I had a functioning version, added more then poof, gone.

// constants won't change. They're used here to set pin numbers:
const int L1 = 2;
const int L2 = 3;          // the number of the pushbutton pin
const int L3 = 4;
const int L4 = 5;
const int L5 = 6;
const int startokPin = 12;
const int reversePin =  13;        // the number of the LED pin

// variables will change:
byte L1state;         // variable for reading the pushbutton status
byte L2state;
byte L3state;
byte L4state;
byte L5state;

void setup() {
  // initialize the pins as an output:
  pinMode(reversePin, OUTPUT);
  pinMode(startokPin, OUTPUT);
  
  // initialize the pins as an input:
  pinMode(L1, INPUT);
  pinMode(L2, INPUT);
  pinMode(L3, INPUT);
  pinMode(L4, INPUT);
  pinMode(L5, INPUT);
}

void loop() {
  
  // read the state of the pushbutton value:
  L1state = digitalRead(L1);
  L2state = digitalRead(L2);
  L3state = digitalRead(L3);
  L4state = digitalRead(L4);
  L5state = digitalRead(L5);
  
  
  if (L1state == HIGH && L2state == HIGH && L3state == HIGH){
    // turn reversePin LED on:
    digitalWrite(reversePin, HIGH);

  } else if (L1state == LOW && L2state == LOW && L3state == LOW){
    // turn startokPin LED on:
    digitalWrite(startokPin, HIGH);

  } else {
    // turn outputs off:
    digitalWrite(reversePin, LOW);
    digitalWrite(startokPin, LOW);



    
  }
}

when program starts, all switches are LOW/off and startokPin turns on led - expected
when any one switch 1 , 2 or 3 is HIGH/on startokPin turns off as expected, but reversePin turns on -not expected not all conditions are met (switch 1,2 and 3 are not HIGH)
after a few seconds of program running and flipping the switch both LED's just stay on regardless

I feel like Im making progress, but at the same time not so much. thanks again

Did you add the 10K pulldown resistors that TomGeorge mentions? It is important that the input pins do not "float". The pins will be at 5V when the switch is closed, but, without pulldowns, when open the input is in an indeterminate state.

That is why I asked,

Are the inputs floating when the switches are in the 0 position?

You may find that this ELSE does strange things

  } else {
    // turn outputs off:
    digitalWrite(reversePin, LOW);
    digitalWrite(startokPin, LOW);

You are testing for 3 states which means there are 8 possible outcomes. You have dealt with two of them with your IF and your ELSE IF clauses. Any of the other 6 outcomes will trigger the ELSE

...R

groundFungus:
Did you add the 10K pulldown resistors that TomGeorge mentions? It is important that the input pins do not "float". The pins will be at 5V when the switch is closed, but, without pulldowns, when open the input is in an indeterminate state.

That is why I asked,

Yes sir, I googled floating pins Arduino and watched a video and understood what you meant by it.

I added the "multimeter" lol so i can make sure the bulb is on or not.... hard to tell after a while

Robin2:
You may find that this ELSE does strange things

  } else {
// turn outputs off:
digitalWrite(reversePin, LOW);
digitalWrite(startokPin, LOW);

the idea behind that attempt is to turn off both outputs since if sensor read something other than the 3 desired switch setpoints ... kinda of like a key, get the right combination at the same time, access:)
so its the wrong way to go about that command? What does strange things mean and do you have a recommendation or command to research on?

Robin2:
You are testing for 3 states which means there are 8 possible outcomes. You have dealt with two of them with your IF and your ELSE IF clauses. Any of the other 6 outcomes will trigger the ELSE

and just like that lost... :slight_smile: .. Im interpreting it as I need to map out all possible outcomes and not just the ones I want?

could you give an example of adding another if statement, or does it need to be else if..
ive seen a few that have lots of if statements, but being new, I have no idea if that is right or wrong.

I really appreciate the help you guys are providing me!!

I also tired to commenting out the 2nd else request with same led result, it only takes one pin 1,2 or 3 to activate the reversePin

  } else {
    // turn outputs off:
    digitalWrite(reversePin, LOW);
   // digitalWrite(startokPin, LOW);

Update.. lol, update.. didn't want to post and make it appear on top... if i use the < instead of == the "combination of switches" works as intended, all or no led illumination... any idea why?

[code} if (L1state < HIGH && L2state < HIGH && L3state < HIGH){ [/code]

Hi,
This project, with this table is screaming out for binary value allocation of each dip switch and the switch... case... application.
table1.jpg

Tom... :slight_smile:

table1.jpg

modularfox:
the idea behind that attempt is to turn off both outputs since if sensor read something other than the 3 desired switch setpoints ...

That's fine. But you have only checked two setpoints.

I asked you a question in Reply #1 that you have not answered and it is key to how you write the program
Do you want to use a separate element of the DIP switch for each of Park, Reverse and Neutral?

If not then you need to write down the switch combinations that you want for each setting.

...R

I would favour the previously suggested idea of reading each DIP switch and using the state of each to set a separate bit in a byte variable. The value of the resulting variable can then be used to run the appropriate code for that combination of switch states

TomGeorge:
Hi,
This project, with this table is screaming out for binary value allocation of each dip switch and the switch... case... application.
table1.jpg
switch...case - Arduino Reference

Tom... :slight_smile:

from the examples Ive found, it seems that the switch would be the one output, and the case would be if the dip switch was a match.... so it would be like 3 switches and just one case per switch then? im still searching for a push button or led at the moment example to see how I can experiment with that option

Robin2:
That's fine. But you have only checked two setpoints.

I asked you a question in Reply #1 that you have not answered and it is key to how you write the program
Do you want to use a separate element of the DIP switch for each of Park, Reverse and Neutral?

If not then you need to write down the switch combinations that you want for each setting.

...R

sorry, I didn't reply with the information. What do you mean separate element of the DIP switch? I dont under stand the question.
once the DIP switch functions as intended /sets led to on, and off accordingly to the chart, it will get replaced with the actual switch in the vehicle therefore wont use the DIP switch at all.. unsure if that's is helpful on the question.

thanks to all for being patient and offering assistance!
clint

Hi,
You make each dip switch worth a binary value 1 , 2 , 4 , 8 , 16 == L1 , L2 , L3 , L4 , L5

If L1 and L3 are ON and the rest OFF

L1 (1 X 1) + L2 (0 x 2) + L3 (1 x 4) + L4 (0 x 8) + L5 ( 0 x 16) == 1 + 0 + 4 + 0 + 0 = 5.

5 is unique to L1 and L3 ON and the rest OFF.

Use switch.. case to specifically do what you want when case == 5.

Tom... :slight_smile:
It saves all those congested if statements.

modularfox:
sorry, I didn't reply with the information. What do you mean separate element of the DIP switch? I dont under stand the question.

The DIP switch has 5 separate switches and you want to detect Park Reverse and Neutral. Do you want to use a separate switch for each of those? For example switch 1 is Park, switch 2 is Reverse and switch 3 is Neutral.

...R

TomGeorge:
Hi,
You make each dip switch worth a binary value 1 , 2 , 4 , 8 , 16 == L1 , L2 , L3 , L4 , L5

If L1 and L3 are ON and the rest OFF

L1 (1 X 1) + L2 (0 x 2) + L3 (1 x 4) + L4 (0 x 8) + L5 ( 0 x 16) == 1 + 0 + 4 + 0 + 0 = 5.

5 is unique to L1 and L3 ON and the rest OFF.

Use switch.. case to specifically do what you want when case == 5.

Tom... :slight_smile:
It saves all those congested if statements.

mann, that is so over my head, it looks like it is the best way to right code, but I'm like beginner beginner cant get code right beginner.
I do thank you for trying to help, but I don't understand the statements/expressions

[/code]

Robin2:
The DIP switch has 5 separate switches and you want to detect Park Reverse and Neutral. Do you want to use a separate switch for each of those? For example switch 1 is Park, switch 2 is Reverse and switch 3 is Neutral.

...R

I swear when I understand the question I'm going to feel like an idiot :frowning: thank you so much for being patient with this... (to put the idiot thing in perspective, its like if i put car keys down, and cant find them, only to realize there in your pocket kind of idiot)

for "want to use a separate switch", are you meaning code or hardware... hardware no, code, whatever works. as long as can get the code to work which i don't understand why even using 2 switches doesn't operate as intended.

trying using only 2 switches, rest //coded out
if both pins 1 and 2 off, down position, low --- startokPin - expected
if either 1 or 2 pin goes high, startokPin turns off and reversePin turns on - not expected, should require both to be high before it turns on right.

I cant understand why the && is not working as intended or is my understanding && is not all or nothing?

tinkercad link

tinkercad virtual project which I feel confident its wired correctly

// constants won't change. They're used here to set pin numbers:
int L1 = 2;         
int L2 = 3;
// int L3 = 4;
// int L4 = 5;
// int L5 = 6;
int startokPin = 12;
int reversePin =  13;        // the number of the LED pin

// variables will change:
int L1state;         // variable for reading the pushbutton status
int L2state;
// int L3state;
// int L4state;
//int L5state;

// byte reverse = (L1state == 1 && L2state == 1 && L3state == 1 && L4state == 1 && L5state == 1);

  void setup() {
  // initialize the pins as an output:
   pinMode(reversePin, OUTPUT);
   pinMode(startokPin, OUTPUT);

  // initialize the pins as an input:
  pinMode(L1, INPUT);
  pinMode(L2, INPUT);
//  pinMode(L3, INPUT);
//  pinMode(L4, INPUT);
//  pinMode(L5, INPUT);
}

void loop() {
  
  // read the state of the pushbutton value:
  L1state = digitalRead(L1);
  L2state = digitalRead(L2);
//  L3state = digitalRead(L3);
//  L4state = digitalRead(L4);
//  L5state = digitalRead(L5);
  
  
  if (L1state > 0 && L2state > 0) {
    // turn reversePin LED on:
    digitalWrite(reversePin, HIGH);
    digitalWrite(startokPin, LOW);

  } else {
    // turn outputs off:
    digitalWrite(reversePin, LOW);
    digitalWrite(startokPin, HIGH);
    
  }
}