Help with dc motors/buttons

Hello,
I am trying to control two dc motors bi-directional, using a motorshield. I’ve got 4 tact switches connected to analog pins 0,1,2 and 3. The buttons don’t do anything, and the two motors just kinda go crazy. They just turn random directions whenever. I need one button to control forward on motor1 and one to control control backward on motor1. The 3rd and 4th buttons control forward and backward on motor2 just like on motor1. What am i doing wrong?

#include <AFMotor.h>
 
 AF_DCMotor motor2(2, MOTOR12_64KHZ);
 AF_DCMotor motor1(1, MOTOR12_64KHZ);
 int switchPin1 = 0;
 int switchPin2 = 1;
 int switchPin3 = 2;
 int switchPin4 = 3;
 int val;                       

int state = HIGH;     
int reading;           
int previous = LOW;   

long time = 0;         
long debounce = 200;  


void setup() {
  motor2.setSpeed(200);   
  motor1.setSpeed(200);   
  pinMode(switchPin1, INPUT); 
  pinMode(switchPin2, INPUT);    
  pinMode(switchPin3, INPUT); 
  pinMode(switchPin4, INPUT); 
  
}


void loop(){
  val = analogRead(switchPin1);  
  if (val == HIGH) {               
    motor1.run(FORWARD);   
  }
  if (val == LOW) {            
    motor1.run(RELEASE);   
  }
  val = analogRead(switchPin2);  
  if (val == HIGH) {               
    motor2.run(FORWARD);   
  }
  if (val == LOW) {              
    motor2.run(RELEASE);   
  }
  val = analogRead(switchPin3);   
  if (val == HIGH) {              
    motor1.run(BACKWARD);  
  }
  if (val == LOW) {           
    motor1.run(RELEASE);    
  }
  val = analogRead(switchPin4);   
  if (val == HIGH) {               
    motor2.run(BACKWARD);   
  }
  if (val == LOW) {            
    motor2.run(RELEASE);    
  }
}

Two things:

First, you are using analogRead() to read a digital input, this is not a good idea. Check some of the switch / button code in the playground.

Second, you do most likely need to debounce your switches.

Check Lady Ada's tutoroals (especially lesson 5) for a really good introduction to switches and debouncing, and why it is needed.

http://www.ladyada.net/learn/arduino/

Are you using pull-up resistors on your tact-switches?

Can I recommend that to get you going, you just concentrate on one set of switches and one motor? Once you've got that working, adding a second set is going to be easy. OTOH, debugging two lots of stuff with neither working properly is just going to confuse things. As has been said, swap your "analogRead"s for "digitalRead"s and try some pull-ups on the switches.

If you really want to use analogue pins for the switches, you should be aware that

val = analogRead(switchPin1);  
  if (val == HIGH) {              
    motor1.run(FORWARD);  
  }

won't work, unless the analogue pin reads exactly 1, which is pretty unlikely.

Thanks for the help, but i still can’t get it to work. Ive tried debouncing, adding delays and stuff, i switched the analogread to digital read and i STILL cannot get it to work. What if i tell the arduino to read it as a button press if it is only over like 3 or 4 volts or something? How would i do this?

What if i tell the arduino to read it as a button press if it is only over like 3 or 4 volts or something?

val = analogRead(0);
if (val > 700) {
motor1.run(FORWARD);
}

With the switch connected from the analogue input pin 0 and ground, and a 4K7 pull up resistor from analogue input pin 0 to +5V.

Assuming a push to make switch this will run the motor until the button is pressed. If you want it the other way round use:-
if (val < 700)