/* I was working on my reflow oven program when I stumbled into this
* conundrum. I want to stuff the reflow profile into an array and
* then send the second by second temperature to the PID and let the
* oven heat along the profile. I ran into a problem using values from
* the array in the loop() function. I isolated the pertinent code in the
* interest of brevity. It fails in exactly the same manner as my full
* code.
* Using Arduino 1.8.1 and Arduino Nano in Windows 10.
*/
double Profile[220], DegPerSec, Setpoint=0; // global declarations
void setup() {
while (!Serial); // wait for Serial Port to open
Serial.begin(9600); // set the baud rate
CalculateProfile(); // stuff heat values into Profile[]
Profile[10]=30.0; // set a value for debug purposes
}
void loop() {
/* Right here, if there is no "Setpoint=20.0;", then the program seems to lock
* up. The Serial.println functions never occur. The Serial Monitor shows no
* activity. If I DO have the "Setpoint=20.0;", then everything works.
* Is this something that I do not understand about arrays? I am pretty new
* so I am sure that I am missing something obvious but I cannot find anything
* in documentation. I stummbled onto how to get it to work, ie. the
* "Setpoint=20.0;" declaration...
* Remove the comments slashes in the next line to watch it work
* Add the comments slashes to watch it fail
*/
//Setpoint= 20.0;
Serial.println("Start of loop");
Serial.println(Profile[10]);
Serial.println(Setpoint);
Setpoint=Profile[10];
Serial.println(Setpoint);
delay(2000);
}
/* CalculateProfile() calculates the heat curve for the selected
solder paste and puts the temps into an array
*/
void CalculateProfile() {
// calculate the Preheat ramp, starting at 20.0 degrees
DegPerSec = (150 - 20.0) / 90.0;
for (int x = 0; x < 91; x++)
{
Profile[x] = (x*DegPerSec) + 20.0;
}
// Calculate the Soak ramp
DegPerSec = (180 - 150) / 90.0;
for (int x = 91; x < 181; x++)
{
Profile[x] = (x - 90)*DegPerSec + Profile[90];
}
// Calculate the Reflow ramp
DegPerSec = (220 - 180) / 30.0;
for (int x = 181; x < 221; x++)
{
Profile[x] = (x - 180)*DegPerSec + Profile[180];
}
}
for (int x = 181; x < 221; x++)
That's certainly wrong, since it will set the 221st element of your 220 element array. (You have 0 to 219...)
That will overwrite "something" and cause unknown behavior.
That was it. I had no idea that going outside the array size could derange matters so badly. Everything is working as it should now.
Thank you for your help.
Ron
Yeah, C doesn't do any bounds checking on array accesses, so writing beyond the end of the array will overwrite some random piece of RAM; something that could be not noticed at all, or something that is critical to the operation of the program.