Hi i have a problem , i want to code a push button to reset my scale to zero but , when i click on it the scale start calibrating ,any solution!??
But without scale.tare(); function.
This is the code i'm using and the button is named (sw).
The bigger issue is, he's clearing the four values unconditionally, instead of when the button's pressed...
And, IIRC, that return may actually exit loop(), but not sure on that one as I rarely use return() to escape out of code structures like this.
Either way, he's somehow popping off into a function that's only executed via setup(), so something smells...
OK, I had to short out a few while loops becuase I do not have the hardware that would otherwise change and let them exit.
Having done that, the code seems to function, that is to say I see no indication that it is a software problem causing the reset.
The only thing the software might be doing just now is to get caught in one of those while loops. But that would mean no further changes.
@doudouprs you should add some serial printing as I did, to see the values of key variables, check that they are plausible and are properly informing the flow through your program.
In seetup(), add
Serial.begin(115200);
Serial.println("bob is your uncle\n");
then use Serial.print() very liberally all over the place.
@alto777
So in this case, the if structure will continue with the {} content, after executing the "return 0;" ? That's interesting. It was my impression that
if(something) statement; was a completed control structure.
I have no hardware connected, so I can't test this right now.
That would also mean the following needs revision:
specifically,
The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.
No
Note that @alto777 has reverted the condition of the if structure.
It is not an if structure will execute {} content. In the OP code the only contents of if structure is a return statement. The {} content is a normal code AFTER if structure. But because fairing the return will cause the code jump to the start of loop(), the {} content can be reached only if condition doesn't matched.
Nope. My point was, the return is the only statement executed conditionally, the rest execute unconditionally, leaving the OP thinking it's recalibrating.
But that's okay, you guys sort it out. I'm done.
Is identical in function, in the context from which it was snipped, to
if (digitalRead(sw) != 0) {
val=0;
sample=0;
w=0;
count=0;
}
The first one returns if the switch reads LOW, therefor does not reset the variables in the {} block, as that is never executed. The block is executed if the switch reads HIGH.
My rewrite executes the block if the switch reads HIGH.
Same same. Both versions conditionally execute those assignemnts.
Or I need to go to sleep or wake up. Always a possibility.
There will in any case be no need to change the documentation of the if statement.
I guess I'll try that when I get to the lab. Because as far as I know, the scope of the conditional ends after one statement, irregardless of anything beyond that, unless the 'door has been opened' for more than one statement, using {. Yes, I know, those are words, not the canonical phrasing from some authority, but there it is.
Maybe, if you have the option, try it in one of the simulators? Because I don't do sim, I cannot, until I have my hands on a Nano.
if(digitalRead(sw)==0) return 0; //<<<<<At this point, the if control statement has no more relevance. It has no more effect on subsequent statements, whether they're enclosed in {} or not.
Is identical in function, in the context from which it was snipped, to
if (digitalRead(sw) == 0) {
return 0;
}
else { // digitalRead was not == 0
val=0;
sample=0;
w=0;
count=0;
}
Again, completely equivalent logically.
Without being sure the OP wired the button the way we all might (pulled high, presst reads LOW) it looks like the button logic is backwards.
@doudouprs when you comb through all this, could you please post the code that just reads the scale and reports? I can't get it to function, and am too lazy to debug code when you probably have a working version with one or two fewer features. I hope.
Or point to any examples code that you are basing your code upon, or which you have actually stolen borrowed.