ARDUINO DUE R3 AND DFROBOT TB6612 DC MOTOR DRIVER UNSTABLE PWM

Hello I used Arduino Due and Dfrobot TB6612 DC motor driver for controlling my 6V DC motor. But i realized that, every start of my code, output pwm signal is different. My code is here:

int DIR1 = 4;
float y;
int yi;
                           
void setup() 
{ 
  pinMode(DIR1, OUTPUT);
  analogWriteResolution(12);
  
} 

void loop() 
{ 
    int x=analogRead(A0);
      
    if (x<31)
    {y=0.0;}
    
    else if ((31<=x)&&(x<590))
    {y=(4096.0/559.0)*x-227;}
        
    else if (x>590)
    {y=4095.0;}
      
    digitalWrite(DIR1,LOW);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               


    yi=(int)y;
    analogWrite(5,yi);  
  
}

As you can see, I have a input signal and its a constant data that recorded by a potantiometer before. This data is produced by a data acquisition card every start my code. So I hope my PWM signal would be same everytime. But the output is different every I start my code. When I used Arduino Uno, everytghing is fine. But when I used Due, the output is unstable. Do you have any idea about that? Thanks in advance!

What is the voltage applied to A0 ?

I do suspect the whole "data acquisition card" part. What happens when you connect a normal pot? And show and tell use more about that "data acquisition card".

Also, I did have to rearrange the code to a normal matter to read it:

int DIR1 = 4;
float y;
int yi;
                           
void setup(){
  pinMode(DIR1, OUTPUT);
  analogWriteResolution(12);
}

void loop(){
  int x = analogRead(A0);
   
  if (x < 31){
    y = 0.0;
  }
  else if ((31 <= x) && ( x < 590)){
    y = (4096.0 / 559.0) * x - 227;
  }
  else if (x > 590){
    y = 4095.0;
  }
   
  digitalWrite(DIR1, LOW);

  yi=(int)y;
  analogWrite(5, yi); 
}

And besides the weird formatting a couple of remarks:

  • The compiler does not care about variable names, they are an aid for you. Using stuff like x, y en yi isn't that useful though.

  • You are missing a case for x == 590

  • You don't need all the float nonsense.

ard_newbie:
What is the voltage applied to A0 ?

Its range is 0-2 V.

septillion:
I do suspect the whole "data acquisition card" part. What happens when you connect a normal pot? And show and tell use more about that "data acquisition card".

It generates PWM, it's not problem. The problem is that PWM signal that is generated from "same analog inputdata " is not same. If I use same analog input data, I would receive sama PWM i think. Wouldn't I?

Do you have an RC filter on A0 to reduce input noise ? How do you power your board ?

msc:
It generates PWM, it's not problem.

PWM on an analog pin IS a problem. Why are people always so stubborn that the problem can't be in a certain part of a system without doing deduction...

Like I said, change to a normal pot or fixed voltage divider (which you can measure) and try again.