I am not sure if what I am asking is possible. I was wondering if I would be able to program my Arduino to have my car go straight until one of the wheels hits a certain amount of wheel rotations then initiating a different code within the same causing that wheel to increase that wheels speed. I have already created a code to make it go and stop in a certain amount of rotations.

/* motorshield_code
Pins Utilized
Digital 2 = encoder0APinA 
Digital 3 (PWM) = MotorASpeed  //motorshield utilized
Digital 4 = encoder0PinB
Digital 8 = MotorBBrakePin // motorshield utilized
Digital 9 = MotorABrakePin // motorshiled utilized
Digital 11 = MotorBSpeedPin // motorshiled utilized
Digital 12 = MotorADirectionPin // motorshiled utilized
Digital 13 = MotorBDirectionPin // motorshiled utilized

//Encoder values
const int encoder0PinA = 2;
const int encoder0PinB = 4;
const int encoder1PinA = 7;
const int encoder1PinB = 6;
int encoder1Pos = 0;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int encoder1PinALast = LOW;
int n = LOW;
int i = 0;

//Motorshield Pin Values
const int MotorADirectionPin = 12; //digital pin value for A direction
const int MotorASpeedPin = 3; //analog pin value for A speed (PWM)
const int MotorABrakePin = 9; // digital pin value for A brake
const int MotorBDirectionPin = 13; //digital pin value for A direction
const int MotorBSpeedPin = 11; //analog pin value for A speed (PWM)
const int MotorBBrakePin = 8; // digital pin value for A brake
//Motor Control Values
int BrakeA = LOW; //Brake for motor A, LOW = motor off, HIGH = motor on
int BrakeB = LOW;
int DirectionA = HIGH; //Direction for motor A, LOW/HIGH are opposite (based on wiring) assume Low is forward
int DirectionB = LOW;
int SpeedA = 0; //Speed for motor A, PWM value from 0 - 255 (0 is off)
int SpeedB = 0;
int encoder0stop = 469.125; //stop value for encoder 0
int encoder1stop = 469.125; //stop value for encoder 1

void setup() {
  //Set Digital I/O pins 2 and 4 to inputs for encoder
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  pinMode (encoder1PinA, INPUT);
  pinMode (encoder1PinB, INPUT);
  //Set Digital I/O pins as outputs for motorshield
  //Analog pins are already set so don't have to initialize speed pins
  pinMode(MotorADirectionPin, OUTPUT);
  pinMode(MotorABrakePin, OUTPUT);
  pinMode(MotorBDirectionPin, OUTPUT);
  pinMode(MotorBBrakePin, OUTPUT);
  //Start serial monitor at 9600
  Serial.begin (9600);
  delay (0);
  Serial.print("Last Enc State");
  Serial.print(" ");
  Serial.print("Current Enc State");
  Serial.print(" ");
  Serial.print("Encoder Count");
  Serial.print(" ");
  Serial.print(" ");
  Serial.print(" ");

void loop() {
  if (i == 0) {
    analogWrite(MotorASpeedPin, 255); //set motor A speed
    analogWrite(MotorBSpeedPin, 255); //set motor B speed
  //Set values to write to arduino
  if (encoder0Pos < encoder0stop) {
    //if hasn't reached end encoder value
    //Direction Forward, Brake OFF, speed 125
    DirectionA = HIGH;
    DirectionB = LOW;
    BrakeA = LOW;
    BrakeB = LOW;
    SpeedA = 254;
    SpeedB = 253;
  } else {
    //if has reached encoder value
    //Direction Forward, Brake ON, speed 0
    DirectionA = HIGH;
    DirectionB = LOW;
    BrakeA = HIGH;
    BrakeB = HIGH;
    SpeedA = LOW;
    SpeedB = LOW;

  //Write all values to the arduino
  digitalWrite(MotorADirectionPin, DirectionA); //set motor A direction
  digitalWrite(MotorBDirectionPin, DirectionB); //set motor B direction
  digitalWrite(MotorABrakePin, BrakeA); //write motor A brake on/off
  digitalWrite(MotorBBrakePin, BrakeB); // write motor B brake on/off
  analogWrite(MotorASpeedPin, SpeedA); //set motor A speed
  analogWrite(MotorBSpeedPin, SpeedB); //set motor B speed
  //Read Encoder
  //This is reading PinA which should be set to pin 2 and storing it as n
  n = digitalRead(encoder0PinA);
  //Check to see if pin 2 has changed from LOW to HIGH
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    //Check to see if pin 4 is LOW -> this determines which direction the encoder is going
    if (digitalRead(encoder0PinB) == LOW) {
      //encoder is going "backwards" decrease encoder position
    } else {
      //If pin 4 is HIGH
      //encoder is going "forward", increase encoder position
    //Tell us what is happening!
    Serial.print (encoder0Pos);
    //go to the next line of code
    Serial.print(" ");
    Serial.print(" ");
    Serial.print(" ");
    Serial.print(" ");
    Serial.print(" ");
    Serial.print(" ");
    encoder0PinALast = n;
  //Set what the last read of pin 2 was so we can compare to see if it changes
  encoder0PinALast = n;
  //End Code for Encoder Reading
  • If your car is capable of having one wheel change speed, and your Arduino is capable of controlling the speed of that wheel, then of course it is possible. You just have to work out how to program it.
    – Majenko
    Feb 4, 2017 at 19:30
  • And can you make it run at different speeds? You don't give us any details about what you have - what type of motor etc - so it's hard to know what your setup can do.
    – Mark Smith
    Feb 4, 2017 at 21:16
  • yes I can make the motors run at different speeds. the motors are also DC motors and I'm using c++ coding I can also put a picture of the coding I already have
    – Katlyn
    Feb 5, 2017 at 6:23

1 Answer 1


I would say that one of the solutions would be to arrange the code in your car into a finite state machine. So your states would be something like:

  1. Start (reset the encoders and set the initial speed)
  2. Go straight (both motors at 100%)
  3. Turn left (left motor at 75%, right at 100%)
  4. Turn right (right motor at 75%, left az 100%)
  5. Stop (stop both motors)

Example code:

//TODO: Init program and variables
void go_straight()
    if(number_of_rotations > 10)
        state = TURN_LEFT
        motor_left = 100;
        motor_right = 100;
void turn_left()
    if(number_of_rotations > 10)
        state = TURN_RIGHT
        motor_left = 100;
        motor_right = 75;
//TODO: Implement remaining methods

void loop()
    //Update number of rotations since state change
    if(state == START)
    else if(state == GO_STRAIGHT)
    else if(state == TURN_LEFT)
    else if(state == TURN_RIGHT)
    //This code gets executed on every loop cycle and is not dependent on state
  • is there any way to program it with the code I already have and without rewiring my Electric Vehicle?
    – Katlyn
    Feb 25, 2017 at 19:13
  • Why do you think you would have to rewire your vehicle? My answer was abstracted in a way that it is not dependent on the actual hardware. Mar 2, 2017 at 9:21
  • You never reset the number of rotations, so it will go from straight, "directly" to turn_right.
    – aaa
    Jul 7, 2017 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.