Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Andres Duarte
Created November 9, 2015

second project, week 2

Protip56
second project, week 2

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Schematics

schematic

Code

vehicle control

Arduino
// 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);
  
  
}

Credits

Andres Duarte

Andres Duarte

14 projects • 8 followers

Comments