Help with variables and if statements

Hi all,
My code needs to run 1 motor in one direction, and be controlled by 3 buttons. Each button corresponds to how long the motor will run, determined by 3 bump sensors.
When button 1 is pressed, motor runs until the first bump sensor it triggered.
When button 2 is pressed, motor runs until the second bump sensor it triggered.
When button 3 is pressed, motor runs until the third bump sensor it triggered.

lets say we press button 3; the motor will turn, and in the process, it will set off bump sensor 1 and then bump sensor 2 before it finally hits bump sensor 3 and turns off.

my problem at the moment is, when i press button 2 or 3, the first bump sensor turns the motor off, when i want it to run untill it gets to bump sensor 3.
I know the solution to my problem, just not how to implement it.
I want to define some kind of variable so the if statements can be more specific. in my code, I wrote the variables, but they are not working how I want them to. What am i doing wrong?

const int BumpinPin1 = 8; //Digital input pin for bump sensor
const int BumpinPin2 = 9; //Digital input pin for bump sensor
const int BumpinPin3 = 10; //Digital input pin for bump sensor

const int ButtonL1 = 5; //Digital input pin for Level 1 button
const int ButtonL2 = 6; //Digital input pin for Level 2 button
const int ButtonL3 = 7; //Digital input pin for Level 3 button

const int motor1Pin = 3; // one side of the H-bridge is controlled here.
const int motor2Pin = 4; // the other side of the H-bridge is controlled here
const int enablePin = 11; // H-bridge enable pin

void setup()
{
//Set the rate for data transmission in bits per second (bps)
//between the Arduino and host
Serial.begin(9600);

// Set pins to be inputs or outputs. note, it is not
// necessary to do this for analog input pins (e.g., A0)
pinMode(BumpinPin1, INPUT);
pinMode(BumpinPin2, INPUT);
pinMode(BumpinPin3, INPUT);
pinMode(ButtonL1, INPUT);
pinMode(ButtonL2, INPUT);
pinMode(ButtonL3, INPUT);
digitalWrite(ButtonL1, HIGH);
digitalWrite(ButtonL2, HIGH);
digitalWrite(ButtonL3, HIGH);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);

// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}

void loop() {
int button1 = digitalRead(ButtonL1);
int button2 = digitalRead(ButtonL2);
int button3 = digitalRead(ButtonL3);
int bump1 = digitalRead(BumpinPin1);
int bump2 = digitalRead(BumpinPin2);
int bump3 = digitalRead(BumpinPin3);
int a;

// if the switch is high, motor will turn on one direction:
if (button1 == 0) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low //
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high //
a = 1; //set variable 'a' equal to 1
delay(3);
}

if (button2 == 0) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
a = 2; //set variable 'a' equal to 2
delay(3);
}

if (button3 == 0) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
a = 3; //set variable 'a' equal to 3
delay(3);
}
if (bump1 == 1) {
if(a == 1) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low //
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low //
bump1 = 0; // set bump1 back to zero
delay(3);
}
}
if (bump2 == 1) {
if(a == 2) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);
bump2 = 0; // set bump1 back to zero
delay(3);
}
}
if (bump3 == 1) {
if(a == 2) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);
bump2 = 0; // set bump1 back to zero
delay(3);
}
}
}

You're close. Make a (you should really pick a better name than a) a global variable. Right now it is local to the loop function, so once the loop exits and comes around again its value is lost.

Delta_G:
You're close. Make a (you should really pick a better name than a) a global variable. Right now it is local to the loop function, so once the loop exits and comes around again its value is lost.

I need the first set of if statements (for the buttons) to set the value of the variable so the second set of if statements (for the bump sensors) can read the value it was last set as. Will making it global do that? I made it a global variable and renamed it to 'state' instead of 'a', but it it still not working. Its acting like in the 1st 3 if statements, after changing the motor pins, it isnt setting the variable to 1, 2, or 3.

You can set the value wherever you want if it is global.

Post the new code. Let's see what you've got.

I changed the order of the last 3 if statements thinking it would help.
When i press button 3, it should set state = 3, but after that when i press bump1 it stops the motor, even though the if statement will only go if state is = 1 as far as i under stand it.

const int BumpinPin1 = 8; //Digital input pin for bump sensor
const int BumpinPin2 = 9; //Digital input pin for bump sensor
const int BumpinPin3 = 10; //Digital input pin for bump sensor

const int ButtonL1 = 5; //Digital input pin for Level 1 button
const int ButtonL2 = 6; //Digital input pin for Level 2 button
const int ButtonL3 = 7; //Digital input pin for Level 3 button

