Variable problem

Guys and gals,

Several of you Gurus have been good enough to give me pointers, recently, which have enabled me to fix problems I have had.

Now I have another one for you. ("Groan", did I hear??).

I am writing a sketch that enables "surplus energy" from a solar system, ie after the battery reaches full charge, to be used to drive particular devices.

Up till now, the testing and debugging has gone well, but I have hit, for me, a solid wall.

In the sketch, I jump to a function readPulseButton(), which is where things go wrong.

Using Serial.println, I step my way through it. Knowing I need to check if the pulse button is pressed, I read the digital pin and I can see it toggling at the #1 block of Serial.print, but at #2, it is solidly latched at 0.

I would be very grateful if one of you would be so kind as to tell me how and where I have stuffed up.

Note: later functions, have not been tested yet, and are bound to contain bugs.

Due to length, I have had to attach the sketch.

TIA

Fof

Kevs_System_V6.ino (12.6 KB)

Line 135:

           if ((IsPulseButtonPressed = 0) and (PulseisRunningFlag = 1))              // Is the button pressed and Pulse O/P active?

line 145:

          if ((IsPulseButtonPressed = 0) and (AuxisRunningFlag = 1) and (PulseisRunningFlag = 0))                // Is the button pressed?

You need to use == instead of =. Your current code sets the variables instead of testing the value of the variables as intended.

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

With 12 kB size? I doubt it, last time I encountered it, the post limit was 9 kB.

while (count < 3)
  {
    store = ((float)analogRead(A2) / 1023) * 5 * 4.8;                     // acquire and process ADC value to give result in volts
    batteryVoltage = batteryVoltage + store;                              // add the values together
    count++;                                                              // increment counter
    delay (50);
  }

  count = 0;

May or may not be a problem, but shouldn't you reset batteryVoltage before you start adding data to it?

-jim lee

IsPulseButtonPressed = digitalRead(PulseButton_Pin);                       // Capture state of the Pulse button (0 = pressed, 1 = not pressed)

if your comment is correct, then your variable name is opposite to what it means.

IsPulseButtonPressed is true that means its not pressed. Don't do this, it'll blow up your brain.

IsPulseButtonPressed = !digitalRead(PulseButton_Pin);                       // Capture state of the Pulse button (0 = pressed, 1 = not pressed)

Now when IsPulseButtonPressed is true its actually true that it was pressed. When false.. You see?

-jim lee

I'd make it even more clear:

const byte buttonPressedState = LOW;
if(digitalRead(PulseButton_Pin) == buttonPressedState) {
  IsPulseButtonPressed = true;
}

You could also write it as:

IsPulseButtonPressed = (digitalRead(PulseButton_Pin) == buttonPressedState);

but I prefer the first way because it makes it very obvious what the code is doing.

May or may not be a problem, but shouldn't you reset batteryVoltage before you start adding data to it?

Not really necessary, since most times, 0 is all that is added.

    store = ((float)analogRead(A2) / 1023.0) * 5.0 * 4.8;

would produce quite different values, though.

JimLee

#4 - Well spotted. It didn't seem to cause issues, when I debugged that bit, but it sure as hell should. Looked back at the earlier iterations, and lo and behold, there is battery = 0. Did I accidently delete it? or Whoops!

#5 - Yes. You read it correctly, and yes it did, still does, scramble my brains when debugging. I thought about using ! but decided to try it at the next iteration, once it is working.

Pert

#6 - I like your idea. Being new to this game I had missed the fact that int is a 2 byte variable, while byte is, well a byte. Less memory usage. Nice one.

PaulS

See comment on #4, above.
I don't know why it didn't go feral on me. That part of the sketch is read every circuit of the loop and there will always be a voltage of between roughly 2v and 4v. Luck, or maybe even a bug, must have been on my side that day. :-))

Thanks guys