# frequencies with switches

i'm trying to make a device that takes the input of a switch and makes a set frequency. That part works, what i'm having trouble with is using two switches at once to make another frequency. I thought the else if (buttonState7 == HIGH) && (buttonState8 == HIGH) would work to make freq 8 which should make 1000 Hz . Any help would be very much appreciated also if my format for posting is in need of improvement i do apologize, i tried following the guidelines but i may have missed some things.

``````int freq[] = {
200,

300,

400,

500,

600,

700,

800,

900,

1000,
};

const int a = 12; //#9 on box

const int b = 11; //#4 on box

const int c = 10; //#8 on box

const int d = 9; //#5 on box

const int e = 7; //#7 on box

const int f = 6; //#3 on box

const int g = 5; //#6 on box

const int h = 4;//#1 on box

int buttonState1 = 0;

int buttonState2 = 0;

int buttonState3 = 0;

int buttonState4 = 0;

int buttonState5 = 0;

int buttonState6 = 0;

int buttonState7 = 0;

int buttonState8 = 0;

void setup() {

pinMode(a, INPUT);

pinMode(b, INPUT);

pinMode(c, INPUT);

pinMode(d, INPUT);

pinMode(e, INPUT);

pinMode(f, INPUT);

pinMode(g, INPUT);

pinMode(h, INPUT);

Serial.begin(9600);

}

void loop() {

if (buttonState1 == HIGH) {

tone(8, freq[0]);

}

else  if (buttonState2 == HIGH) {

tone(8, freq[1]);

}

else  if (buttonState3 == HIGH) {

tone(8, freq[2]);

}

else  if (buttonState4 == HIGH) {

tone(8, freq[3]);

}

else if (buttonState5 == HIGH) {

tone(8, freq[4]);

}

else  if (buttonState6 == HIGH) {

tone(8, freq[5]);

}

else  if (buttonState7 == HIGH) {

tone(8, freq[6]);

}

else  if (buttonState8 == HIGH) {

tone(8, freq[7]);

}

else  if (buttonState7 == HIGH) && (buttonState8 == HIGH) {

tone(8, freq[8]);
}

else{

noTone(8);

}
}
``````

Have you got pulldown resistors on the input pins or are they floating in the breeze at an unknown state ?

If not then either add some or consider using INPUT_PULLUP in pinMode() to activate the built in pullup resistors, wire the buttons to take the input LOW when pressed and invert the program logic to match the hardware setup

Well, you get credit for using code tags in the first post. You shouldn't have blank lines between your code lines, though. Starting off with 6 of them isn't really useful, either. You should never use single letter variable names except where the usage and scope does not exceed a few lines.

If you test:

``````   else  if (buttonState7 == HIGH) {
``````

then the code following is executed, and the following else-if's are skipped, like

``````    else  if (buttonState7 == HIGH) && (buttonState8 == HIGH) {
``````

So in the case where either (buttonState7 == HIGH) or (buttonState8 == HIGH), the individual button code is run, and the test for both is unreachable.

I do have resistors and i apologize for any mistakes i made. Im doing a project for my work, this is step two of a much bigger piece. I have no experience with any coding prior to 3 weeks ago and they just handed me the task and said good luck lol. the switch box is actually to simulate a set of relays being controlled by a computer pannel in a locomotive, all which is way over my head. im just a loco mechanic dont know why they picked me but i cant let them down. thanks for any help

OK, understood

You have

``````  else  if (buttonState7 == HIGH)
``````

then later

``````  else  if (buttonState7 == HIGH) && (buttonState8 == HIGH)
``````

Think about it. The second test will never happen if the first one returns true.

A quick fix, until you decide to do add something else, would be to do the combination test before the single test. The combo could then return false and fall through to the single test which could still return true.

Thank you

Rather than two separate checks on buttonState7 I would recommend this style

``````if (buttonState7 == HIGH) {
if (buttonState8 == HIGH) {
// do stuff
}
else {
// do other stuff
}
}
``````

...R

Got it working, thanks UKHeliBob, your tip along with a little debounce did the trick!