L298n Hbridge + Potentiometer + incremental encoder

i understand what u mean nw.. it makes sense..

however i am a newbie and am unsure what parts of the code that need altering...

please help..

I am presently starting code to control this IC. I haven’t thought about it, but I might think about pulsing the ENABLE pin rather than a direction pin. I have not tried it yet, but you might want to put the PWM on the ENABLE pin, and set the direction digitally. That way the 298 will be in “freewheel” mode rather than “brake” mode.

EDIT: Here is a start using PWM on the enablePin:

const int motor1Pin = 9;    // H-bridge leg 1 (pin 9, 1A)
const int motor2Pin = 10;    // H-bridge leg 2 (pin 10, 2A)
const int enablePin =11;    // H-bridge enable pin 11
const int PotPin = 1;    // select the input pin for the potentiometer
 
int Speed;
int sensorValue = 0;  // variable to store the value coming from the sensor
byte brakePressure = 0;
        
void setup()
{
  pinMode(motor1Pin,OUTPUT);
  pinMode(motor2Pin,OUTPUT);
  pinMode(enablePin,OUTPUT);
  digitalWrite (enablePin, LOW);
  digitalWrite(motor1Pin,LOW);
  digitalWrite(motor2Pin,LOW);
}

void loop()
{
  sensorValue = analogRead(PotPin);

  if (sensorValue < 510)
  {
    Speed = (512 - sensorValue) /2;
    digitalWrite(motor2Pin,LOW);
    digitalWrite(motor1Pin,HIGH);
    analogWrite(enablePin,Speed);
  }
  else if(sensorValue > 518)
  {
    Speed = (sensorValue - 512) /2;
    digitalWrite(motor1Pin,LOW);
    digitalWrite(motor2Pin, HIGH);
    analogWrite(enablePin,Speed);
  }
  else
  {
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);

    // This stops according to the value set above in brakePressure
    // brakePressure = 0 (no brake)
    // brakePressure = 127 (half brake)
    // brakePressure = 255 (full brake)
    analogWrite(enablePin,brakePressure);

  }

  delay(30);
}

Please excuse all the edits. I change it as I write this for me. :slight_smile:

You need to take some initiative. Add some Serial.print() statements to the code, so you can see what is going on.

You could have the potentiometer connected wrong, so you are not properly reading it. You could have the code wrong, so that the potentiometer value is not used correctly. You could have the HBridge connected incorrectly, so that the motor does not function in both directions.

You need to actively participate in defining and debugging the code, to see where the problem is.

the code is running correctly however am not sure if the motor is running at full speed at the max and min pot values.. and if there maybe restricting this..

he code is running correctly however am not sure

So, do as PaulS suggested, and add some prints.

I second that. Thanks PaulS.

thanks for the information and i have got the POT to work with the Motor as required.

i have an incremental encoder attached to the motors shaft that delivers 500 pulses for every 360 degrees the motor turns.

i need to create code that counts the pulses from the encoder.

i cant find a count function in the libary but have come across the pulse function, can this be used in order to do this?

i would like it to +1 for every step forward and -1 for every negative step.

i need to create code that counts the pulses from the encoder.

i cant find a count function in the libary

What? i = i + 1; doesn’t work? It does for me, as does i++;

Here's a good thread that should get you moving on writing code for a rotary encoder.

http://arduino.cc/forum/index.php/topic,62026.msg450013.html#msg450013

As a bit of advice on the side, just about every basic and a good many not so basic questions have been asked and answered multiple times on this forum. A few minutes searching is typically all that's needed to find answers to your questions, or at least get you headed in the right direction.

i have managed to gather and store the required variables such as the…

“int count” variables
“int time” variables

for the four individual positions required… however now the issue is using this to create a playback subroutine. what it needs to do is to see what switch is pressed connected to analog pins 16 - 19 now being used as digital pins with individual push switches and to rotate the motor depending on the count variable determined by the incremental encoder. to make things trickier the adjustment must be made in regards to the time variable stored.

for example… position 2 will have its own count value “count2” and time value “time1” prestored. this is where i am facing difficulties as i am not sure how i can do this :~

here is my code…

[code]/*

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 13
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * Pot to Analog 0

 */

#include <LiquidCrystal.h>                          // include the library code:

