TRYING to UNDERSTAND and (later) ADAPT A PID program

Hello everyone.

I bought a Micro DC Motor with Encoder-SJ01 ant there are two programs to use it.
First works and the second as well but it doesn’t control my motor.
I turn my self the motor and pulse is well detected and the output value to control the motor change too.

I don’t really understand which variable to declare to control the motor I think is E_left because it is the output 5 which is analog output on Arduino uno.
But how manage the direction of the motor with only one PIN? (to control the bridge of the L298N)
How can I print the value LOW or HIGH of a digital pin?

Here the code

//The sample code for driving one way motor encoder
#include <PID_v1.h>
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 3;//B pin -> the digital pin 3
int E_left =13; //The enabling of L298PDC motor driver board connection to the digital interface port // to me 5 ou 11 ???
int M_left =11;; //The enabling of L298PDC motor driver board connection to the digital interface port // to me  4 ; 7 ou 13 ;12 ???
byte encoder0PinALast;
double duration,abs_duration;//the number of the pulses
boolean Direction;//the rotation direction 
boolean result;

double val_output;//Power supplied to the motor PWM value.
double Setpoint;
double Kp=0.6, Ki=5, Kd=0;  
PID myPID(&abs_duration, &val_output, &Setpoint, Kp, Ki, Kd, DIRECT); 
 
void setup()
{  
   Serial.begin(9600);//Initialize the serial port
   pinMode(M_left, OUTPUT);   //L298P Control port settings DC motor driver board for the output mode
   pinMode(E_left, OUTPUT); 
   Setpoint =80;  //Set the output value of the PID
   myPID.SetMode(AUTOMATIC);//PID is set to automatic mode
   myPID.SetSampleTime(100);//Set PID sampling frequency is 100ms
  EncoderInit();//Initialize the module
}
 
void loop()
{   
      advance();//Motor Forward
      abs_duration=abs(duration);
      result=myPID.Compute();//PID conversion is complete and returns 1
      if(result)
      {
        Serial.print("Pulse: ");
        Serial.println(duration); 
        duration = 0; //Count clear, wait for the next count
        Serial.print("val_output"); Serial.println(val_output);
        Serial.print("M_left"); Serial.println(M_left);
        Serial.print("E_left"); Serial.println(E_left);
      }
        

}
 
void EncoderInit()
{
  Direction = true;//default -> Forward  
  pinMode(encoder0pinB,INPUT);  
  attachInterrupt(0, wheelSpeed, CHANGE);
}
 
void wheelSpeed()
{
  int Lstate = digitalRead(encoder0pinA);
  if((encoder0PinALast == LOW) && Lstate==HIGH)
  {
    int val = digitalRead(encoder0pinB);
    if(val == LOW && Direction)
    {
      Direction = false; //Reverse
    }
    else if(val == HIGH && !Direction)
    {
      Direction = true;  //Forward
    }
  }
  encoder0PinALast = Lstate;
 
  if(!Direction)  duration++;
  else  duration--;

}
void advance()//Motor Forward
{
     digitalWrite(M_left,LOW);
     analogWrite(E_left,val_output);
}
void back()//Motor reverse
{
     digitalWrite(M_left,HIGH);
     analogWrite(E_left,val_output);
}

void Stop()//Motor stops
{
     digitalWrite(E_left, LOW); 
}

and some results

Pulse: 61.00
val_output255.00
M_left11
E_left13
Pulse: 74.00
val_output255.00
M_left11
E_left13
Pulse: 86.00
val_output248.40
M_left11
E_left13
Pulse: 91.00
val_output239.90
M_left11
E_left13
Pulse: 91.00
val_output234.40
M_left11
E_left13
Pulse: 81.00
val_output239.90
M_left11
E_left13
Pulse: 67.00
val_output254.80
M_left11
E_left13
Pulse: 38.00
val_output255.00
[code]

Thanks for your attention

Be careful, originally the program is:

int E_left =5; //The enabling of L298PDC motor driver board connection to the digital interface port 5
int M_left =4; //The enabling of L298PDC motor driver board connection to the digital interface port 4

you can find it, it is the code 2

https://www.dfrobot.com/wiki/index.php/Micro_DC_Motor_with_Encoder-SJ01_SKU:_FIT0450#Encoder_Sample_Code_1

I am confused between your Title and your text. There seems to be nothing in your text about PID.

I am also confused because you say "First works and the second as well but it doesn't control my motor." If they don't control your motor then how can you say they work?

Then your questions seem to be about how to connect an L298 to your Arduino.

If you have a program that works - meaning that it controls the motor as it is intended to do, then post that program and also a diagram showing how the Arduino is connected to everything. (See this Simple Image Guide).

If you also have a program that does not work then look carefully to see how it differs from the program that does work.

With L298 motor drivers it is common to feed the PWM signal to the Enable pin and use the IN1 and IN2 pins for direction control. That way a motor can be controlled with a single PWM pin.

...R

Hi Robin,
Problem is solved I manage to control at a good speed my motor but it oscillate a bit.
I'm gonna post a new topic.