Hi everyone !
I am trying to control a DC motor with an Arduino nano.
The PWM command comes from labview, is transferred by bluetooth to the Arduino nano and sent to the motor thanks to analogWrite.
I need to get datas from a sensor to use a PID controller. The data from the sensor are collected thanks to an interruption (a simple counter), processed by the arduino and sent to Labview by bluetooth. It's also easy to do and it is working.
It looks easy to do and everything is ALMOST working BUT
when I try to control the motor, the PWM output from Arduino is jerky. I can't find the source of this problem.
Here are two little videos of the problem, the arduino code and I join the VI.
1 : View of the motors that are not working properly. You can hear the sound. like bipbipbipbip [u]PWM issue - YouTube
2 : View of the oscilloscope. It the PWM output from pin 6 of arduino nano
[u]Pwm issue 2 - YouTube
volatile byte counter;
int rpm= 0;
int defaultVal = -1; //will be used when labview is waiting for something and Arduino hasn't got anything to send
long timeold; //time value
int interruptPin = 0, pinPWM = 6;
String command;
void setup()
{
pinMode ( interruptPin, INPUT );
pinMode ( pinPWM, OUTPUT );
counter = 0;
Serial.setTimeout( 70 ); //to avoid waiting 1000ms between each change of the pwm value on labview
Serial.begin ( 9600 );
Serial.println ( "let's go" );
attachInterrupt ( interruptPin, interruptCounter, FALLING );
timeold = millis();
}
void loop()
{
if( counter >= 18 ){ //2spins because half of the reflective parts are hidden
detachInterrupt ( interruptPin );
//analogWrite ( pinPWM, command.toInt() );
/******************* TREATMENT OF THE ROTATION SPEED *******************/
//we know that the platform made 2 spins sins we registered the time. We mow have to check the time, sustract timeold with it and we kmow how much time it took. And convert rpmilliseconds to rpminutes
rpm = 30000 / ( millis() - timeold );
/******************* GETTING READY FOR ANOTHER ROUND ******************/
Serial.print( rpm );
counter = 0;
timeold = millis();
attachInterrupt ( interruptPin, interruptCounter, FALLING );
}else if (millis() - timeold >= 1001){
Serial.print( 0 );
}
else {
Serial.print( defaultVal );
} //Not important, just because labview is waiting to read something
/********************** ACTUALISING THE PWM **********************/
command = Serial.readString();
analogWrite ( pinPWM, command.toInt() );
}
void interruptCounter() { ++counter; }
Thanks for reading :smileyhappy:
If you have any idea, please tell me.
Quentin