user37681: I created a setup in which a potentiometer controls a dc motor, with current flow through the dc motor controlled by an H-bridge.
The principle is as follows:
With the potentiometer dial set to the mid-point, the motor is off. Rotating the potentiometer clockwise would cause the motor to rotate in one direction, with speed dictated by the displacement of the potentiometer from the mid-point: the greater the displacement from mid-point, the greater the speed of rotation of the motor. If the potentiometer is rotated anti-clockwise, away from mid-point, the motor would rotate in the opposite direction from before; again, with speed dictated by the extent of displacement of the potentiometer from mid-point.
The loop has three functions, where HB_12EN is to enable the drivers, HB_1A is the first driver input, and HB_2A is the second driver input (H-Bridge pins 1, 2 and 7, respectively) as per this datasheet:
http://www.ti.com/lit/ds/symlink/sn754410.pdf
To summarise their functions: HB_12EN allows voltage to be applied to the the device controlled by the H-Bridge (in this case a motor), and is pulse-wave-modulated. HB_1A and HB_2A control the flow of current through the motor, setting its angular velocity. HB_1A set HIGH and HB_2A set LOW directs current one way through the motor, and HB1A set LOW and HB_2A set HIGH directs current the other way through the motor.
The loop is:
//OUTPUTS
int const HB_12EN = 9; //pwm pin
int const HB_1A = 2;
int const HB_2A = 3;

//INPUTS
int const POT_MOT = 0; //A0, analog 0

//GLOBAL VARIABLES
unsigned int pot_val = 0; //ADC value from potentiometer
unsigned int grp_num = 1;

pot_val = analogRead(POT_MOT);
grp_num = setMotSwitch(HB_12EN, HB_1A, HB_2A, pot_val, grp_num);
setMotSpeed(HB_12EN, pot_val, grp_num);

The two (non-library) functions are:
unsigned int setMotSwitch(int const motor_reg, int const first_motor, int const sec_motor, unsigned int pot_value, unsigned int group_num) //grp 0: 0-383; grp 1: 384-639; grp 2: 640-1023
{
  if (pot_value >= 0 && pot_value < 384)
  {
    if (group_num != 0)
    {
       digitalWrite(motor_reg, LOW);
       digitalWrite(first_motor, HIGH);
       digitalWrite(sec_motor, LOW);
       group_num = 0;
    }
  }
  else if (pot_value > 383 && pot_value < 640)
  {
    if (group_num != 1)
    {
      digitalWrite(motor_reg, LOW);
      digitalWrite(first_motor, LOW);
      digitalWrite(sec_motor, LOW);
      group_num = 1;
    }
  }
  else if (pot_value > 639 && pot_value <= 1023)
  {
    if (group_num != 2)
    {
      digitalWrite(motor_reg, LOW);
      digitalWrite(first_motor, LOW);
      digitalWrite(sec_motor, HIGH);
      group_num = 2;
    }
  }
  else
  {
      digitalWrite(motor_reg, LOW);
      digitalWrite(first_motor, LOW);
      digitalWrite(sec_motor, LOW);
      group_num = 0;
  }
  return group_num;
}

void setMotSpeed(int const motor_reg, unsigned int pot_value, unsigned int group_num)
{
  unsigned int mot_speed = 0;
  if (group_num == 0)
  {
    mot_speed = map(pot_value, 0, 383, 255, 0);
    mot_speed = constrain(mot_speed, 0, 255);
    analogWrite(motor_reg, mot_speed);
  }
  else if (group_num == 1)
  {
    if (pot_value > 500 && pot_value < 523)
    {
      digitalWrite(motor_reg, HIGH); //braking
    }
    else
    {
      digitalWrite(motor_reg, LOW);
    }
  }
  else if (group_num == 2)
  {
    mot_speed = map(pot_value, 640, 1023, 0, 255);
    mot_speed = constrain(mot_speed, 0, 255);
    analogWrite(motor_reg, mot_speed);
  }
}

