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?
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.
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.
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
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.
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;
}
}
}
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;
}
}