I have an L298N board much like this one: http://www.ebay.co.uk/itm/Dual-H-Bridge-Motor-Driver-L298N-for-Arduino-PIC-etc-DC-Stepper-L298-Board-/160831035879?pt=UK_Computing_Other_Computing_Networking&hash=item257246d9e7
I'm just using one side to control a 12v DC motor. Currently sending it a direction and PWM using code base on one of the standard examples. The thing that is confusing me is that when the direction is '1' 255 is stopped and 0 is max speed on the PWM, when the direction is '0' these are reversed and 0 is stopped and 255 is max speed. Is this normal. It's making my code a little more tricky than I would like.
This is the code I have. There is a variable resistor that as you turn it the motor slows, stops, reverses and speeds up. It works fine. Just seems a little odd about the speed thing. If there is a better way to achieve this then I would be happy to hear it.
//Arduino PWM Speed Control:
const int E1 = 5;
const int M1 = 4;
const int E2 = 6;
const int M2 = 7;
const int RsupplyPIN = 14;
const int RvaluePIN = 15;
const int RgroundPIN = 16;
const int RvalueMAX = 825;
const int RvalueMIN = 775;
const int RvalueZERO = 800;
int Rvalue = 0;
int Direction = 0;
int PWMvalue =0 ;
void setup()
{
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(RsupplyPIN, OUTPUT);
pinMode(RvaluePIN, INPUT);
pinMode(RgroundPIN, OUTPUT);
digitalWrite(RgroundPIN, LOW);
digitalWrite(RsupplyPIN, HIGH);
Serial.begin(9600);
}
void loop()
{
int PWMvalue;
Rvalue = analogRead(RvaluePIN);
delay(30);
if (Rvalue<=RvalueMIN)Rvalue=RvalueMIN;
if (Rvalue>=RvalueMAX)Rvalue=RvalueMAX;
Rvalue-=RvalueZERO;
if (Rvalue>=0)
{Direction=1;
PWMvalue=255-(10*abs(Rvalue));}
else
{Direction=0;
PWMvalue=10*abs(Rvalue);}
digitalWrite(M2, Direction);
analogWrite(E2, PWMvalue); //PWM Speed Control
}