PID speed controller

Hi guys !

I'm working on a project an I'm totally stuck. I must programm a PID speed controller with specific material.
I have at my disposal :

I made the following connexions : (see attached image)

And I wrote this code :

#include <PID_v1.h>  // For the PID
#include <TimerOne.h> //For the timer


#define ENCODEURA 2
#define ENCODEURB 4
#define A 5       // Speed control for the motor


double Setpoint, Input, Output;


double Kp=0.8, Ki=10, Kd=0.1;


PID PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);


volatile int count =0;  // encoder's ticks counting
volatile double speed =0; // motor's speed 

volatile unsigned long time0=0;  // get a time value at a given time
volatile unsigned long timer=0; // a value which will contain the last time gotten via millis
volatile byte laststate =0;  // last encoder's state

 
volatile int commande=200; // order for the motor (between 0 and 255)

int valeur;  //order's reading via the monitor


void setup()
{
  
  //monitor's initialization
  Serial.begin(9600); // here for feedback

// Inputs and Outputs initialization
 pinMode(ENCODEURA, INPUT_PULLUP);
 pinMode(ENCODEURB, INPUT_PULLUP);
 
pinMode(A, OUTPUT);
 
  

  // Encoder quadrature counter
  attachInterrupt(0,counter, CHANGE);
  Timer1.initialize(100000); // set a timer of length 100000 microseconds
                      //(or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( timerIsr );

  // PID 
  Input = 0;
  Setpoint = 0;
  //activate the PID
  PID.SetMode(AUTOMATIC);
  PID.SetOutputLimits(-255, 255);
}

void loop(){
   
if(Serial.available()){
  valeur=Serial.parseInt();
 if(valeur!=0){
 commande = valeur;
}
}

    Setpoint=commande;
    Input=speed;
    PID.Compute();
    int output=(int)Output;

   
    Serial.println("commande");
    Serial.println(commande);               
    Serial.print("'''''''''''''''''");     
    
    Serial.println("speed");
    Serial.println(speed);              
    Serial.print("'''''''''''''''''");      

    Serial.println("output");
    Serial.println(output);               
    Serial.print("'''''''''''''''''");     //only here for feedback


//using the PID Output in order to control the motor
if(output>=0){
  advance(255-output);
}


  timer = millis();
  if( (timer-time0)>10000)
  {
    commande= 0;
    time0=timer;
    //Timeout ! 
    
  }
}


void advance(int a) // Let's go !
{
analogWrite (A,a); // PWM speed control
}



// Encoder counter 0

void counter()
{
  byte state=PIND;
  
  state|=B11101011;  // mask to look at the changes only on PIN 2 and 4 
  if( state!=laststate)
  {
    (((state&(1<<ENCODEURA))>>ENCODEURA)^((state&(1<<ENCODEURB))>>ENCODEURB))?count--:count++;
    laststate=state;
  }
}


// Timer in order to calculate the speed with the encoders

void timerIsr()
{
    speed=count;
    count=0;
}

The problem is : nothing works ! I don't know why but the motor doesn't turn.
I tried many different things and read many tutorials but I can't find a way to make this work.

Could a good Samaritan help a man in need ?

Thanks !

Start small and get each piece of the project running before putting everything together.

Obviously, the motor comes first. A simple program to exercise the motor should take just a few lines of code.

If you have difficulty with that part of the project, post a hand drawn wiring diagram of the motor connections (not a photo or Fritzing diagram) and the code.

jeanpaulo:
The problem is : nothing works ! I don't know why but the motor doesn't turn.
I tried many different things and read many tutorials but I can't find a way to make this work.

What you probably need to do is to start from the beginning.

Rather than PID, just try to write basic code to get some activity happening at the PWM pin of the arduino. Then you can use that PWM waveform to drive the motor-driver, and then see (using your multimeter voltage measurements) whether you get expected DC voltage levels at the output of your motor driver. This is all basic open-loop mode to start with.

Once you're satisfied that your motor driver output voltage (ie. average output voltage) is able to change with a few different settings of PWM level, you can then connect up your motor. And then you can start tinkering with feedback operation.