My problem is, the Output (not out) will stay at 0. Or once it went to 30 and then it stayed there.
I have made Out the analogwrite, since my DC motor is only supplied with 5V, and it needs 12V, so I need to up the PWM.
I am using this libray
#include <PID_v1.h>
#include <Encoder.h>
//motor pins
double ccwRot1 = 4;
const double cwRot1 = 5;
const double cwRot2 = 6;
const double ccwRot2 = 7;
//Encoder pins
const int encoderPinA1 = 2;
const int encoderPinB1 = 3;
Encoder encoder1(encoderPinA1, encoderPinB1);
//Revolutions to Degrees
double angle1 = 360; // Angle you wish
int rotPerCycle = 2500; //Number of pulses per 360 degrees
double targetRot1 = angle1 * rotPerCycle / 360;
volatile int lastCase1 = 0;
//standalone variables
int i = 0;
//Variables for the PID control loop
double Setpoint, Input, Output;
//To loops, hvis den går over angle1, så aktiveres det andet loop, og det første lukkes, omvendt, hvis det er under angle1 - stadig WIP
PID motPoscw1(&Input, &Output, &Setpoint, 5, 1, 3, DIRECT);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
motPoscw1.SetMode(AUTOMATIC);
motPoscw1.SetSampleTime(1);
} void loop() {
long revs1 = encoder1.read();
double Input = revs1;
double Setpoint = targetRot1;
motPoscw1.Compute();
analogWrite(cwRot1, 120);
Serial.println(Output);
if (revs1 > 1) {
motPoscw1.Compute();
double out = Output + 40;
analogWrite(cwRot1, out);
}
}
blh64:
You are declaring local variables inside loop() which have nothing to do with the global variables of the same name (Input & Setpoint)
Also, why are your motor pins declared as 'double'? They are not floating point numbers.
When I declared them as int, the PID function said something along the line of "No function for PID(const int*, const long*, const int*). And the library says the output,input and setpoints have to be doubles.
is what I was referring to and has nothing to do with PID.
Yes, they're the output for the PID loop. It didn't accept any variables other than doubles for some reason. Once again I will reffer you to the library, which states the different variables must be in doubles Arduino Playground - PIDLibraryConstructor.
gigawhat:
Yes, they're the output for the PID loop. It didn't accept any variables other than doubles for some reason. Once again I will reffer you to the library, which states the different variables must be in doubles Arduino Playground - PIDLibraryConstructor.
They most certainly are not tied to PID, given the code you posted. If you look where 'cwRot1' is used in your posted code, it is only used as the pin# for a analogWrite
blh64:
They most certainly are not tied to PID, given the code you posted. If you look where 'cwRot1' is used in your posted code, it is only used as the pin# for a analogWrite
analogWrite(cwRot1, 120);
Oh yeah you're right, oops.
I was thinking of when I got the error message, but I changed it after that.