Hi
I have problems (note the plural) getting my sketch to compile. I am using an L298N driver to drive a motor, a speed encoder to calculate the rotations per second and a potentiometer to change the speed. This was all fine but then I changed the code a lot to try to add PID code. Could someone help out with adapting the code at http://playground.arduino.cc/Code/PIDLibaryBasicExample please? (broken code below)
The log says:
L298N_apr08b:14: error: ‘Setpoint’ was not declared in this scope
PID myPID(&Setpoint, &Input, &Output,2,5,1, DIRECT); // DO I LEAVE IT LIKE THIS?
^
L298N_apr08b:14: error: ‘Input’ was not declared in this scope
PID myPID(&Setpoint, &Input, &Output,2,5,1, DIRECT); // DO I LEAVE IT LIKE THIS?
^
L298N_apr08b:14: error: ‘Output’ was not declared in this scope
PID myPID(&Setpoint, &Input, &Output,2,5,1, DIRECT); // DO I LEAVE IT LIKE THIS?
^
L298N_apr08b.ino: In function ‘void setup()’:
L298N_apr08b:64: error: ‘analogPinToInterrupt’ was not declared in this scope
attachInterrupt(analogPinToInterrupt(2), potval, CHANGE); // adjust speed when pot pin changes
^
L298N_apr08b:66: error: ‘Input’ was not declared in this scope
Input = rotation;
^
L298N_apr08b:67: error: ‘Setpoint’ was not declared in this scope
Setpoint = desired;
^
L298N_apr08b.ino: In function ‘void loop()’:
L298N_apr08b:73: error: ‘Input’ was not declared in this scope
Input = analogRead(2);
^
exit status 1
‘Setpoint’ was not declared in this scope
#include <PID_v1.h>
#include <TimerOne.h>
float maxrpsCW = 3.60; // the desired max revs per second for the CW motion as measured under current load
float maxrpsACW = 3.80; // the desired max desired revs per second for the ACW motion as measured under current load
unsigned int counter = 0; // counts the encoder holes I think (to calculate rotation)
int c = 0; // counter that delays Serial display of desired rotation (variable: desired)
//Define Variables we'll be connecting to //float Setpoint, Input, Output;
float desired, rotation, motorspeed;
//Specify the links and initial tuning parameters
PID myPID(&Setpoint, &Input, &Output,2,5,1, DIRECT); // DO I LEAVE IT LIKE THIS?
void docount() { // counts from the speed sensor
counter++; // increase +1 the counter value
}
void timerIsr() {
Timer1.detachInterrupt(); //stop the timer
Serial.print("Motor Speed: ");
float timeadjust = 6.00; // I had to adjust the rotation to make it approximately correct (not sure why it is inaccurate)
float rotation = counter / 20.00 / timeadjust; // divide by number of holes in Disc and again by timeadjust
Serial.print(rotation); Serial.println(" rps");
counter = 0; // reset counter to zero
Timer1.attachInterrupt( timerIsr ); //enable the timer
}
void potval() {
int potvalue = analogRead(2); // Potentiometer connected to Pin A2
Serial.print("potvalue: ");
Serial.println(potvalue);
if (potvalue <= 685/2-30) { // ACW rotation
digitalWrite(8, LOW); digitalWrite(7, HIGH); digitalWrite(5, LOW); digitalWrite(4, HIGH);
float desired = map(potvalue, 0.00, 685.00/2.00-30.00, maxrpsACW, 0.00);
}
if (potvalue >= 685/2+30) { // CW rotation
//do later
}
if (potvalue > 685/2-30 && potvalue < 685/2+30) { // no rotation
//do later
c = c + 1;
if (c > 5000) {
Serial.print("Desired rps: ");
Serial.print(desired); Serial.println(" rps");
c = 0;
}
}
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); //motor 1
pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); //motor 2
Timer1.initialize(1000000); // set timer for 1sec
attachInterrupt(digitalPinToInterrupt(0), docount, RISING); // increase counter when speed sensor pin goes High
Timer1.attachInterrupt( timerIsr ); // enable the timer
attachInterrupt(analogPinToInterrupt(2), potval, CHANGE); // adjust speed when pot pin changes
//initialize the variables we're linked to
Input = rotation;
Setpoint = desired;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop() {
Input = analogRead(2);
myPID.Compute();
if (potvalue <= 685/2-30) { // ACW rotation
//float desired = map(potvalue, 0.00, 685.00/2.00-30.00, maxrpsACW, 0.00);
float motorspeed = map(desired, 0.00, maxrpsACW, 240, 0);
}
if (potvalue <= 685/2+30) { // CW rotation
//do later
}
if (potvalue > 685/2-30 && potvalue < 685/2+30) { // no rotation
//do later
}
analogWrite(6, motorspeed); // set speed of motor (0-255)
}