-1

This is a basic battery monitor for a solar-powered control box. It keeps the solar panels tracking the sun.

The Arduino monitors the voltage on the battery and turns on an AC-powered battery charger if the voltage drops too low.

It works, but there is an odd quirk.

  • Actual code is posted here. Too long to post and it does a bunch of other stuff that can be distracting.

One simple if statement isn't executing, as described below and shown on the plot.


HERE IS THE KEY CODE SEGMENT WHERE THE FAILURE OCCURS:

if (vind < vTriggerOn) {    // Batt low, turn on charger
  digitalWrite(powerPin,LOW); 
  digitalWrite(powerPin2,LOW);
  vAC=17.0;
  }  
if (vind > vTriggerOff) {   // Solar panels on, turn off charger
  digitalWrite(powerPin,HIGH); 
  digitalWrite(powerPin2,HIGH); 
  } 

//  HERE IS THE PROBLEM :  THIS ONE DOESN'T EXECUTE
if (vind > 15.5) {vind=15.48; vin=15.48;}   

This is followed by yet another if statement that works.

Problem: That if statement in question is supposed to keep it from going off-scale above the top of the plot. Yet it is not executing.

enter image description here

The two gray lines show that it is detecting the on and off triggers, but the voltage is going off the top. This should be impossible.

The first two if statements are executing but the third one is not.


Considered the fact that I hadn't upgraded the Arduino IDE since first installation. It is "Arduino 2:1.0.6:dpsg2-4"

Tried this:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install arduino

and it told me I had the latest version.


Question: How can I make it execute that third if statement?

  • 3
    Still don't compile. – user31481 Nov 24 '17 at 20:49
  • 2
    There is absolutely no problem with multiple if statements. How do you know that the third one doesn't "execute"? Maybe "other stuff..." is the problem. – Nick Gammon Nov 25 '17 at 6:42
  • 1
    I've noticed that when I have too many if statements, they may or may not work. absolutely not. You have a bug in code you haven't shown us. If the gcc compiler sometimes didn't execute "if" statements the world would grind to a standstill because of the amount of code that uses it. – Nick Gammon Nov 25 '17 at 6:44
  • 1
    Nick, you are right. It was indeed "other stuff" - having nothing to do with my source code. – SDsolar Nov 26 '17 at 15:43
  • 2
    The program you posted does not compile. I did the minimal fixes to make it compilable, then compiled it on an up-to-date Raspbian stretch (arduino-core 1.0.5, gcc-avr 4.9.2) and ran it on an Uno. It works as expected. It seems you misdiagnosed your problem. My guess is you had some issue in your code, which is not in the part you showed us. Either you fixed the bug when upgrading the IDE, or it just stopped manifesting itself (this can happen when you have undefined behavior). Then you wrongly blamed the Arduino IDE. – Edgar Bonet Nov 26 '17 at 15:48
1

This a new answer, upon the complete, original sketch as posted here by OP.

I deleted all the code that doesn't affect vind directly. Also, I replaced the value=analogRead(A2) with worst case scenario value=1023. Also tested with 781 and 782, which produces values for vind around 15.5.

Reduced sketch

const float R1 = 147000.0;
const float R2 =  47000.0;
const float constADC = 5.0;
const float offsetADC = -0.25; //
const float convADC = constADC / 1024.0;
const float numReadings = 10.0;
const float vTrigAlertLow = 12.1;

float nReadings = 0.0;
float tReadings = 0.0;

int value = 0;
float vout, vin, vind;

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("");
}

void loop() {
  int value = 1023;
  vout = value * convADC;
  vin = vout / (R2 / (R1 + R2));
  vind = vin + offsetADC;
  tReadings = tReadings + vind;
  nReadings = nReadings + 1.0;

  Serial.print(nReadings); Serial.print(" "); Serial.print(tReadings); Serial.print(" ");   Serial.println(vind);

  if (nReadings >= numReadings) {
    vind = tReadings / numReadings;
    tReadings = 0.0;
    nReadings = 0;

    Serial.print("Before "); Serial.print(vind);
    if (vind > 15.5) vind = 15.48; // <----  PROBLEM - not executing
    Serial.print(" After "); Serial.println(vind);

    if (vind > vTrigAlertLow) { // Normal, otherwise below vTrigAlertLow it will send alerts in the 'else' below
      Serial.println(vind);
    }
    else {
      Serial.println("LOW VOLTAGE");  // send Texts
    }
    Serial.println("----");
  }
}

The output is

1 20.37 20.37
2 40.74 20.37
3 61.10 20.37
4 81.47 20.37
5 101.84 20.37
6 122.21 20.37
7 142.58 20.37
8 162.95 20.37
9 183.31 20.37
10 203.68 20.37
Before=20.37 After=15.48
Send to PI 15.48
----

The if works as intended. It must be something else.

  • Thanks for going to the trouble of testing it. It did indeed turn out to be something else. – SDsolar Nov 26 '17 at 15:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.