Audrey Leung
Created December 21, 2015

Remote-Controlled Car (Pt 3): Push-button Remote Control

IntermediateWork in progress3,520
Remote-Controlled Car (Pt 3): Push-button Remote Control

Things used in this project

Story

Read more

Schematics

Dpad schematic

Dpad schematic image

Code

D-pad remote control

C/C++
Use this to create a d-pad remote control for motors to make car go forwards, backwards, and turn left/right.
// connect motor controller pins to Arduino digital pins
// MOTOR A
//int enA = 10; //to control speed
int motorPin1 = 9;
int motorPin2 = 8;
// MOTOR B
//int enB = 5; //to control speed
int motorPin3 = 7;
int motorPin4 = 6;

// set button pins
int buttonPinBack = 2; //backwards; green
int buttonPinLeft = 3; //left; blue
int buttonPinFwd = 4; //forward; yellow
int buttonPinRight = 5; //right; purple

// initialize button states
int buttonStateBack = 0;
int buttonStateLeft = 0;
int buttonStateFwd = 0;
int buttonStateRight = 0;  

void stop() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);  
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
}

void setup()
{
  //serial monitor 9600 for printing to console
  Serial.begin(9600);
   
  // set all the motor control pins to outputs
  //pinMode(enA, OUTPUT);
  //pinMode(enB, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);

  stop();
  
  //initialize the button pins as INPUT:
  pinMode(buttonPinBack, INPUT);
  pinMode(buttonPinLeft, INPUT);
  pinMode(buttonPinFwd, INPUT);
  pinMode(buttonPinRight, INPUT);
}

void dumpState() {
  Serial.print("\t");
  Serial.print(buttonStateBack);
  Serial.print("\t");
  Serial.print(buttonStateLeft);
  Serial.print("\t");
  Serial.print(buttonStateFwd);
  Serial.print("\t");
  Serial.print(buttonStateRight);
  Serial.println();
  delay(50);
}

void remoteControl()
{
  buttonStateBack = digitalRead(buttonPinBack);
  buttonStateLeft = digitalRead(buttonPinLeft);
  buttonStateFwd = digitalRead(buttonPinFwd);
  buttonStateRight = digitalRead(buttonPinRight);
  dumpState();
  
  if (buttonStateBack == HIGH) {   // BACKWARD
    // this function will run the motors in both directions at a fixed speed
    // turn on motor A
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    //xxxxx analogWrite(enA, 200);  // set speed to 200 out of possible range 0~255
    // turn on motor B
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
    //xxxxx analogWrite(enB, 200); // set speed to 200 out of possible range 0~255
    delay(250); 
  } 
  
  if (buttonStateLeft == HIGH) { // TURN
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);  
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH); 
    delay(250); 
  }
  
  if (buttonStateFwd == HIGH) {  // FORWARD
    Serial.print("forward");
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);  
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW); 
    delay(250);
  }
  
  if (buttonStateRight == HIGH) {  // TURN OTHER WAY
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);  
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW); 
    delay(250);
  } 
  
  if (buttonStateBack == LOW &&
      buttonStateLeft == LOW &&
      buttonStateFwd == LOW &&
      buttonStateRight == LOW) {
    stop(); 
  }
}


void loop()
{
  remoteControl();
}

Credits

Audrey Leung

Audrey Leung

9 projects • 15 followers

Comments