Making a robot go straight using encoder motors and micro-controller (Arduino)
Simply turning the motors on and off won't get you far when it comes to driving straight. Motors and the robot itself have some inertia, so they won't just stop in place when you switch them off. Much better approach would be to implement a PID controller, or at least proportional part of it.

How proportional controller works? 


First you measure how many ticks you get in a certain, constant period, i.e. 10 ms, for each of wheels.
You compare the result, with desired target value, let's say 40 ticks.
When measured value is greater than the target you slow down motor a bit, when it's smaller you speed it up.


On Arduino, where you control a motor speed by PWM it looks like:

void loop() {
  if(micros() - t &gt; PERIOD) {
    error = target - ticks;
    newPWM = error * proportionalGain;
    runMotor(newPWM);
    t = micros();
  }
}


This example is for only one motor, but I hope you get the general idea. You will need to tune PERIOD and proprtionalGain yourself. Remember, that valid PWM range for Arduino is 0-255.

Problems you will probably encounter
Even when you have your motor running at the exactly same speed, you may still observe that your robot will turn. Here are few possible reasons:
 - Unequal wheels diameters
 - Uneven mass distribution in the robot (more mass on one wheel than on another)
 - Misalignment of wheels
Here's a great paper, on a method called UMBmark developed by Johann Borenstein, that helps to counter these errors: http://www-personal.umich.edu/~johannb/Papers/paper60.pdf