New to arduino and need some help

Hey all,

I am working on a project which involves using an arduino microcontroller. To give you a bit of an idea of what I'm doing, I have two buttons that will control an actuator. One button controls the actuator to move one way (positive X) and vise versa for the second button (negative X). I also have a third output to control a fan that will turn on after the actuator has moved to the end of the track (approximately 4 seconds). I am new to using the arduino code and am having some issues. The problem I am having is that as soon as I power up the arduino it begins running the loop for the second button and does not wait for an input, it will continuously cycle through the loop for the second button. I am trying to figure out what I may have done wrong in the coding but not quite sure what is wrong as I am new to using arduino. I will post the code below, I have included comments in my code (sorry if they sound really simplified but I like to spell it out as I'm coding so I know what I did) but if anything else is needed feel free to ask. Thanks in advance!

//To begin the pins being used must be defined as a variable, this means that whenever the variable is called
//the Arduino will associate that name to the pin (ie. the vairable is equal to the pin number)

const int DP1 = 8; //Drop point 1 connected to pin 8
const int DP2 = 12; //Drop point 2 connected to pin 12
const int LA_DP1 = 4; //Linear actuator drop point 1 connected to pin 4
const int LA_DP2 = 5; //Linear actuator drop point 2 connected to pin 5
const int FAN = 6; //Fan will be connected to pin 6

int val1 = 0; //val1 will store the value of dp1
int val2 = 0; //val2 will store the value of dp2

//Next the pins each variable have been assigned to need to be defined as either an input or output

void setup(){
pinMode(DP1, INPUT); //dp1 is set as an input, pin 8 is an input
pinMode(DP2, INPUT); //dp2 is set as an input, pin 12 is an input
pinMode(LA_DP1, OUTPUT); //la_dp1 is set as an output, pin 4 is an output
pinMode(LA_DP2, OUTPUT); //la_dp1 is set as an output, pin 5 is an output
pinMode(FAN, OUTPUT); //FAN is set as an output, pin 6 is an output
}

//Finally, now that all the variables have been defined and each varaible has been designated as either
//an input or an output, the heart of the coding can be written. This is what will actually control the
//pneumatic system, everything that needs to take place when either drop point button is pushed will
//be stated in the void loop below.

void loop(){

val1 = digitalRead(DP1); //reads the input value at drop point 1 and stores it
val2 = digitalRead(DP2); //reads the input value at drop point 2 and stores it

//Check if the button is high (pressed) or low (not pressed) for either drop point
if (val1 == HIGH){
digitalWrite(LA_DP1, HIGH); //Activates linear actuator to move in the direction of drop point 1
delay(4000); //Wait for 4 seconds (ie. the amount of time it takes the actuator to move from one end of the track to the other)
digitalWrite(LA_DP1, LOW); //Shuts off the actuator after it has reached drop point 1
delay(1000); //Wait a second
digitalWrite(FAN, HIGH); //Fan turns on
delay(60000); //Wait for a minute
digitalWrite(FAN, LOW); //Sets the fan output to low (off), this will shut the fan off after a minute
}
if (val2 == HIGH){
digitalWrite(LA_DP2, HIGH); //Activates linear actuator to move in the direction of drop point 2
delay(4000); //Wait for 4 seconds (ie. the amount of time it takes the actuator to move from one end of the track to the other)
digitalWrite(LA_DP2, LOW); //Shuts off the actuator after it has reached drop point 2
delay(1000); //Wait a second
digitalWrite(FAN, HIGH); //Fan turns on
delay(60000); //Wait for a minute
digitalWrite(FAN, LOW); //Sets the fan output to low (off), this will shut the fan off after a minute
}
else {
digitalWrite(LA_DP1, LOW); //If button is not pushed, linear actuator will remain at a low (off) state
digitalWrite(LA_DP2, LOW); //If button is not pushed, linear actuator will remain at a low (off) state
digitalWrite(FAN, LOW); //Turns the fan off if it is currently on since the system isn't being used
}
}

You're not using the internal pull-up resistors, so do you have a pull-up (or pull-down) resistor on each of your input pins?

No I don't, what is the purpose of these resistors, I did notice them on other similar examples but didn't think it was necessary, looks like I was wrong haha.

MasterJES:
No I don't, what is the purpose of these resistors, I did notice them on other similar examples but didn't think it was necessary, looks like I was wrong haha.

Without them, the pins will float and there is no guarantee they will be at HIGH or LOW. The easiest thing to do is enable the internal pull-up:

pinMode(some_pin, INPUT_PULLUP);

and wire the pin to the switch and the switch to the ground. So the switch will be active whenever the pin is pulled LOW.

ok, just so I understand you correctly, I am making pin 8 the input pin for the first button (calling it DP1) so for my code it will look something like this:

const int DP1 = 8;

pinMode(DPI, INPUT_PULLUP);

Yes. You just have to keep in mind that HIGH means the switch is not been pressed, and LOW means it is.

Just made the revisions to my code and it works! Thanks a lot Arrch, really appreciate it!