Toy Car Power Wheels Part 3

Toy Car Power Wheels Part 3

Aug 01, 2019

John Praleston


After a few weeks off, some lengthy rewiring, and a week and a half of learning Arduino coding the Audi car is operating correctly. There are few more issues to resolve. When I went to run 12V directly to the Arduino, I was getting sporadic steering input and angle signals. I have ordered variable voltage regulators to convert the 12V incoming into 7V for the Arduino as well as running the other devices on their own power supply.

Wiring changes

  • The steering angle and gas pedal potentiometers were only able to run and read into the Arduino on a 3.3 V signal. New power and ground wires running from the Arduino were run directly to the potentiometers and back to the Arduino.
  • Individual circuits were ran to the forward/reverse switch and remote/manual switch (wiring diagram to come later). These included a 10k ohm resistor and were pinned to the Arduino. The remote manual switch needed a wiring change due to grounding the circuit and closing the signal to the Arduino when I didn’t want to.
  • Two H-bridge controllers were installed. One for the drive motors and one for the steering motor. These were required due to the need to reverse the direction of the DC motors
  • I am installing a master on off switch to avoid battery drain when the car is resting

 

Below is the working code for the car. It took quite some time to figure out how to integrate all the code together. I began step by step doing each part of the code individually and then splicing it together. One issue I had was when I was recognizing the switches, I was reading a change of value instead of reading the value itself. This was causing the switches to only work individually and not together.

Individual codes that I have. There were about 20 total iterations with various combinations of below codes

  • All inputs
    • Traxxas steering, Traxxas throttle, gas pedal potentiometer, steering potentiometer
  • Manual/Remote Switch
  • Forward/Reverse Switch
  • Throttle signal, Forward/Reverse
  • Gas pedal variable speed
  • Steering with Traxxas controller
  • Steering with Central location

 

CODE FOR AUDI CAR

 

// MANUAL VS REMOTE INT

const int  buttonPinRM = 11;    // the pin that the pushbutton is attached to

int buttonStateRM = 0;         // current state of the button

 

// FORWARD VS REMOTE INT

const int  buttonPinFR = 12;    // the pin that the pushbutton is attached to

int buttonStateFR = 0;         // current state of the button

 

// Motor Left

int en1 = 10;

int in1 = 9;

int in2 = 8;

 

// Motor Right

int en2 = 5;

int in3 = 6;

int in4 = 7;

 

// Motor Speed Values - Start at zero

int MotorSpeed1 = 0;

int MotorSpeed2 = 0;

 

//Traxxis Controller

//Throttle

byte THROTTLE_PIN = A5;

int pwm_value_t;

//Steering

byte STEERING_PIN = A4;

int pwm_value_s;

 

// Motor Steering

 

int en1s = 3;

int in1s = 2;

int in2s = 4;

int MotorSpeed3 = 0;

int InputValue;

int SteeringAngle;

int Pcontroller;

int Position;

 

 

void setup() {

 

  // initialize serial communication:

  Serial.begin(9600);

 

  // initialize the manual remote pin as a input: 11

  pinMode(buttonPinRM, INPUT);

 

  // initialize the forward reverse pin as a input: 12

  pinMode(buttonPinFR, INPUT);

 

  // Set all the motor control pins to outputs

  pinMode(en1, OUTPUT);

  pinMode(in1, OUTPUT);

  pinMode(in2, OUTPUT);

  pinMode(en2, OUTPUT);

  pinMode(in3, OUTPUT);

  pinMode(in4, OUTPUT);

  pinMode(STEERING_PIN, INPUT);

  pinMode(en1s, OUTPUT);

  pinMode(in1s, OUTPUT);

  pinMode(in2s, OUTPUT);

 

  // Start with motors disabled and direction forward

 

  // Motor Left

  digitalWrite(en1, LOW);

  digitalWrite(in1, HIGH);

  digitalWrite(in2, LOW);

 

  // Motor Right

  digitalWrite(en2, LOW);

  digitalWrite(in3, HIGH);

  digitalWrite(in4, LOW);

 

  // Motor Steering

  digitalWrite(en1s, LOW);

  digitalWrite(in1s, HIGH);

  digitalWrite(in2s, LOW);

}

 

 

 

 

 

 

 

