Dip switch to change code if... then.. question

A quick observation… your meters[2] array specifies 2 elements. However, your dip index will hold values of 1 and 2. When dip == 2, you will be accessing information outside of the array bounds. Because the array is zero-indexed, dip should only evaluate to 0 and 1 in the current code. You are currently using each bit of the dip variable to hold the switch position, but then accessing the meters array directly using “dip” is not good.

consider changing this:

dip = 0;
if (digitalRead(D2)) dip+=1;
if (digitalRead(A2)) dip+=2;
// if (digitalRead(B4) dip+=4;
// if (digitalRead(B5) dip+=8;

to this:

dip = 0;
if (digitalRead(D2)) dip = 0;
if (digitalRead(A2)) dip = 1;
// if (digitalRead(B4) dip = 2;
// if (digitalRead(B5) dip = 3;

If you meant to have 16 possible meters using just the 4 dip switches, then don’t use the above… you would have to rethink based on your desired usage.

1 Like

Thank you Tim,
I will give that a shot. I have a rotary switch on order like you suggested. My end goal would be to have a setting for 10 meters using the 10 channel rotary switch.

I ordered this one to test.
https://www.digikey.com/product-detail/en/nkk-switches/FR01FR10P-W-S/FR01FR10P-W-S-ND/2104083

I really appreciate you taking the time to comment and help. I would be lost without this board. Having absolutely no coding experience what so ever. The closest I had ever come to C++ is alphabet soup. :joy:

I see what you are doing... I like the rotary switch (because I suggested it) but now how do you convert the BCD to something that you can access the array with directly? Wait... reading your statement again...

This statement indicates your code was working. You are not using a rotary switch in the most recent photo, rather you were using 2 slide switches. When the switch on D2 was off, meter[0] was selected and when the switch was on, meter[1] was selected. If you swap out the slide switch for the rotary switch it may make more sense to you since you will be selecting a position instead of manually creating a BCD with slide switches.

Aha. That would make sense. I will try both. I wonder what will happen when you select the higher numbers on the rotary switch?

Rotary%20Switch

Thanks Tim

If you use your unaltered code, then you can only use positions 0 and 1 at the moment. If you un-comment the other 2 elements in meters (i.e. meters[2] and meters[3]) then you can use positions 2 and 3 as well. If you don’t want to add all the meter combinations right now, you should add some error checking so that your dip index never exceeds the array bounds. For example:

if (dip >= sizeof(meters)) {
  dip = sizeof(meters) - 1;
}

Awesome. Should I just paste that right under the last element?

dip = 0;
if (digitalRead(D2)) dip+=1;
if (digitalRead(A2)) dip+=2;
if (digitalRead(B4) dip+=4;
if (digitalRead(B5) dip+=8;

if (dip >= sizeof(meters)) {
  dip = sizeof(meters) - 1;
}

If I wanted to go ahead and just add generic values for all ten meters how would I declare multiple pins being high at the same time? Assuming that is what happens looking at the picture from the meter data sheet. Example rotary set to 7. Pins 1,2,and 4 are high. So I am guessing that in this example D2, A2, and B4 are high. Will the meter_lookup just go to the seventh array and use that formula?

Oh by the way. I tested your code change for the use on the little test switches and it worked like a charm… but I guess you knew that was going to work. ha ha.

I really appreciate all the help.

Cheers,
Tom

Yes. The placement of the dip index check is correct.

You should be able to use your code with the rotary switch without any alteration. When you select position 7, your code will add 4+2+1 = 7. And so it will select meters[7] from the array. In fact, you were already setup to use the rotary switch from the first post today, you just got a little hung up on how you were using those slide switches in place of a rotary switch.

Glad it’s working out! :sunglasses:

Thank you. That just seems too easy. lol.

Yea I asked the right question and then did the test with the wrong parts… very typical of me over here. :joy:

Normally I can blame the wine but its too early here for that excuse.

Thank you again. I will update this post with pics when I get it all working.

Thank you @ninjatill, @peekay123, and @seulater

Your codes and help worked great. My new rotary dip showed up today. I put in some random numbers and wired up to a Photon to test. Worked like a champ. Here is a pic and the code if someone needs an example.

Pic:

CODE:

int dip;
int meters;

void setup() {
pinMode(D2, INPUT_PULLDOWN);
pinMode(A2, INPUT_PULLDOWN);
pinMode(D6, INPUT_PULLDOWN);
pinMode(D7, INPUT_PULLDOWN);
}
void loop() {
dip = 0;
if (digitalRead(D2)) dip+=1;
if (digitalRead(A2)) dip+=2;
if (digitalRead(D6)) dip+=4;
if (digitalRead(D7)) dip+=8;
if (dip >= sizeof(meters)) {
  dip = sizeof(meters) - 1;
}
struct meter_lookup {
  float fromLow;
  float fromHigh;
  float toLow;
  float toHigh;
};

const meter_lookup meters[4] = {
 { 2139.0, 10614.0, 0.0, 200.0 },// Dip set to 0:
 { 2045.0, 10502.0, 0.0, 40.0 }, // Dip set to 1:  
 { 2045.0, 10502.0, 0.0, 100.0 },  // Dip set to 2:  
 { 2045.0, 10502.0, 0.0, 150.0 }   // Dip set to 3:  
};
float flow = map(adc, meters[dip].fromLow, meters[dip].fromHigh, meters[dip].toLow, meters[dip].toHigh);

Cheers and thanks again, this is an awesome feature for me.
Tom

3 Likes