Hi,
Im tring to develop code that would allow a valve(servo motor) to operate on a scale from open to close depending on the light a sensor is recieving.
At the same time id like to switch this to a manual control where a potentiometer can be used to carry out the valve operation.
Currently it feels that the system is not reflecting any changes in light as expected.
Also my manual control has zero control.
// Setting up the Constraint Values for Pid Controller and Input Devices for Control Loop
#include <PID_v1.h>
const int photores = A0; // Photo Resistor Input
const int pot = A1; // Potentiometer Input
double lightLevel; // Variable that stores the incoming light level
// Setting up the Constraint Variables for Servo Motor
#include <Servo.h>
Servo myServo; // Creation of a servo object
int val; // Variable to read value from the analog pin
int voltValuue = 0; // variable to store the value from potentiometer
// Declaring Switch pin constants
const int sw1pin = A2; // Input pin for manual switch
const int manLEDpin = 7; // Output pin for manual indicator lamp
// Declarinf LED variable
int sw1_state = 0; // Manual switch state (HIGh/LOW)
// Tuning Parameters
float Kp=10; // Initial Proportional Gain
float Ki=10; // Initial Integral Gain
float Kd=10; // Initial Differential Gain
//Variable Creation for Storing Values
double Setpoint, Setpoint1, Input, Output;
// Setting up the PID loop
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT); // Input is the PV
// Output is the Output
// Setpoint is the SP
const int sampleRate = 1; // Variable that determines the speed of the PID Loop
// Communication setup
const long serialPing = 500; // This determins how often the loop is pinged
// Serial pingback interval in milliseconds
unsigned long now = 0; // Variable acting to keep track of time
// Placeholder for current timestamp
unsigned long lastMessage = 0; // This keeps track of when the loop last communicated to Serial
void setup() {
lightLevel = analogRead(photores); // Rean in the light level
Input = map(lightLevel, 0, 1024, 0, 1000); // Change the read scale to anolog out scale
Setpoint = map(analogRead(photores), 0, 1024, 0, 1000); //Get setpoint from Phot Resistor
Setpoint1 = map(analogRead(pot), 0, 1024, 0, 1000); //get our setpoint from our potentiometer
Serial.begin(9600); // Start a serial session
myPID.SetMode(AUTOMATIC); // Turns on the PID Loop
myPID.SetSampleTime(sampleRate); // Sets the sample rate
Serial.print("System EN2107 Starting"); // System stating message
lastMessage = millis(); // timestamp
myServo.attach(9); // Attaches the Servo connected to pin 9 to the servo object
// Initialize the I/O pins
pinMode( sw1pin, INPUT );
pinMode( manLEDpin, OUTPUT );
}
void loop() {
sw1_state = digitalRead( sw1pin ); // check manual switch state
if (sw1_state == HIGH) { // Switch activated for manual mode
Serial.println("System in Manual");
digitalWrite( manLEDpin, HIGH); // Activate manual mode
Setpoint1 = map(analogRead(pot), 0, 1024, 0, 1000); //Read our setpoint
Input = map( pot, 0, 1024, 0, 1000);
// Manual operation looks at the value from the potentiometer
analogWrite(9, Output);
Serial.print("Setpoint = ");
Serial.print(Setpoint);
Serial.print(" Input = ");
Serial.print(Input);
Serial.print(" Output = ");
Serial.print(Output);
Serial.print("\n");
}
else {
digitalWrite( manLEDpin, LOW); // Manual mode off
Serial.println("System in Automatic");
Setpoint = map(analogRead(photores), 0, 1024, 0, 1000); // Read the setpoint value
lightLevel = analogRead(photores); // Get the light level
Input = map(lightLevel, 0, 900, 0, 1000); // Map it to the right scale
myPID.Compute(); // Run the PID loop
analogWrite(9, Output); // Write out the output from the PID loop to Servo Motor pin
now = millis(); // Keep track of time
{ // If its been long enough give us some info on serial
// send a message to serial monitor
Serial.print("Setpoint = ");
Serial.print(Setpoint);
Serial.print(" Input = ");
Serial.print(Input);
Serial.print(" Output = ");
Serial.print(Output);
Serial.print("\n");
if (Serial.available() > 0) { // If we sent the program a command deal with it
for (int x = 0; x < 4; x++) {
switch (x) {
case 0:
Kp = Serial.parseFloat();
break;
case 1:
Ki = Serial.parseFloat();
break;
case 2:
Kd = Serial.parseFloat();
break;
case 3:
for (int y = Serial.available(); y == 0; y--) {
Serial.read(); // Clear out any residual useless information
}
break;
}
}
Serial.print(" Kp,Ki,Kd = ");
Serial.print(Kp);
Serial.print(",");
Serial.print(Ki);
Serial.print(",");
Serial.println(Kd); // Display the value received
myPID.SetTunings(Kp, Ki, Kd); // Set the PID gain constants and start running
}
lastMessage = now;
// update the time stamp.
}
// Servo Motor control
val = &Output; // read the value of the output
// print out the value to the Serial Monitor
Serial.print("val: ");
Serial.print(val);
// wait for the servo to get there
delay(500);
}
}