SNS0 and SNS1 Arduino Motor Sheild r3

Does anyone have a sample code for obtaining the voltage from the SNS0 and SNS1 from a brushed motor? I can display the values from the A0 and A1 pins, and find an average. Is A0 for the A+ and A-? A1 for B+ and B-? However, I do not know the best way to change that into voltage.

Also, the values for the A0 and A1 seem to change depending on the amount of charge I have left in my batteries. I understand why this occurs but is there a way to adjust this in the code or in the way that I power the arduino and motor shield? I would like to keep just batteries and not have to plug in my project.

I would like to get two values one for the amount of voltage on my brushed motor, and another value that I can use to change what my motor does if it is too high or too low.

Here is part of the code that obtains the values. I know it needs work.

const int analog0 = A0; //SNS0? Motor port A+ A-?

int sensorVal = 0;

int sensorRead () //values vary quite a bit, I need an average
{
int sensorVal0 = analogRead(analog0);
delay (10);
int sensorVal1 = analogRead(analog0);
int sensorVal = (sensorVal0 + sensorVal1)/2;
}

void setup()
{
Serial.begin(9600);
}

void loop()
{
sensorRead (); //gathers average values from SNS0?
Serial.print(sensorVal);
Serial.println(" motor"); // Hopefully these are the values that I need
}

The SNS0 and SNS1 pins don't measure voltage, they measure the current to each motor. Is that what you meant?

To average analogRead each pin many times (not just 2) and average (don't call delay!)

The changing current with battery state probably represents the reduced current capacity from the batteries as
they discharge. I presume the Arduino is getting a stable 5V via its regulator?

MarkT:
The SNS0 and SNS1 pins don't measure voltage, they measure the current to each motor. Is that what you meant?

I did mean voltage but I should be measuring the current across the motors. Thanks.

My plan is to have the Robotic Car change directions when the motor stalls do to an obstacle in the way. I want to measure the current across the motor so that when it stalls and current increases it will change directions. I've uploaded a video to http://youtu.be/yTfU8XdpGds so that you can see what I am doing.

Would this ( http://arduino.cc/en/Tutorial/Smoothing ) be a good code to modify for the average?

I powering the arduino motor shield with 6 AA batteries which is in turn powering the arduino. I am not powering the arduino separate from the motors. Should it be? Because it can be.

Here is the complete code. Sorry about some of the comments. I am still new to arduino. Any help you can give me is appreciated.

#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  7
#define  ECHO_PIN     6
#define MAX_DISTANCE 176 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceCm = 0;
int opto = 2; //controls servo power which prevents it from humming 
int opto2 = 10; //controls lights
int opto3 = 5; //controls lights
int times = 1; 
int time = 1;

const int analog0 = A0;  //subtract ambient analog noise 
const int analog1 = A1;  // Analog input pin that the potentiometer is attached to
int sensorVal = 0;

//servo setup
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(4);  // attaches the servo on pin 9 to the servo object 
  pinMode(13, OUTPUT);   //Initiates Motor Channel B pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel B pin
  pinMode(opto, OUTPUT);
  pinMode(opto2, OUTPUT);
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  servoset(85,800); //servo faces forward
} 
void loop() 
{
  if (times == 1) {
    servoset(85,700); 
    times = 0; 
  } //turn off servo set after first read, I will reset position later
  servoread (); //reading the sonar and returning value as distanceCm

  int x = DistanceCm; //I like x it is easier to type
  x = map(x , 0 , 177, 0, 160); //mapping the values because the greates distance is about 177 read by the cheap servo
  if (x==0){
    x = 160;
  } //out of range value is normally 0 but because I mapped it at 1 I made it equal to 160 because I want to control the motor speed with these values

  carmovement (132);

  digitalWrite(opto2, LOW);
  analogWrite(opto3, 0);
  sensorRead (); 

  //Serial.print(x); Serial.println(" cm"); // I want to see what is going on to debug 

  Serial.print(sensorVal); 
  Serial.println(" motor"); // I want to see what is going on to debug 

  while (x < 30)
  {
    digitalWrite(opto2, HIGH);
    analogWrite(opto3, 255);

    carstop(1000);   
    servoset(185,800); //servo is turning left direction
    servoread (); //reading with ping sensor

    int xl = DistanceCm; 
    xl = map(xl , 0 , 177, 0, 160); 

    if (xl==0){
      xl = 160;
    }
    //Serial.print(xl); 
    //Serial.println(" left");


    servoset(0,900); //servo is turning right direction
    servoread (); //reading with ping sensor

    int xr = DistanceCm; 
    xr = map(xr , 0 , 177, 0, 160); 

    if (xr==0){
      xr = 160;
    }
    //Serial.print(xr); 
    //Serial.println(" right"); 

    servoset(85,800); //servo faces forward
    servoread (); //reading with ping sensor

    if (xl < 40 && xr > xl) // if the left distance is less than 40cm and the right distance is greater than the left
    {
      servoread (); //read the ping
      int x = DistanceCm; //intialize x again
      while (x < 20) //while the forward distance is less than 20
      {
        carmovement(-2); //car will reverse for specified distance
        break; // then break the while loop
      }
      carbackright(); // car will back up and go right because left is blocked
      digitalWrite(8, HIGH); //Engage the Brake for Channel B
      //Serial.print("carturnright"); // to debug is serial screen
      break; //break the while loop
    }
    if (xr < 40 && xr < xl)
    {
      servoread ();
      int x = DistanceCm;
      while (x < 20)
      {
        carmovement(-113); //reverse for specified distance
        break;//break the while loop
      }
      carbackleft();
      digitalWrite(8, HIGH); //Engage the Brake for Channel B
      //Serial.print("carturnleft");
      break; //break the while loop
    }  
    if (xl > 40 && xr > 40)
    {
      servoread ();
      int x = DistanceCm;
      while (x < 20)
      {
        carmovement(-113);
        break; //break the while loop
      }
      carstop(100); 
      carbackleft();
      digitalWrite(8, HIGH); //Engage the Brake for Channel B
      //Serial.print("carturnleft");
      break; //break the while loop
    }
  }
  if (sensorVal > 230 ) {
    carbackright ();
  }

  if (//sensorValue < 10 && 
  time == 1) 
  {//carbackright (); 
    time = 2; 
    return;
  }
  if (//sensorValue < 10 && 
  time == 2) 
  {//carbackleft (); 
    time = 1; 
    return;
  }
}

void servoset(int x, int y)
{
  digitalWrite(opto, HIGH);
  myservo.write(x);
  delay(y);
  digitalWrite(opto, LOW);
}
int servoread ()
{
  delay(32);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();  
  return (DistanceCm); 
}

int sensorRead ()
{
  int sensorVal0 = analogRead(analog0); 
  delay (10);
  int sensorVal1 = analogRead(analog1);
  int sensorValold = (sensorVal0 + sensorVal1)/2; 
  delay (10); 
  int sensorVal2 = analogRead(analog0); 
  delay (10);
  int sensorVal3 = analogRead(analog1);
  int sensorValnew = (sensorVal2 + sensorVal3)/2; 
  sensorVal = (sensorValnew + sensorValold)/2; 
}

void carback(int x)
{
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, x);   //Spins the motor on Channel A at half speed
}

