Hey there,
I am very new to object oriented programming, but I have written a simple class which has a method which returns the state of the button. However I would like to make it a toggle button, so I need to know the state the button was the last time the method was called. I have done some research on it, and it seems that static variables are the way to go.
But when I define the static variable like public or private variables like so:
static:
boolean _statel
I get the following error: expected primary-expression before '_statel'
When I simply put it under the private vars like so:
public:
static _statel
It simply does not work, the variable does not seem to be stored.
Am I simply asking for something I am not intended to, or am I doing something wrong?
Create a private variable to store the state of the button. Use that variable from the functions of the class (because it is private). Forget about static. Show us a sketch that we can test.
Delta_G:
I didn't mean a static function, I just meant a local variable in a regular class function where the local variable was declared static.
But, you learn something every day. When I tested this sure enough the two classes shared the same variable even though it was local to a non-static function. I stand corrected.
You have to question why they couldn't have invented another keyword for static class members or static variables in a function...
maybe because there was just no better word to describe either case!
javamaster10000:
But when I define the static variable like public or private variables like so:
static:
boolean _statel
I get the following error: expected primary-expression before '_statel'
Syntax is
static boolean foo;
'static' is a storage class specifier, 'boolean' is a type specifier.
Having said this 'static' means "there's only one of 'em". What happens if you want two toggle buttons? Using a class is the right approach. You want an ordinary member variable to hold the current/previous state.
I like using constructors to do pin assignments, and I like all of these classes to have their own setup() and loop() methods, which I call even if I don't do anything with them.
See my blog post ArduinoTheOOWay for more on how I personally go about doing this kind of stuff..
Koepel:
Create a private variable to store the state of the button. Use that variable from the functions of the class (because it is private). Forget about static.
I mean, if for example I create an object of that type, named button1 and an object named button2, all static vars are share across them, so the code would not work, is this correct?
someone pointed out the semicolon was missing at the end of the definitions, I actually have them in the code.
No, the code WOULD work exactly the way that the language developers expected. That may not be the same way YOU expect, but when that happens, it only means that your expectations are wrong.
someone pointed out the semicolon was missing at the end of the definitions, I actually have them in the code.
Then THAT is the code you should have posted.
By now, you certainly should have gotten the message that a static variable is not what you (seem to) want.
javamaster10000:
I mean, if for example I create an object of that type, named button1 and an object named button2, all static vars are share across them, so the code would not work, is this correct?
Depends on what you mean by "work". If you want the variables shared, it will work. If you don't want them shared it won't work.
javamaster10000:
However I would like to make it a toggle button, so I need to know the state the button was the last time the method was called. I have done some research on it, and it seems that static variables are the way to go.
Why? If each class member has to remember the state of the button, then static variables are not appropriate.