const int motor1Pin = 3; // one side of the H-bridge is controlled here.
const int motor2Pin = 4; // the other side of the H-bridge is controlled here
const int enablePin = 11; // H-bridge enable pin
int state = 0; //-------------------global?

void setup()
{
//Set the rate for data transmission in bits per second (bps)
//between the Arduino and host
Serial.begin(9600);

// Set pins to be inputs or outputs. note, it is not
// necessary to do this for analog input pins (e.g., A0)
pinMode(BumpinPin1, INPUT);
pinMode(BumpinPin2, INPUT);
pinMode(BumpinPin3, INPUT);
pinMode(ButtonL1, INPUT);
pinMode(ButtonL2, INPUT);
pinMode(ButtonL3, INPUT);
digitalWrite(ButtonL1, HIGH);
digitalWrite(ButtonL2, HIGH);
digitalWrite(ButtonL3, HIGH);

// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);

// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}

void loop() {
int button1 = digitalRead(ButtonL1);
int button2 = digitalRead(ButtonL2);
int button3 = digitalRead(ButtonL3);
int bump1 = digitalRead(BumpinPin1);
int bump2 = digitalRead(BumpinPin2);
int bump3 = digitalRead(BumpinPin3);

// if the switch is high, motor will turn on one direction:
if (button1 == 0) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low //
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high //
state = 1;
delay(3);
}

if (button2 == 0) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
state = 2;
delay(3);
}

if (button3 == 0) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
state = 3;
delay(3);
}
if (state == 1) {
if(bump1 == 1) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low //
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low // /
delay(3);
}
}
if (state == 2) {
if(bump2 == 1) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);
delay(3);
}
}
if (state == 3) {
if(bump3 == 1) {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, LOW);
delay(3);
}
}
}

Please post code inside code tags in the future. It makes it easier to read and copy. It's the little scroll looking thingy with the <> on it above the smileys right above where you type.

How are the buttons and switches wired? Do you have pull-up or pull-down resistors? Might you have some floating inputs?

EDIT: I see now the use of internal pull-ups on the buttons. But what about the bump switches? They would need pull-downs the way the code is written.

Buttons are wired to ground and pin 5, 6, 7.
Bump switches are wired NC to GND, NO to 5v on the board, and C to pin 8.

Instead of reading all 6 switch pins on every pass through loop(), only read the ones that you care about. If switch 1 is pressed, you don't care about the states of bump pins 2 and 3. If switch 2 is pressed, you don't care about the state of bump pins 1 or 3. If switch 3 is pressed, you don't care about the state of bump pins 1 or 2.

So, don't read the pins you don't care about.

That way, if switch 3 is pressed, bump pin 3 is the only pin's state to read, and the only pin's state that matters.

PaulS:
Instead of reading all 6 switch pins on every pass through loop(), only read the ones that you care about. If switch 1 is pressed, you don't care about the states of bump pins 2 and 3. If switch 2 is pressed, you don't care about the state of bump pins 1 or 3. If switch 3 is pressed, you don't care about the state of bump pins 1 or 2.

So, don't read the pins you don't care about.

That way, if switch 3 is pressed, bump pin 3 is the only pin's state to read, and the only pin's state that matters.

So are you saying make 3 different loop()s?

No, he's saying use your state variable to decide which bump sensor to read. But I don't think that's what is causing your problems here. It shouldn't matter how many pins you read since you are only comparing the one you care about.

To me it looks like the code should be doing what you want it to do. I'm thinking the issue must be in the hardware or wiring of those bump sensors.

How are your switches wired? You are enabling the internal pullup resistors for only 3 of the 6 switches. Why those three, rather than all 6?

PaulS:
How are your switches wired? You are enabling the internal pullup resistors for only 3 of the 6 switches. Why those three, rather than all 6?

From the description of the bump switches given above, they connect to 5V in one position and GND in the other. Not like a switch that would be unconnected and floating when not made.

OP, got a link to the bump switches?

Delta_G:
From the description of the bump switches given above, they connect to 5V in one position and GND in the other. Not like a switch that would be unconnected and floating when not made.

OP, got a link to the bump switches?

Code worked flawlessly, you were right it was a mistake on the hardware. I'm pretty new to arduino, so I didn't think this would be a problem. When I was writing this code and getting your help, I only had 1 bump switch with me. I guess since the other 2 weren't plugged in it was really screwing with the code. Once all 3 were wired up it worked perfectly. Another lesson learned.
Thanks for all your help!