void carmovement (int x)
{
  if (x > 0)
  { 
    digitalWrite(12, HIGH); //Establishes forward direction of Channel A
    digitalWrite(9, LOW);   //Disengage the Brake for Channel A
    analogWrite(3, x);   //Spins the motor on Channel A at full speed 
    delay(10); 
  }
  if (x < 0)
  {
    digitalWrite(12, LOW); //Establishes backward direction of Channel A
    digitalWrite(9, LOW);   //Disengage the Brake for Channel A
    analogWrite(3, -x);   //Spins the motor on Channel A at half speed
    delay(10); 
  }

}
int carstop (int t)
{  
  digitalWrite(8, HIGH); //Eengage the Brake for Channel B  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A
  delay (t);
}

void carbackleft ()
{
  digitalWrite(opto2, LOW);


  digitalWrite(13, HIGH); //left
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at full speed
  delay (100); 
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 145);   //Spins the motor on Channel A at half speed
  delay (2000); 
  carstop(10);
  digitalWrite(13, LOW); //right
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at half speed
  delay (100);  
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 145);   //Spins the motor on Channel A at full speed 
  delay (1700);    
  carstop(10);
}

void carbackright ()
{
  analogWrite(opto3, 0);

  digitalWrite(13, LOW); //right
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at full speed
  delay (100); 
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 135);   //Spins the motor on Channel A at half speed
  delay (2000);  
  carstop(10); 
  digitalWrite(13, HIGH); //left
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at half speed
  delay (100);  
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 125);   //Spins the motor on Channel A at full speed 
  delay (1700);   
  carstop(10); 
}