{SOLVED} Not Understanding IF Statement Operation

int dnButton = 2;
int upButton = 3;
int decrease = 0;
int increase = 0;
int stepNumber = 1;
int targetAmps = 1;
int current = 0;

void setup() {
  pinMode(dnButton, INPUT_PULLUP);
  pinMode(upButton, INPUT_PULLUP);


void loop() {

  decrease = digitalRead(dnButton);             // read the down button
  increase = digitalRead(upButton);              // read the up button

  if (decrease == 0){Serial.println ("decrease"); --targetAmps;}   // increase the desired amps
  if (increase == 0){Serial.println ("increase"); ++targetAmps;}    // decrease the desired amps
  if (targetAmps > 99) {targetAmps = 100;}                             // max amps = 100
  if (targetAmps < 2) {targetAmps = 1;}                                  // min amps = 1

  if (decrease + increase >1){                   // display target amps, real amps & load step
    Serial.print ("hold");
    Serial.print ("     ");
    Serial.print (targetAmps);
    Serial.print ("     ");
    int sensorIn = analogRead(0);               // read the current sensor (0-1023 counts)
    int current = ((sensorIn - 125) * .135);  // set zero and calculate amps @ .135A/count
    Serial.print("     ");
    Serial.println (stepNumber);                  // show the current load step #

  if (targetAmps > current) {++stepNumber;}  // **if current decreases, decrease loading (more steps)
  if (current > targetAmps) {--stepNumber;}   // if current increases, reduce loading (less steps)

The purpose of the above is to set the value of current I want to draw out of a battery and have my load mechanism (incremental resistances) hold that current relatively constant until the battery voltage drops to a predetermined level in order to evaluate the actual current capacity of the battery.

There is obviously much more code monitoring the battery voltage, selecting the load points, etc.

The problem I am having is with the ** if statement (next to last). Though I have targetAmps showing “1”, and current is reading “2”, which should make that “if statement” evaluate as false, instead, my load begins to increment the moment I load the program.

No matter what value I set “targetAmps” to the load will always increment, even when the “current” goes way above the “targetAmps” setting.

Somehow the logic does not seem to follow the math.

Can anyone please shed light?


Sorry, this runs on an Arduino UNO.

   if (decrease + increase >1) {
      int current = ((sensorIn - 125) * .135);  // set zero and calculate amps @ .135A/count

This "current" only exists inside this if() block. Remove the "int" before it.


Ok, I got it.

I moved the "current" calculation out of the if() statement, giving it a global scope, and it worked perfectly.

Thanks so much for your help!!!