# If-statement gets ignored?

I'm kind of stuck with strange behaviour of my code and I really hope, someone could help me..

Objective I'm trying to build a (multiple) magnetic stirring device with which I can either control the speed via potentiometer or via serial commands from a python script from pc (usb). When the speed is set via serial, it shouldn't be possible to interfere with the potentiometer unless a button is pressed to unlock it.

Code

``````// Constants

const int FanPin[7] = {2,3,5,6,7,8,9}; // PWM Pins 2,3,5,6,7,8,9 for fan
const int PotiPin[7] = {A0,A1,A2,A3,A4,A5,A6}; // Analoge Pins 0,1,2,3,4,5,6 for potentiometers
const int SwitchPin[7] = {30,31,32,33,34,35,36}; // Digital Pins 30, 31, 32, 33, 34, 35, 36 for switches

// Variables
boolean ManualLock = false; // will be true if locked by serial input
int Fan[7][2] = { // [x][] for number of fan, [][0] for fan speed, [][1] for ManualLock
{0, ManualLock},
{1, ManualLock},
{2, ManualLock},
{3, ManualLock},
{4, ManualLock},
{5, ManualLock},
{6, ManualLock}
};
int temp = 0; // Variable for temporary data
int FanSpeed[7] = {0,0,0,0,0,0,0}; // Variable fan speed
int PotiVar[7] = {0,0,0,0,0,0,0} ; // Variable for saving poti-input
byte bfr[2] = {7,7}; // Buffer for serial data

void setup() {

TCCR2B = TCCR2B & 0b11111000 | 0x01; // Timer2 (Pin 9, 10) @ 31000Hz
TCCR3B = TCCR3B & 0b11111000 | 0x01; // Timer3 (Pin 2, 3 und 5) @ 31000Hz
TCCR4B = TCCR4B & 0b11111000 | 0x01; // Timer4 (Pin 6, 7 und 8) @ 31000Hz

Serial.begin(9600);
delay(100);

// Declaration of Pin Modes
for (int i = 0; i < 7; i++){
pinMode(FanPin[i], OUTPUT);
pinMode(PotiPin[i], INPUT);
pinMode(SwitchPin[i], INPUT);
}
}

void loop() {
for (int i = 0; i < 7; i++){
// ManualLock not active -> manual mode
if (!Fan[i][1]){
//Serial.print(i);
//Serial.println(" ManualLock not active");
Fan[i][0] = map(PotiVar[i], 51, 1023, 60, 255);
//Serial.println(Fan[i][0]);
}
//ManualLock active -> unlock by pressing button
if (Fan[i][1]){
if (temp == HIGH){
Serial.print(i);
Serial.println(" ManualLock will be deactivated");
Fan[i][1] = false;
Serial.print("Fan[i][1]: ");
Serial.println(Fan[i][1]);
}
}

// fan off @ low poti setting
if (PotiVar[i] < 50){
Fan[i][0] = 0;
}
}

// look for serial data

if (Serial.available()){
Serial.println("Something's there!");
delay(1000);
Serial.print("Serial data: ");
Serial.println(bfr[0]);
//  Serial.flush();
}

//Serial.print("before loop bfr[0]: ");
//Serial.println(bfr[0]);

for (int i = 0; i < 7; i++){
if (i == int(bfr[0])){
Serial.print("i: ");
Serial.println(i);
Fan[i][0] = int(bfr[1]); //set fan speed variable
Fan[i][1] = true; // activate ManualLock variable
byte bfr[2] = {7,7}; // clear buffer
Serial.print("bfr[0]: ");
Serial.println(int(bfr[0]));
}
}

for (int i = 0; i < 7; i++){
analogWrite(FanPin[i], Fan[i][0]); // write fan speed
}
}
``````

Error(s)

When I send a command via my python script, the speed is set correctly and I get the following output:

``````Something's there!
Serial data: 0
i: 0
bfr[0]: 7
i: 0
bfr[0]: 7
...and again and again and again...
``````

Which means, although i "reset" my buffer variable to something that should prevent entering the if-statement, it will somehow fulfill the "if (i == int(bfr[0]))" condition.. but why? If any further information is needed, i'll happily provide that.

This (inside your `for` loop) does not do what I think you think it does:

``````byte bfr[2] = {7,7}; // clear buffer
``````

You think it sets the contents of the `bfr` array you declared at the top to `{7,7}` but what it actually does is declare a new byte array in that scope, containing the values `{7,7}`. It's not used and is immediately thrown away again.

I would expect the compiler to warn about it -- does it? (If so, take it as a lesson not to ignore compiler warnings!)

I think you want to replace it with

``````bfr[0] = 7;
bfr[1] = 7;
``````

or something equivalent.

• unfortunately, there was no warning. but now it works! thank you so much! Jan 25, 2017 at 23:24