Thanks for the thoughts.
I have a number of limit switches here I could fit to detect when the closed position has been reached. e.g. Hall, optic and micro switches. Logically, the initial position will always be closed because a spring or weight-on-arm will close the valve the moment power is cut. The sensor switch could be considered an extra safety feature I suppose. Also an additional sensor that detects the full open position might be helpful.
The example "StepperTest.pde" is what I run a few days ago to test out the system. It worked fine. Sorry, I'm not understanding how I can specifically adapt it.
OK, so I put a few more hours in and I have come up with a modified sketch that has some promise. However, once uploaded the TC stopped working. The stepper was rotating but continuous in one direction. To double check I swapped back to the previous sketch and found the TC working. Again back to the new code and the TC wasn't working so it appears there's a error or conflict.
So, that's where I'm up to. Here's the updated code:
Any thoughts please?
#include <PID_v1.h>
#include "Adafruit_MAX31855.h"
#include <AFMotor.h>
int thermoDO = 3;
int thermoCS = 4;
int thermoCLK = 10;
Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
// Connect a stepper motor with 48 steps per revolution (7.5 degree)
// to motor port #2 (M3 and M4)
AF_Stepper motor(48, 2);
int pos;
int pre;
int num_step;
int direct;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
int inputPin=3, outputPin=9;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
unsigned long serialTime; //this will help us know when to talk with processing
void setup()
{
//initialize the serial link with processing
Serial.begin(9600);
Serial.println("MAX31855 test");
// wait for MAX chip to stabilize
delay(500);
Serial.println("Stepper test!");
motor.setSpeed(10); // 10 rpm
//initialize the variables we're linked to
Input = digitalRead(inputPin);
Setpoint = 50;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
//pid-related code
Input = thermocouple.readCelsius();
myPID.Compute();
analogWrite(outputPin,Output);
int val = analogRead(9); // get the sensor value
int pos = map(val, 0, 1023, 0, 12);
int num_step = pos - pre;
pre = pos;
// move a number of steps equal to the change in the
// sensor reading
Serial.println("Single coil steps");
Serial.print("Step Value: ");
Serial.println(num_step);
if(num_step > 0)
{
direct = FORWARD;
}
else
{
direct = BACKWARD;
}
Serial.print("Direction: ");
Serial.println(direct);
motor.step(num_step, direct, SINGLE);
delay(1000);
//send-receive with processing if it's time
if(millis()>serialTime)
{
SerialReceive();
SerialSend();
serialTime+=500;
}
}
/********************************************
* Serial Communication functions / helpers
********************************************/
union { // This Data structure lets
byte asBytes[24]; // us take the byte array
float asFloat[6]; // sent from processing and
} // easily convert it to a
foo; // float array
// getting float values from processing into the arduino
// was no small task. the way this program does it is
// as follows:
// * a float takes up 4 bytes. in processing, convert
// the array of floats we want to send, into an array
// of bytes.
// * send the bytes to the arduino
// * use a data structure known as a union to convert
// the array of bytes back into an array of floats
// the bytes coming from the arduino follow the following
// format:
// 0: 0=Manual, 1=Auto, else = ? error ?
// 1: 0=Direct, 1=Reverse, else = ? error ?
// 2-5: float setpoint
// 6-9: float input
// 10-13: float output
// 14-17: float P_Param
// 18-21: float I_Param
// 22-245: float D_Param
void SerialReceive()
{
// read the bytes sent from Processing
int index=0;
byte Auto_Man = -1;
byte Direct_Reverse = -1;
while(Serial.available()&&index<26)
{
if(index==0) Auto_Man = Serial.read();
else if(index==1) Direct_Reverse = Serial.read();
else foo.asBytes[index-2] = Serial.read();
index++;
}
// if the information we got was in the correct format,
// read it into the system
if(index==26 && (Auto_Man==0 || Auto_Man==1)&& (Direct_Reverse==0 || Direct_Reverse==1))
{
Setpoint=double(foo.asFloat[0]);
//Input=double(foo.asFloat[1]); // * the user has the ability to send the
// value of "Input" in most cases (as
// in this one) this is not needed.
if(Auto_Man==0) // * only change the output if we are in
{ // manual mode. otherwise we'll get an
Output=double(foo.asFloat[2]); // output blip, then the controller will
} // overwrite.
double p, i, d; // * read in and set the controller tunings
p = double(foo.asFloat[3]); //
i = double(foo.asFloat[4]); //
d = double(foo.asFloat[5]); //
myPID.SetTunings(p, i, d); //
if(Auto_Man==0) myPID.SetMode(MANUAL);// * set the controller mode
else myPID.SetMode(AUTOMATIC); //
if(Direct_Reverse==0) myPID.SetControllerDirection(DIRECT);// * set the controller Direction
else myPID.SetControllerDirection(REVERSE); //
}
Serial.flush(); // * clear any random data from the serial buffer
}
// unlike our tiny microprocessor, the processing ap
// has no problem converting strings into floats, so
// we can just send strings. much easier than getting
// floats from processing to here no?
void SerialSend()
{
Serial.print("PID ");
Serial.print(Setpoint);
Serial.print(" ");
Serial.print(Input);
Serial.print(" ");
Serial.print(Output);
Serial.print(" ");
Serial.print(myPID.GetKp());
Serial.print(" ");
Serial.print(myPID.GetKi());
Serial.print(" ");
Serial.print(myPID.GetKd());
Serial.print(" ");
if(myPID.GetMode()==AUTOMATIC) Serial.print("Automatic");
else Serial.print("Manual");
Serial.print(" ");
if(myPID.GetDirection()==DIRECT) Serial.println("Direct");
else Serial.println("Reverse");
}