code for PWM signals to H bridge

I'm about to order a small motor and components to build a H-bridge.
I have written the following code and compiling gives no trouble.

Will this work?

I mean to control both directions with one potentiometer so the mapping is very important in this sketch. The value in the first mapping is inverted, will this work?

int potpin1 = A1;  // analog pin used to read the potentiometer voltage
int PWMPin1 = 1;    // PWM signal to digital pin 1
int PWMPin2 = 2;    // PWM signal to digital pin 2
int value1;
int value2;

void setup()
{ 
  // nothing happens in setup 
} 

void loop()
{ 
  if (analogRead(potpin1) < 512)
  {
    value1 = map(potpin1, 0, 511, 255, 0);
    analogWrite (PWMPin1, value1);
  }
  else if (analogRead(potpin1) > 524)
  {
    value2 = map(potpin1, 525, 1023, 0, 255);
    analogWrite (PWMPin2, value2);
  }
  else
  {
    analogWrite (PWMPin1, 0);
    analogWrite (PWMPin2, 0);
  }
}

Thanks,

Leo

The dead-band is quite narrow - I can imagine being able to turn the pot fast enough that you have both PWMs active at the same time.

If your schematic looks like what i imagine, then you need to drive both sides of the H-bridge. You have 2 choices, either drive the high side with PWM and simply analogWrite a zero to the opposite phase, or else you can drive the phases equal and opposite, something like:

void loop()
{
if (analogRead(potpin1) < 512)
{
value1 = map(potpin1, 0, 511, 255, 0);
analogWrite (PWMPin1, value1);
analogWrite (PWMPin2, (255-value1));
}
else if (analogRead(potpin1) > 524)
{
value2 = map(potpin1, 525, 1023, 0, 255);
analogWrite (PWMPin2, value2);
analogWrite (PWMPin1, (255-value2));
}
else
{
analogWrite (PWMPin1, 0);
analogWrite (PWMPin2, 0);
}
}

The second way is more typical for a servo to work, but am just getting my first Arduino so dont know how well that code would work here, it depends on if the two pins are phase coherent, ie running on the same timer of ATmega.

The simpler option to analogWrite (PWMPinX, 0); to the opposite side is pretty safe.

  if ([glow]analogRead(potpin1)[/glow] < 512)
  {
    value1 = map(potpin1, 0, 511, 255, 0);
    analogWrite (PWMPin1, value1);
  }
  else if ([glow]analogRead(potpin1)[/glow] > 524)

You'd be better off reading the potentiometer pin once, and storing that value. Then, make the decisions based on the stored value.

You mean like this?

I have broadened the dead-band.

int potpin1 = A1;  // analog pin used to read the potentiometer voltage
int PWMPin1 = 1;    // PWM signal to digital pin 1
int PWMPin2 = 2;    // PWM signal to digital pin 2
int value1;
int value2;
int pinread;

void setup()
{ 
  // nothing happens in setup 
} 

void loop()
{ 
pinread == analogRead(potpin1);  
  if (pinread < 482)
  {
    value1 = map(pinread, 0, 481, 255, 0);
    analogWrite (PWMPin1, value1);
  }
  else if (pinread > 554)
  {
    value2 = map(pinread, 555, 1023, 0, 255);
    analogWrite (PWMPin2, value2);
  }
  else
  {
    analogWrite (PWMPin1, 0);
    analogWrite (PWMPin2, 0);
  }
}

Thanks Groove, fixed it.

You're mapping a pin number.

Like this?

pinread [glow]==[/glow] analogRead(potpin1);

No, not quite. Close, though.

I have tested the sketch and it works fine. I had to use outputs 2 and 3, 1 is not a PWM output.

When I try to open 2 transistors using one output it doesn't work???

When I use one transistor I can it works and I have checked direction using leds.

Could there be a timing problem? I checked the transistors and they work fine until I make the base common and it will not work anymore. Even when it's working with one transistor and I only connect the base of the second transistor (so not using collector and emiter) it doesn't work anymore.
EDIT: base only works but collector of second transistor connected makes it go bad.

I knew there was magic smoke involved in electronics but apperently there is even more magic???

When I try to open 2 transistors using one output it doesn't work???

What does that mean?

Ok, sorry.

I have two npn transistors and with opening I mean make them conduct by putting the signal on the base.

I guess I have to get a couple of PNP transistors also.