// makes it so that behavior of vehicle can be controled via 4 buttons
int buttonPin1 = 4; // each button pin corresponds to a different direction
int buttonPin2 = 5;
int buttonPin3 = 6;
int buttonPin4 = 7;
int motorPin1 = 8; // pint to where IN 1 of h-bridge connects to
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int buttonState1 = 0; // starts with car stoped
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(motorPin1, OUTPUT); // makes it so that connected pins work as outputs
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1); // set button state to the input from that button
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
// fowards
if(buttonState1 == HIGH){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4,LOW);
}
//backwards
if(buttonState2 == HIGH){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4,HIGH);
}
//left
if(buttonState3 == HIGH){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4,LOW);
}
//right
if(buttonState4 == HIGH){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4,HIGH);
}
delay(350);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
Comments