Hi guys
I have written some code to power a fan system and although it appears to work fine, I have a really strange issue with one of the variables.
The variable in question is "FanRun"
In my project "FanRun" can only be 0 (for off) or 1 (for on), yet when I run it on my circuit I get a Serial.print return of almost any value.
I have just uploaded it into my Uno PCB with no other inputs connected and I am getting "FanRun" returning as 2 and sometimes 3.
Anyone see an obvious fault in my code (attached)?
Fume_1_0c.ino (13.4 KB)
I have found the problem:
PBKAC - of course
I had set byte ValveTimeout[6];
And then used a For loop for (i = 0; i < 7; i++)
while using "i" to set the array index.
So of course I would write ValveTimeout[6] which doesn't exist (0nly 0-5).
My correct For loop is for (i = 0; i < 6; i++)
, calling (setting) Array index 0-5.
byte ValveTimeout[6];
///...code...//
for (i = 0; i < 7; i++)
{
if (bitRead(Irons, i) > 0) // If the Irons is On
{
ValveTimeout[i] = 0; // Keeps counter at 0
You have exceeded the size of the array here. So my guess is that you've made the same mistake in other places as well. In any case, this error will result in other variables being corrupted.
It's a good reason to use constants to represent array sizes, and then use those constants in both the declaration and the code, for example:
const byte valveTimeoutSize = 6;
byte ValveTimeout[valveTimeoutSize];
///...code...//
for (i = 0; i < valveTimeoutSize; i++)
{
if (bitRead(Irons, i) > 0) // If the Irons is On
{
ValveTimeout[i] = 0; // Keeps counter at 0
But of course you would tailor this to your code. Another benefit is that you can safely and easily increase the size of the array later if need be. If you're careful, you can also use #define for this purpose.