I know this question with toggle has been asked many times...
I already found one at the playground and works fine. Toggles a led on and off.
But I was wondering, can you do something like that to a global variable?
For example int gVar = 2;
Then I insert the rest of the code...
I have 4 leds, I create 4 functions that are called from the loop() function.
They control each of the leds on the board.
Their pins are 2, 3, 4 and 5, that's why I made "gVar = 2;"
In one function that's for the led on pin 2, it checks if gVar is 2.. if it is, the led will turn on HIGH. Otherwise it will turn off LOW. And that goes for the rest of the functions, except it counts up...
I also have 2 pushbuttons, so with the code "for toggle", one pushbutton raises gVar by one gVar++ and the other one lowers gVar--, but don't worry, I limited it. So it basically makes an X axis game, walk around or something.
Is something like this even possible? I used server.print to see the gVar variable, it doesn't limit and changes all on It's own.
Excuse my English and the fact that you might didn't understand what I was trying to say.
But it would probably a very bad idea to implement it that way.
You don't control target selection (LED) by global vars, but by function parameters.
You don't control inputs selection (buttons) by global vars, but by function parameters.
The parameter would typically be an index into one or more arrays (pins[], states[], ...),
or a pointer to a structure holding all the individual object attributes in one place.
from what I am reading you are asking if 4 functions can use one global
the answer is yes you can use one global in 4 different functions as long as you remember that any change to the global is changed every place its used in the program
so if int gVar = 2; is your global you can use it with in the 4 functions but lets say that function 2 adds one to gval then gval now equals 3 any place its referenced.
Now from what you have said I see that you are using pins 2, 3, 4 and 5 so you can count 2 to 5 a better way would be to look at using a array to hold the pin numbers then the pin numbers do not have to be sequential
e.g say you want to use pin numbers 5, 8, 11 and 13 then using a array the program will see them as array 0, 1, 2, 3. which means you can still use a counter even if the pin numbers do not follow each other.