I tried to proceed as follows:
As the potentiometer would produce an ADC input with 1024 states, I divided those 1024 states in to 8 groups, the first three groups, covering the first 384 states (0-383) correspond to a dc motor direction (let's say anticlockwise), representing different speeds of rotation (ignoring issues of many-to-one mapping). The middle 2 groups, 256 states (384-639), correspond to a stationary motor. The last three groups, 384 states (640-1023), correspond to motor rotation in the other direction (in this example, clockwise). The ADC inputs have to be translated in to analog outputs with 256 possible states (0-255), so I map each range as appropriate; for 0-383, the mapping has to be reversed, so an input of 383 corresponds to an input of 0. The middle range (384-639) is stationary, but I tried to make a small "sub"-range in the middle a "braking range", I believed this is achieved by setting both H-bridge inputs to LOW. The 640-1023 range mapping is 640 to 0 and 1023 to 255.
Hopefully that makes sense. It seems to work, the issue I am having is that the motor seems to achieve a greater speed in one direction compared to the other: the maximum rotation speed produced in the motor is greater when the potentiometer is at its maximum in one direction compared to the other. I am pretty sure the code above gives an "even" mapping from the potentiometer to the motor for both directions. Is this, then, something to do with hardware?, and is it something to be anticipated in these kinds of setups?
Any help would be appreciated. If anyone has any advice on the best way to combine a potentiometer -or similar- input with a motor output let me know. The "braking" functionality is also a little confusing.

user4143: Personally I wouldn't bother with "groups". Just do simple maths:
int velocity = analogRead(POT_MOT) - 512;

velocity now gives you a value from -512 to +511.
bool dir = (velocity >= 0);

dir is now true for a positive value (0 is considered positive) or false for negative.
velocity = abs(velocity);

velocity is now made positive if it is negative.
So you now have two variables: dir which sets the direction, and velocity which sets the speed.
Now you can "offset" it:
velocity = map(velocity, 0, 512, -128, 255);

velocity is now changed to fit into the range -128 to 255. Anything above 0 is a valid speed, anything below is the "dead zone" and stops the motor.
if (velocity <= 0) {
    // If it's negative or zero stop
    digitalWrite(motor_reg, LOW);
    digitalWrite(first_motor, LOW);
    digitalWrite(sec_motor, LOW);
} else {
    if (dir) { // Pot was positive
        analogWrite(motor_reg, velocity);
        digitalWrite(first_motor, LOW);
        digitalWrite(sec_motor, HIGH);
    } else { // Pot was negative
        analogWrite(motor_reg, velocity);
        digitalWrite(first_motor, HIGH);
        digitalWrite(sec_motor, LOW);
    }
}

Adjust the negative value in the map to change the size of the "dead zone".

37681: <p>I created a setup in which a potentiometer controls a dc motor, with current flow through the dc motor controlled by an H-bridge.
The principle is as follows:
With the potentiometer dial set to the mid-point, the motor is off. Rotating the potentiometer clockwise would cause the motor to rotate in one direction, with speed dictated by the displacement of the potentiometer from the mid-point: the greater the displacement from mid-point, the greater the speed of rotation of the motor. If the potentiometer is rotated anti-clockwise, away from mid-point, the motor would rotate in the opposite direction from before; again, with speed dictated by the extent of displacement of the potentiometer from mid-point.</p>

<p>The loop has three functions, where HB_12EN is to enable the drivers, HB_1A is the first driver input, and HB_2A is the second driver input (H-Bridge pins 1, 2 and 7, respectively) as per this datasheet:
<a href="http://www.ti.com/lit/ds/symlink/sn754410.pdf" rel="nofollow noreferrer">http://www.ti.com/lit/ds/symlink/sn754410.pdf</a></p>

<p>To summarise their functions: HB_12EN allows voltage to be applied to the the device controlled by the H-Bridge (in this case a motor), and is pulse-wave-modulated. HB_1A and HB_2A control the flow of current through the motor, setting its angular velocity. HB_1A set HIGH and HB_2A set LOW directs current one way through the motor, and HB1A set LOW and HB_2A set HIGH directs current the other way through the motor.</p>

<p>The loop is:</p>



<pre class="lang-c++ prettyprint-override"><code>//OUTPUTS
int const HB_12EN = 9; //pwm pin
int const HB_1A = 2;
int const HB_2A = 3;

//INPUTS
int const POT_MOT = 0; //A0, analog 0

//GLOBAL VARIABLES
unsigned int pot_val = 0; //ADC value from potentiometer
unsigned int grp_num = 1;

pot_val = analogRead(POT_MOT);
grp_num = setMotSwitch(HB_12EN, HB_1A, HB_2A, pot_val, grp_num);
setMotSpeed(HB_12EN, pot_val, grp_num);
</code></pre>

<p>The two (non-library) functions are:</p>

<pre class="lang-c++ prettyprint-override"><code>unsigned int setMotSwitch(int const motor_reg, int const first_motor, int const sec_motor, unsigned int pot_value, unsigned int group_num) //grp 0: 0-383; grp 1: 384-639; grp 2: 640-1023
{
  if (pot_value &gt;= 0 &amp;&amp; pot_value &lt; 384)
  {
    if (group_num != 0)
    {
       digitalWrite(motor_reg, LOW);
       digitalWrite(first_motor, HIGH);
       digitalWrite(sec_motor, LOW);
       group_num = 0;
    }
  }
  else if (pot_value &gt; 383 &amp;&amp; pot_value &lt; 640)
  {
    if (group_num != 1)
    {
      digitalWrite(motor_reg, LOW);
      digitalWrite(first_motor, LOW);
      digitalWrite(sec_motor, LOW);
      group_num = 1;
    }
  }
  else if (pot_value &gt; 639 &amp;&amp; pot_value &lt;= 1023)
  {
    if (group_num != 2)
    {
      digitalWrite(motor_reg, LOW);
      digitalWrite(first_motor, LOW);
      digitalWrite(sec_motor, HIGH);
      group_num = 2;
    }
  }
  else
  {
      digitalWrite(motor_reg, LOW);
      digitalWrite(first_motor, LOW);
      digitalWrite(sec_motor, LOW);
      group_num = 0;
  }
  return group_num;
}

void setMotSpeed(int const motor_reg, unsigned int pot_value, unsigned int group_num)
{
  unsigned int mot_speed = 0;
  if (group_num == 0)
  {
    mot_speed = map(pot_value, 0, 383, 255, 0);
    mot_speed = constrain(mot_speed, 0, 255);
    analogWrite(motor_reg, mot_speed);
  }
  else if (group_num == 1)
  {
    if (pot_value &gt; 500 &amp;&amp; pot_value &lt; 523)
    {
      digitalWrite(motor_reg, HIGH); //braking
    }
    else
    {
      digitalWrite(motor_reg, LOW);
    }
  }
  else if (group_num == 2)
  {
    mot_speed = map(pot_value, 640, 1023, 0, 255);
    mot_speed = constrain(mot_speed, 0, 255);
    analogWrite(motor_reg, mot_speed);
  }
}
</code></pre>

<p>I tried to proceed as follows:
As the potentiometer would produce an ADC input with 1024 states, I divided those 1024 states in to 8 groups, the first three groups, covering the first 384 states (0-383) correspond to a dc motor direction (let's say anticlockwise), representing different speeds of rotation (ignoring issues of many-to-one mapping). The middle 2 groups, 256 states (384-639), correspond to a stationary motor. The last three groups, 384 states (640-1023), correspond to motor rotation in the other direction (in this example, clockwise). The ADC inputs have to be translated in to analog outputs with 256 possible states (0-255), so I map each range as appropriate; for 0-383, the mapping has to be reversed, so an input of 383 corresponds to an input of 0. The middle range (384-639) is stationary, but I tried to make a small "sub"-range in the middle a "braking range", I believed this is achieved by setting both H-bridge inputs to LOW. The 640-1023 range mapping is 640 to 0 and 1023 to 255.</p>

<p>Hopefully that makes sense. It seems to work, the issue I am having is that the motor seems to achieve a greater speed in one direction compared to the other: the maximum rotation speed produced in the motor is greater when the potentiometer is at its maximum in one direction compared to the other. I am pretty sure the code above gives an "even" mapping from the potentiometer to the motor for both directions. Is this, then, something to do with hardware?, and is it something to be anticipated in these kinds of setups?</p>

<p>Any help would be appreciated. If anyone has any advice on the best way to combine a potentiometer -or similar- input with a motor output let me know. The "braking" functionality is also a little confusing.</p>
 4143: <p>Personally I wouldn't bother with "groups". Just do simple maths:</p>

<pre><code>int velocity = analogRead(POT_MOT) - 512;
</code></pre>

<p><code>velocity</code> now gives you a value from -512 to +511.</p>

<pre><code>bool dir = (velocity &gt;= 0);
</code></pre>

<p><code>dir</code> is now <code>true</code> for a positive value (0 is considered positive) or <code>false</code> for negative.</p>

<pre><code>velocity = abs(velocity);
</code></pre>

<p><code>velocity</code> is now made positive if it is negative.</p>

<p>So you now have two variables: <code>dir</code> which sets the direction, and <code>velocity</code> which sets the speed.</p>

<p>Now you can "offset" it:</p>

<pre><code>velocity = map(velocity, 0, 512, -128, 255);
</code></pre>

<p><code>velocity</code> is now changed to fit into the range -128 to 255. Anything above 0 is a valid speed, anything below is the "dead zone" and stops the motor.</p>

<pre><code>if (velocity &lt;= 0) {
    // If it's negative or zero stop
    digitalWrite(motor_reg, LOW);
    digitalWrite(first_motor, LOW);
    digitalWrite(sec_motor, LOW);
} else {
    if (dir) { // Pot was positive
        analogWrite(motor_reg, velocity);
        digitalWrite(first_motor, LOW);
        digitalWrite(sec_motor, HIGH);
    } else { // Pot was negative
        analogWrite(motor_reg, velocity);
        digitalWrite(first_motor, HIGH);
        digitalWrite(sec_motor, LOW);
    }
}
</code></pre>

<p>Adjust the negative value in the map to change the size of the "dead zone".</p>