void loop() {

 

  // Read Steering Output from Traxxas Controller - Values (Left full 1960, Right full 980)

  // Needs tuning

 

  // Steering Angle

  int sensorValueS = analogRead(A1);

  SteeringAngle = map(sensorValueS, 485, 675, 0, 1000);

  if (SteeringAngle < 510 && SteeringAngle > 500)SteeringAngle = 500;

 

  // Traxxas Position

  pwm_value_s = pulseIn(STEERING_PIN, HIGH);

  InputValue = map(pwm_value_s, 1960, 990, 0, 1000);

  if (InputValue < 530 && InputValue > 480)InputValue = 500;

 

  // Turn or not to turn

  Pcontroller=abs(InputValue-SteeringAngle);

 

  Position = (InputValue-SteeringAngle);

 

if(Pcontroller < 50)MotorSpeed3 = 0;

 

//------Steering------// Needs tuning

if(Pcontroller > 50)

{

 

if (Position > 0)

  {

    // This is Right

    digitalWrite(in1s, LOW);

    digitalWrite(in2s, HIGH);

 

    // Motor Speed - needs tune

    MotorSpeed3 = map(pwm_value_s, 1970, 990, 0, 255);

  }

  else if (Position < 0)

  {

    // This is Left

    digitalWrite(in1s, HIGH);

    digitalWrite(in2s, LOW);

 

    //Motor Speed - needs tune

    MotorSpeed3 = map(pwm_value_s, 990, 1970, 0, 255);

  }

 

  else

  {

    // Stopped

    MotorSpeed3 = 0;

  }

}

  if (MotorSpeed3 > 255)MotorSpeed3 = 255;

  if (MotorSpeed3 < 8)MotorSpeed3 = 0;

 

   // Send Motorspeed to Motor

 analogWrite(en1s, MotorSpeed3);

 delay(1);

 

  Serial.println(Pcontroller);

  Serial.print("\t");

  Serial.println(InputValue);

  Serial.print("\t\t");

  Serial.println(SteeringAngle);

  Serial.print("\t\t\t");

  Serial.println(Position);

  Serial.print("\t\t\t\t");

  Serial.println(MotorSpeed3);

 

//------THROTTLE------//

  // read the manual vs remote input pin:

  buttonStateRM = digitalRead(buttonPinRM);

 

  // read the forward reverse input pin:

  buttonStateFR = digitalRead(buttonPinFR);

 

  // read the input value for gas pedal:

  int sensorValueG = analogRead(A0);

 

  // Read Throttle Output from Traxxas Controller - Values (Full Back 1960, Full Forward 980)

  pwm_value_t = pulseIn(THROTTLE_PIN, HIGH);

 

  // -----MANUAL MODE-----

  if (buttonStateRM == HIGH) {

//  Serial.println("Manual");

 

  if (buttonStateFR == HIGH) {

//  Serial.print("\t");

//  Serial.println("Forward");

     

  digitalWrite(in1, HIGH);

  digitalWrite(in2, LOW);

  digitalWrite(in3, HIGH);

  digitalWrite(in4, LOW);

}

  if (buttonStateFR == LOW) {

// Serial.print("\t");

// Serial.println("Reverse");

 

  digitalWrite(in1, LOW);

  digitalWrite(in2, HIGH);

  digitalWrite(in3, LOW);

  digitalWrite(in4, HIGH);     

    }

   

delay(5);

  // Gas Pedal Value Map

  sensorValueG = map(sensorValueG, 240, 540, 0, 255);

 

  if (sensorValueG < 10)sensorValueG = 0;

  analogWrite(en1, sensorValueG);

  analogWrite(en2, sensorValueG);

  }

 

 

// -----REMOTE MODE-----

 

  if (buttonStateRM == LOW) {

   

// Serial.println("Remote");

 

  if (pwm_value_t < 1400)

  {

    // This is Backward

 

    // Set Motor Left backward

    digitalWrite(in1, LOW);

    digitalWrite(in2, HIGH);

 

    // Set Motor Right Backward

    digitalWrite(in3, LOW);

    digitalWrite(in4, HIGH);

 

    //Determine Motor Speeds

    MotorSpeed1 = map(pwm_value_t, 1400, 990, 0, 255);

 

  }

  else if (pwm_value_t > 1550)

  {

    // This is Forward

 

    // Set Motor Left forward

    digitalWrite(in1, HIGH);

    digitalWrite(in2, LOW);

 

    // Set Motor Right Forward

    digitalWrite(in3, HIGH);

    digitalWrite(in4, LOW);

 

    //Determine Motor Speeds

    MotorSpeed1 = map(pwm_value_t, 1550, 1970, 0, 255);

    }   

 

  else

  {

    // This is Stopped

    MotorSpeed1 = 0;

  }

 

  if (MotorSpeed1 < 10)MotorSpeed1 = 0;  

  if (MotorSpeed1 > 255)MotorSpeed1 = 255;

  

 

  // Set the motor speeds

  analogWrite(en1, MotorSpeed1);

  analogWrite(en2, MotorSpeed1);

  delay(5);

 

// Serial.print("\t\t");

// Serial.print(MotorSpeed1);

 

    }

}

Tags

Archives