Sequential "if" statements not functioning as expected

Good afternoon,
First something about me; I have recently started using Arduino UNO for several projects, not previously having any knowledge of C. I have however used (many years ago!) FORTRAN and programmed microprocessor (ie Intel 8748) using assembler and even a little machine code when operation was time critical.

Currently I have a project that requires action to be taken depended upon the state of 3, possibly 4 analog inputs. When the code I had writted failed I eventually traced the problem to a set of sequential "if" operations that were not working the way I expected.

The set-up was very simple, a 10k potentiometer was used between the 0 and 5V of the UNO with the slider connected to analog input A1. The intention was to detect 3 states of the input, 0 & 5V and approximately mid point or 2.5V. The "if" tests were threfore to check the value returned by analog.Read for <10, >1000 or between 450 & 650. The code is reproduced below:-
/
/*
DRT test of if function
*/

int pot = A1;
int potValue = 0;
int casecount = 0;

void setup()
{
casecount = 0; // variable to record input state
potValue = 0;

Serial.begin(9600); // necessary to monitor operation of code
}

void loop()
{
delay(1000); // delay set just to give loop time of approx 1 sec
potValue = analogRead(pot); // read the analog value
Serial.println(" "); // blank line to gice space between loop prints
Serial.print("potValue "); // send the result of analogread to monitor together
Serial.println(potValue); // with the "casecount"
Serial.print("casecount = ");
Serial.println(casecount);

if (potValue < 10) // test input vale for potentiometer at 0V end
{
int casecount = 100; // choice of value assigned is purely arbitary
Serial.print("casecount after if test <10 ");
Serial.println(casecount); // sent message to monitor to monitor operation of if test
}
else if (potValue > 40 && potValue < 650) // Test input for potentiometer at mid point
{
int casecount = 200;
Serial.print("casecount after if test for mid point ");
Serial.println(casecount); // as previously sent message to monitor to check
}
else if (potValue > 1000)
{
int casecount = 300;
Serial.print("casecount after if test for 5V input ");
Serial.println(casecount);
}

Serial.print("casecount after all if tests ");
Serial.println(casecount); // value of casecount sent to monitor AFTER all if tests
// It is expected that at this point IF one of the if tests has
// been satisfied the value of casecount sent to the monitor
// would be the value assigned in the statements associated with the
// satisfied if test

}
/
As noted my expectaion was that if one of the conditional test was satisfied (ie the value at A1 was 0V) the value assigned to casecount would be 100 (a purely arbitary choice) and that this would be the value at the end of the sequence of if tests. However the output to the serial monitor (for the 3 conditions) was:-
Potentiometer output at 0V
potValue 0 Output of the analogread action
casecount = 0 Case count at start of loop
casecount after if test <10 100 Shows that test is satisfied and casecount set to 100
casecount after all if tests 0 But after all if tests the value of casecount is zero
not 100!

Potentiometer at approx. mid point
potValue 519
casecount = 0
casecount after if test for mid point 200 Again if test satisfied and cascount set to 200
casecount after all if tests 0 But back to zero after all if tests
Potentiometer output at 5V
potValue 1023 Exactly the same happens when the input voltage is 5V
casecount after if test for 5V input 300
casecount = 0 casecount after all if tests = 0

So my question is simple; what is it that I do not understand about sequential if functions? How do I get the "casecount" pparameter to hold the value assigned in a satisfied "if" test so that it can be used in subsequent operation?

I have used Google (including the site specific) searches, bur could not find the answer.

So any help that can be given would be much appreciated
Thanks,
David

Lose the int here:

   int casecount = 100;

It's declaring a local variable.

wildbill,

Thank you for your reply - clearly I have a lot to learn about C and its use in programming Arduino. Is there a good book on the subject?

Again many thanks

David

That's a C problem, nothing to do with Arduino. I would suggest finding an online tutorial on C++ (there are lots) and work through the first few chapters. Stop before you get to classes - that can come later, if ever.

Again many thanks for helping a beginner learning C

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.