user30639: 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("Speed");
  Serial.print(" ");
  Serial.print("Brake");
  Serial.print(" ");
  Serial.print("Direction");
  delay(2000);
}

void loop() {
  if (i == 0) {
    analogWrite(MotorASpeedPin, 255); //set motor A speed
    analogWrite(MotorBSpeedPin, 255); //set motor B speed
    i++;
  }
  //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
      encoder0Pos--;
    } else {
      //If pin 4 is HIGH
      //encoder is going "forward", increase encoder position
      encoder0Pos++;
    }
    //Tell us what is happening!
    Serial.print (encoder0Pos);
    //go to the next line of code
    Serial.print(" ");
    Serial.print(encoder0PinALast);
    Serial.print(" ");
    Serial.print(n);
    Serial.print(" ");
    Serial.print(encoder0Pos);
    Serial.print(" ");
    Serial.print(SpeedA);
    Serial.print(" ");
    Serial.print(BrakeA);
    Serial.print(" ");
    Serial.print(DirectionA);
    Serial.println();
    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
}


user8308: 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:

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

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

void loop()
{
    //Update number of rotations since state change
    update_rotations();
    if(state == START)
        start();
    else if(state == GO_STRAIGHT)
        go_straight();
    else if(state == TURN_LEFT)
        turn_left();
    else if(state == TURN_RIGHT)
        turn_right();
    else
        stop();
    //This code gets executed on every loop cycle and is not dependent on state
    print_status();
    wait(100);
}


30639: <p>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.</p>

<pre><code>/* 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("Speed");
  Serial.print(" ");
  Serial.print("Brake");
  Serial.print(" ");
  Serial.print("Direction");
  delay(2000);
}

void loop() {
  if (i == 0) {
    analogWrite(MotorASpeedPin, 255); //set motor A speed
    analogWrite(MotorBSpeedPin, 255); //set motor B speed
    i++;
  }
  //Set values to write to arduino
  if (encoder0Pos &lt; 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) &amp;&amp; (n == HIGH)) {
    //Check to see if pin 4 is LOW -&gt; this determines which direction the encoder is going
    if (digitalRead(encoder0PinB) == LOW) {
      //encoder is going "backwards" decrease encoder position
      encoder0Pos--;
    } else {
      //If pin 4 is HIGH
      //encoder is going "forward", increase encoder position
      encoder0Pos++;
    }
    //Tell us what is happening!
    Serial.print (encoder0Pos);
    //go to the next line of code
    Serial.print(" ");
    Serial.print(encoder0PinALast);
    Serial.print(" ");
    Serial.print(n);
    Serial.print(" ");
    Serial.print(encoder0Pos);
    Serial.print(" ");
    Serial.print(SpeedA);
    Serial.print(" ");
    Serial.print(BrakeA);
    Serial.print(" ");
    Serial.print(DirectionA);
    Serial.println();
    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
}
</code></pre>
 8308: <p>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:</p>

<ol>
<li>Start (reset the encoders and set the initial speed)</li>
<li>Go straight (both motors at 100%)</li>
<li>Turn left (left motor at 75%, right at 100%)</li>
<li>Turn right (right motor at 75%, left az 100%)</li>
<li>Stop (stop both motors)</li>
</ol>

<p>Example code:</p>

<pre><code>//TODO: Init program and variables
void go_straight()
{
    if(number_of_rotations &gt; 10)
    {
        state = TURN_LEFT
    }
    else
    {
        motor_left = 100;
        motor_right = 100;
    }
}
void turn_left()
{
    if(number_of_rotations &gt; 10)
    {
        state = TURN_RIGHT
    }
    else
    {
        motor_left = 100;
        motor_right = 75;
    }
}
//TODO: Implement remaining methods

void loop()
{
    //Update number of rotations since state change
    update_rotations();
    if(state == START)
        start();
    else if(state == GO_STRAIGHT)
        go_straight();
    else if(state == TURN_LEFT)
        turn_left();
    else if(state == TURN_RIGHT)
        turn_right();
    else
        stop();
    //This code gets executed on every loop cycle and is not dependent on state
    print_status();
    wait(100);
}
</code></pre>
