Counting Boolean statues

Hi all

This has may have been asked before I am using Boolean to track the state of a set resistance. I have the Boolean set up and working how ever I am wanting to count the statues of the Boolean

I used a simple count

If Boolean = 1
I=I+1

However when I do this the count is climbing up until I change the state of the Boolean. how do I stop the count from climbing and count just the change of state?

how do I stop the count from climbing and count just the change of state?

Check when the state becomes true rather than when it is true

Take a look at the StateChangeDetection example in the IDE

Well, without any code how can we know?
The usual answer to questions like yours is to suggest that you read the state change tutorial. I am using a phone to reply, which makes searching awkward. Look for a tutorial on state change.

Read the state change detection tutorial and the update to the tutorial.

I count at least one Boolean statue:

It's so frustrating when politicians and journalists talk about "binary choices" when what they really mean is Boolean.

PerryBebbington:
It's so frustrating when politicians and journalists talk about "binary choices" when what they really mean is Boolean.

A Boolean choice is binary (ie true or false) but a binary choice is not necessarily Boolean (ie difficult or easy)

Binary is the name of a numbering system, it is not a choice between 2 things.

A variable of type bool stores a choice between 2 thing typically expressed as true or false, high or low, the terms don't matter, what matters is there are 2 of them. 'Bool' is short for 'Boolean', a Boolean choice is between 2 things. There's no such thing as a 'binary' choice, people use the wrong word.

If Boolean = 1
I=I+1

it is seriously confusing to use 'Boolean' as a variable name while 'boolean' is a variable type (as is 'bool' it's shorter form) moreover to compare a value one should use '==' not '=' , though without the braces it does not compile, nor does 'If' with an 'I' capital

Binary is the name of a numbering system

True, but that is only because its original meaning was something consisting of 2 parts, such as a binary star

See BINARY Definition & Usage Examples | Dictionary.com

UKHeliBob:
True, but that is only because its original meaning was something consisting of 2 parts, such as a binary star
See BINARY Definition & Usage Examples | Dictionary.com

That's even more confusing ! ! !

Have some Karma!

Binary seems to have been around for a while

https://www.google.com/search?q="binary"&tbm=bks&tbs=cdr:1,cd_min:1800,cd_max:1920&lr=lang_en

Hi all,

thanks for the input. so i have wrote my code out no i dont have an issue with count going up by its self but it seams to be stuck on one and not progressing here is what i have so far.

int sensorPin = A0;
int sensorValue = 0;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonbounce = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.print (sensorValue);
Serial.print (",");
Serial.print("on");
Serial.print (",");
Serial.print("number of button pushes: ");
Serial.print (",");
Serial.print (buttonState);
Serial.print (",");
Serial.println(buttonPushCounter);
buttonState = buttonbounce
 if (sensorValue <= 300)
buttonbounce = HIGH;
{
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
 // if the state has changed, increment the counter
 if (buttonState == HIGH) {
   // if the current state is HIGH then the button went from off to on:
   buttonPushCounter++;
   Serial.println("on");
   Serial.print("number of button pushes: ");
   Serial.println(buttonPushCounter);
 } else {
   // if the current state is LOW then the button went from on to off:
   Serial.println("off");
   buttonbounce - LOW;
 }
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
}

Please remove the unnecessary blank lines from your sketch, format with ctrl-T, and re-post it in code tags as required by the forum protocols. You have a misplaced '{' that is killing your logic.

Secondly, you shouldn't be using integer arithmetic on Boolean values. C/C++ is a permissive language, it will happily let you shoot yourself in the foot with no complaints at all. This statement does that, and yet throws away the result:

      buttonbounce - LOW;

You don't have any real problem, that can't be solved by simply studying C syntax. It is too lengthy to explain it all in a forum, you just need to go spend some time with tutorials and references.

issue resolved

int sensorPin = A0;
int sensorValue = 0;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonbounce = 0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.print (sensorValue);
  Serial.print (",");
  Serial.print("on");
  Serial.print (",");
  Serial.print("number of button pushes: ");
  Serial.print (",");
  Serial.print (buttonState);
  Serial.print (",");
  Serial.println(buttonPushCounter);
  buttonState = buttonbounce;
{
    if (sensorValue <= 300) 
  buttonbounce = HIGH;
else {
      buttonbounce = LOW;    
    } 
  {
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);     
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");    
    }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
}
}
}

issue resolved

Great! For the benefit of anyone that encounters this thread in future, what was the solution?

}
}
}

is no help in showing the code structure whereas if you use Auto format in the IDE the code blocks are clearly visible because of the indentation, as does putting each { and } on its own line. Also putting { and } round dependant code, even if it only one statement, makes the likelihood of making a mistake less likely

For example :

int sensorPin = A0;
int sensorValue = 0;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int buttonbounce = 0;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  sensorValue = analogRead(sensorPin);
  Serial.print (sensorValue);
  Serial.print (",");
  Serial.print("on");
  Serial.print (",");
  Serial.print("number of button pushes: ");
  Serial.print (",");
  Serial.print (buttonState);
  Serial.print (",");
  Serial.println(buttonPushCounter);
  buttonState = buttonbounce;
  {
    if (sensorValue <= 300)
    {
      buttonbounce = HIGH;
    }
    else
    {
      buttonbounce = LOW;
    }
    // compare the buttonState to its previous state
    if (buttonState != lastButtonState)
    {
      // if the state has changed, increment the counter
      if (buttonState == HIGH)
      {
        // if the current state is HIGH then the button went from off to on:
        buttonPushCounter++;
        Serial.println("on");
        Serial.print("number of button pushes: ");
        Serial.println(buttonPushCounter);
      }
      else
      {
        // if the current state is LOW then the button went from on to off:
        Serial.println("off");
      }
    }
    // save the current state as the last state, for next time through the loop
    lastButtonState = buttonState;
  }
}

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