LiquidCrystal lcd(12, 11, 5, 13, 3, 2);             // initialize the library with the numbers of the interface pins
  
        int sensorPin = A1;                         // select the input pin for the potentiometer for time
        int sensorValue = 0;                        // variable to store the value coming from the sensor

  const int motor1Pin = 9;                          // H-bridge leg 1 (pin 9, 1A)
  const int motor2Pin = 10;                         // H-bridge leg 2 (pin 10, 2A)
  const int enablePin =8;                           // H-bridge enable pin 8
  const int PotPin = A0;                            // input pin for the potentiometer for speed/direction
  
        int x = 10;
        int zero_speed;
        int Reverse;
        int Forward;
        int sensorValue2 = 1;                       // variable to store the value coming from the sensor
  
        int value = analogRead(PotPin);
        int speed = abs(value - 512)/2 ;            // 0..1023 -> -512.0..+512 -> 512..0..512 -> 256..0..256
        int direction = value < 512;                // or "value > 512" to reverse the action of the pot
 
        char flag;
        
        int count1 = 0;
        int time_1;
        
        int count2;
        int time_2;
        
        int count3;
        int time_3;
        
        int count4;
        int time_4;


 
 void enter_point_a()                               // Subroutime "enter_point_a"
{
  lcd.clear();                                      // Clear Screen 
  lcd.setCursor(1, 0);                              // set the cursor to column 0, line 0
  lcd.print("Enter Data for");                      // Print "Enter Data for"
  lcd.setCursor(4, 1);                              // set the cursor to column 4, line 1
  lcd.print("Point A");                             // Print "Point A"
  delay(3000);                                      // Delay
  lcd.clear();                                      // Clear Screen
  
  zero_motor();
}

 
 
 void enter_point_b()                               // Subroutime "enter_point_b_data"
{
  
  lcd.setCursor(1, 0);                              // set the cursor to column 0, line 0
  lcd.print("Enter Data for");                      // Print "Enter Data for"
  lcd.setCursor(4, 1);                              // set the cursor to column 4, line 1
  lcd.print("Point B");                             // Print "Point B"
  delay(3000);                                      // Delay
  lcd.clear();                                      // Clear Screen

  enter_data_2();
}

 
 void enter_point_c()                               // Subroutime "enter_point_c_data"
{
  
  lcd.setCursor(1, 0);                              // set the cursor to column 0, line 0
  lcd.print("Enter Data for");                      // Print "Enter Data for"
  lcd.setCursor(4, 1);                              // set the cursor to column 4, line 1
  lcd.print("Point C");                             // Print "Point B"
  delay(3000);                                      // Delay
  lcd.clear();                                      // Clear Screen

  enter_data_3();
}

 
  
 void enter_point_d()                               // Subroutime "enter_point_d_data"
{
  
  lcd.setCursor(1, 0);                              // set the cursor to column 0, line 0
  lcd.print("Enter Data for");                      // Print "Enter Data for"
  lcd.setCursor(4, 1);                              // set the cursor to column 4, line 1
  lcd.print("Point D");                             // Print "Point B"
  delay(3000);                                      // Delay
  lcd.clear();                                      // Clear Screen

  enter_data_4();
}


void enter_data_1()                                   // Subroutime "enter_data_1"                 
{  
  digitalWrite(7, HIGH);                            // set the LED on
  
  lcd.setCursor(0, 0);                              // set the cursor to column 0, line 0 
  lcd.print("Time");                                // Print "Time"
  lcd.setCursor(4, 0);                              // set the cursor to column 4, line 0
  lcd.print(":");                                   // Print ":"
  lcd.setCursor(8, 0);                              // set the cursor to column 8, line 0
  lcd.print(".");                                   // Print ":"
  lcd.setCursor(12, 0);                             // set the cursor to column 12, line 0
  lcd.print("Min");                                 // Print "Min"
  lcd.setCursor(0, 1);                              // set the cursor to column 0, line 1
  lcd.print("Rotation");                            // Print "Rotation"
  lcd.setCursor(8,1);                               // set the cursor to column 8, line 0
  lcd.print(":");                                   //Print ":"
  

  Pot_Speed_Time_1();                                 // go to sub routine "Pot_Speed_Time"

  digitalWrite(7, LOW);                             // set the LED off
}

 
   
  void Pot_Speed_Time_1()
  {
  
    while(1)
    {
    digitalWrite (enablePin, HIGH);
    int retval = analogRead(value);
      
      if (retval < 510)
        {
        Reverse = map(retval, 0, 510, 255, 0);
        analogWrite (motor1Pin, Reverse);
        count_pulse_neg_1();
        }
        else if(retval > 518)
          {
          Forward = map(retval, 518, 1023, 0, 255);
          analogWrite (motor2Pin, Forward);
          count_pulse_pos_1();
          }
        else
          {
          analogWrite (motor1Pin, 0);
          analogWrite (motor2Pin, 0);
          }

  int val = analogRead(sensorPin);                    // read value from pot at sensorPin
      time_1 = map(val, 0, 1023, 0, 600);                // map function

    lcd.setCursor(6,0);                               // set the cursor to column 0, line 1
    lcd.print(time_1/60);                                // Print "val/100"  

    lcd.setCursor(8, 0);                              // set the cursor to column 2, line 1
    lcd.print(":");                                   // Print ":"
  
    lcd.setCursor(9,0);                               // set the cursor to column 3, line 1
    lcd.print(time_1/10);                                // Print "val/10"
       
  if (digitalRead(1) == HIGH)                        // if button is pressed 
    {                                              // then go to sub routine
    lcd.clear();                                   // Clear Screen
    enter_point_b();                               // go to sub routine "enter_point_b"
    }
   }
 }



void count_pulse_pos_1()
{
  if((digitalRead(4) == HIGH) && (flag == 0))
    {
    count1 ++ ;
    flag = 1;
    }
   if(digitalRead(4) == LOW)
      {
      flag = 0;
      }  
      lcd.setCursor(10,1);    
      lcd.print((count1/1.4));

}

 

void count_pulse_neg_1()

{
  if((digitalRead(4) == HIGH) && (flag == 0))
    {
    count1 --;
    flag = 1;
    }
   if(digitalRead(4) == LOW)
      {
      flag = 0;
      }  
      lcd.setCursor(10,1);    
      lcd.print((count1/1.4));
}



void zero_motor()
{
 zero_speed= map(x, 0,50,0, 255);
  
  while(1)
 {

   
     digitalWrite(enablePin, HIGH);
   
     analogWrite (motor1Pin, HIGH);  
     digitalWrite(motor2Pin, LOW); 


  if(digitalRead(6) == HIGH)
    {
     digitalWrite(motor1Pin, LOW);  
     digitalWrite(motor2Pin, LOW); 
     lcd.clear();
     return; 
    }  
  } 
}

[/code]