I´ve spent hours to try to find whats wrong here, with no success. I need help.
The sketch is supposed to give a Setpoint value in oC as input to a PID-calculation. The Setpoint is solely time dependent - its a burning curve for a pottery kiln. The different burning curves characteristics are stored on a mSD-card, with temperatures, times and slopes of the curve. All time variables are in millis. A typical file looks like:
0,0,21600000,650,25200000,960,26100000,960,27000000,20,27600000,0,30093,86110,0,-1044444,-33333\r
The maths I want it to do is fairly simple - just calculate Setpoint by a straight line equation:
Setpoint = dTemp/dTime * time + intersect.
All logics seems to work ok, with the exception of this straight line equation on row 99. Whats wrong?
/*
Calculation of setpoint temperature (C) for time values when thermocouples will be read.
Linear extrapolation from temperature vs time retrieved from maths on SetPoint-curves caracteristics stored on mSD.
Logfile created on mSD with time and Setpoint values.
SetPoint temperature is regarded as input to PID calculations.
Hardware is Arduino UNO and Sparkfun mSD card.
*/
#include <SD.h>
#include <ARDUINO.h>
//Declarations
char* BurnCurve[]={
"GLABEND.TXT", "RAWGLAZ.TXT", "DRYING_.TXT", "STOWARE.TXT", "STOGLAZ.TXT", "CLAGLAZ.TXT", "BISCUIT.TXT"};
int tempArray[6], time0, temp, incomingByte, Wait=0;
long time, time1, timeDiff, timeDiffmin, timeArray[6], Setpoint;
float konst, konstArray[6];
void setup()
//Open serial comm. & set comm. channels
{
Serial.begin(9600);
while (!Serial)
{
}
pinMode(10,OUTPUT);
if(!SD.begin(8))
{
Serial.println("initialization failed!");
return;
}
// Instructions to choose right Burn curve
for (int i=0; i<7; i++)
{
Serial.print(BurnCurve[i]);
Serial.print(" - Choose ");
Serial.print(i + 1);
Serial.println(" for this curve");
}
while(Serial.available()==0)
;
{
// read the incoming byte:
incomingByte = Serial.read();
incomingByte = incomingByte - 49;
Serial.println("");
Serial.print("Burn Curve is: ");
Serial.println(BurnCurve[incomingByte]);
File SPFile = SD.open(BurnCurve[incomingByte]);
Serial.println("");
if (SPFile)
{
for (int i=0; i<6; i++)
{
time = SPFile.parseFloat();
temp = SPFile.parseFloat();
tempArray[i] = temp;
timeArray[i] = time;
Serial.println("");
Serial.print(timeArray[i]);
Serial.print(" ");
Serial.println(tempArray[i]);
Serial.println(" ");
}
for (int i=1; i<6; i++)
{
konst = SPFile.parseFloat();
konstArray[i] = konst*1E-9;
Serial.println(konstArray[i],9);
}
}
else
{
// if the file failed to open:
Serial.println("error opening file");
}
// close file:
SPFile.close();
}
Serial.println("");
Serial.println("Removing loggfile...");
SD.remove("datalog.txt");
Serial.println(" START BURNING! ");
time0 = millis();
}
void loop ()
{
delay(1000); //New setpoint each sec
long time1 = 100 * millis(); //get current time in ms*100
long timeDiff = time1 - time0; //Time elapsed since start
long timeDiffmin = (timeDiff/60000);//Time elapsed in minutes
int i = GetIndex(timeDiff); //Calculate current index based on time
if(i<=5)
{
long timeDiffmin = (timeDiff/60000);
Setpoint = (long)(konstArray[i],9) * ((long)timeDiff - (long)timeArray[i - 1]) + tempArray[i - 1];
Serial.println ("");
Serial.print (i);
Serial.print (" ");
Serial.print (tempArray[i - 1]);
Serial.print (" ");
Serial.print (konstArray[i],9);
Serial.print (" ");
Serial.print (timeDiff);
Serial.print (" ");
Serial.print (timeArray[i - 1]);
Serial.print (" ");
Serial.print (Setpoint);
Serial.print (" ");
Serial.println (timeDiffmin);
// This is where reading of Thermocouples should be done.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println ("");
dataFile.print (i);
dataFile.print (" ");
dataFile.print (tempArray[i - 1]);
dataFile.print (" ");
dataFile.print (konstArray[i],9);
dataFile.print (" ");
dataFile.print (timeDiff);
dataFile.print (" ");
dataFile.print (timeArray[i - 1]);
dataFile.print (" ");
dataFile.print (Setpoint);
dataFile.print (" ");
dataFile.println (timeDiffmin);
dataFile.close();
}
else {
Serial.println("error opening datalog.txt");
}
}
else
{
Serial.println("Done!");
Wait = 1;
}
while(Wait==1)
;
}
int GetIndex(long Timediff) //Get index from time difference since start
{
for(int i = 0; i<6; i++)
{
if( (long) Timediff < timeArray[i])
return i;
}
return 6;
}
Bisquit_overwiev.doc (16.5 KB)