Hello
Would like to ask experts if they know how can i plot real time graph of the PID (Speed Versus Time) of the DC Motor from Serial Monitor of the Arduino to the MATLAB ?
This is my arduino code:
/********************************************************
- PID Basic Example
- Reading analog input 0 to control analog PWM output 3
********************************************************/
#include <PID_v1.h>
#define PIN_OUTPUT 11
double SPEED();
//Define Variables we’ll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=1, Ki=2, Kd=0.00001;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, REVERSE);
void setup()
{
//initialize the variables we’re linked to
Serial.begin(9600);
Input = 0;
Setpoint = 120;
pinMode(PIN_OUTPUT, OUTPUT); //PWM PIN 10 with white line
pinMode(9, OUTPUT);//direction control PIN 11 with blue line
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = SPEED();
myPID.Compute();
analogWrite(PIN_OUTPUT, Output);
Serial.println("");
Serial.print(“Speed:”);
Serial.println(Input);
Serial.print(“PWM:”);
Serial.println(Output);
Serial.println("");
//delay(500);
}
double SPEED()
{
int i = 0;
for(int j = 0;j<8;j++)
{
i += pulseIn(10, HIGH, 500000); //SIGNAL OUTPUT PIN 9 with white line,cycle = 2i,1s = 1000000us,Signal cycle pulse number:272
}
i = i >> 3;
double x = 111111 / i;
if (x>160.0)
{x = 0;}
else
;
return x;
}
Thanks