Malfuncioning Project

I am working on a project where the user presses one of five buttons, and based on whichever button they press, it flashes a Morse code message. However, it flashes all of the messages as if all the buttons were pressed. Here is my code. Can anyone figure out a problem with it?

const int buttonPin1 = 2;
const int buttonPin2 = 4;
const int buttonPin3 = 7;
const int buttonPin4 = 8;
const int buttonPin5 = 12;
const int LEDPin = 13;
const int morseDot = 250;
const int morseDash = 1000;

int buttonState1 = 1;
int buttonState2 = 1;
int buttonState3 = 1;
int buttonState4 = 1;
int buttonState5 = 1;
int i;

int writeMorseDot();
int writeMorseDash();

void setup() {
pinMode(LEDPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}

void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);

//Button One ... --- ... SOS
if(buttonState1 == HIGH) {
//Letter 1
for(i = 0; i < 3; i++){
writeMorseDot();
}

//Letter 2
for(i = 0; i < 3; i++){
writeMorseDash();
}

//Letter 3
for(i = 0; i < 3; i++){
writeMorseDot();
}
}

//Button Two .... . .-.. .-.. --- Hello
if(buttonState2 == HIGH) {
//Letter 1
for(i = 0; i < 4; i++){
writeMorseDot();
}

//Letter 2
writeMorseDot();

//Letter 3
writeMorseDot();
writeMorseDash();
writeMorseDot();
writeMorseDot();

//Letter 4
writeMorseDot();
writeMorseDash();
writeMorseDot();
writeMorseDot();

//Letter 5
for(i = 0; i < 3; i++){
writeMorseDash();
}
}

//Button Three --. --- --- -.. -... -.-- . Goodbye
if(buttonState3 == HIGH) {
//Letter 1
writeMorseDash();
writeMorseDash();
writeMorseDot();

//Letter 2
for(i = 0; i < 3; i++){
writeMorseDash();
}

//Letter 3(same as letter 2)
for(i = 0; i < 3; i++){
writeMorseDash();
}

//Letter 4
writeMorseDash();
writeMorseDot();
writeMorseDot();

//Letter 5
writeMorseDash();
writeMorseDot();
writeMorseDot();
writeMorseDot();

//Letter 6
writeMorseDash();
writeMorseDot();
writeMorseDash();
writeMorseDash();

//Letter 7
writeMorseDot();
}

//Button Four .- .-. -.. ..- .. -. --- Arduino
if(buttonState4 == HIGH) {
//Letter 1
writeMorseDot();
writeMorseDash();

//Letter 2
writeMorseDot();
writeMorseDash();
writeMorseDot();

//Letter 3
writeMorseDash();
writeMorseDot();
writeMorseDot();

//Letter 4
writeMorseDot();
writeMorseDot();
writeMorseDash();

//Letter 5
writeMorseDot();
writeMorseDot();

//Letter 6
writeMorseDash();
writeMorseDot();

//Letter 7
for(i = 0; i < 3; i++){
writeMorseDash();
}
}

//Button Five -.-- .- -.-- YAY
if(buttonState5 == HIGH) {
//Letter 1
writeMorseDash();
writeMorseDot();
writeMorseDash();
writeMorseDash();

//Letter 2
writeMorseDot();
writeMorseDash();

//Letter 3
writeMorseDash();
writeMorseDot();
writeMorseDash();
writeMorseDash();

}
}

int writeMorseDot()
{
digitalWrite(LEDPin, HIGH);
delay(morseDot);
digitalWrite(LEDPin, LOW);
delay(morseDot);
}

int writeMorseDash()
{
digitalWrite(LEDPin, HIGH);
delay(morseDash);
digitalWrite(LEDPin, LOW);
delay(morseDash);
}

Have you got pulldown resistors on the pins?

You're looking for a high which means you have the pins wired (edit: switched) to 5V for a push. BUT there's no guarantee they're not high anyway: you need a resistor to ground to force a pin low when it's not pushed.

Have a look at this tutorial.

That said, it's easier to use the built-in pullUPs and reverse your logic in the program (look for a low), by using :

pinMode(xxx, INPUT_PULLUP);

Ah, and you also declare the pins as 1 right at the top of the sketch.... so no wonder they're high already.
Change....
int buttonState1 = 1;
to....
int buttonState;

Ignore that, it's crap as pointed out a few posts later.

While you are at it, go and read the instructions, then go back and modify your first post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, your code can easily be meaningless.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

JimboZA:
Ah, and you also declare the pins as 1 right at the top of the sketch.... so no wonder they're high already.

Change....
int buttonState1 = 1;to....int buttonState;

It's true that he unnecessarily initialises the button states as 1, but that's not the cause of the problem because he reads the buttons right at the beginning of the 'loop()', before taking any action.

It's gotta be a hardware problem. No pulldown resistors or resistors to 5V instead of to GND.

OldSteve:
It's true that he unnecessarily initialises the button states as 1, but that's not the cause of the problem because he reads the buttons right at the beginning of the 'loop()', before taking any action.

Oh yeah, brain fart here.

Edit.... OP, also you should furnish us with a schematic so we're not speculating about the state of the pins when the buttons aren't pushed.

JimboZA:
OP, also you should furnish us with a schematic so we're not speculating about the state of the pins when the buttons aren't pushed.

I agree Jim. It would make things much easier in many cases, not just this one. :slight_smile: