use an enum or #defines - both will work fine and store your state in as an integer (or enum).
typedef enum state {ON, BLINK, OFF} state;
state LED1state, LED2state, LED3state, LED4state;
...
{
if (LED1state == BLINK)
{
// blink
}
else
digitalWrite(1, (LED1state == ON) ? HIGH : LOW);
if (LED2state == BLINK)
{
// blink
}
else
digitalWrite(2, (LED2state == ON) ? HIGH : LOW);
if (LED3state == BLINK)
{
// blink
}
else
digitalWrite(3, (LED3state == ON) ? HIGH : LOW);
if (LED4state == BLINK)
{
// blink
}
else
digitalWrite(4, (LED4state == ON) ? HIGH : LOW);
}
you are of course missing some logic as to how you'll do blinking - but you didn't include this in your original question so i've left that as an exercise for you. this is more of a basic C/C++ language question regarding the use of variables, statements and work-flow constructs more than an arduino specific question.
you may want to start with an "introduction to C" type tutorial to teach you some of the basics of how to program in C, then move onto C++ (which is used in Arduino libraries). Introduction to C - Cprogramming.com
When i try to create the enum, it gives me an error:
LEDbox3_ino:79: error: expected identifier before numeric constant
LEDbox3_ino:79: error: expected `}' before numeric constant
LEDbox3_ino:79: error: expected unqualified-id before numeric constant
LEDbox3_ino:79: error: expected declaration before '}' token
relevant part of the code:
long int setpDo;
//variables
typedef enum state {ON, BLINK, OFF} state;
state zone1state, zone2state, zone3state, zone4state;
const int interval = 900;
I did try to look up some examples, but none of the examples i saw for enums have that second line (state zone1state....)
I would be inclined to attack the problem differently - something like this
void updateLED() {
for (byte n = 0; n < numLeds; n++) {
digitalWrite(ledPin[n], ledState[n]);
if (ledBlink[n] == true && millis() - ledPrevMillis[n] >= ledInterval[n]) }
ledPrevMillis[n] += ledInterval[n];
ledState[n] = ! ledState[n];
}
}
}
It assumes there is a variable (an array) called ledBlink[] which is either true or false depending on whether you want blinking. When it is true it overrides the setting of ledState[]. When it is false the value in ledState[] determines whether the led is on.
I figured out the problem with the code, you can't use LOW or HIGH as variables, they are reserved for arduino. I changed them to ISON and ISOFF and now it works.
@Robin, i am looking for the optimal way of doing this, so, in your example though wouldn't that cause all the LED's to become a group, eg, they can ALL either be blinking, or they ALL can be OFF, or they ALL can be OFF.
I need to be able to separate them,
for instance.
Zone one and four is on, so their LED is on
zone two is in set up, so it's LED is blinking
Zone three is off, so it's led is off.
Its a conditional expression, which isn't really an operator because its two operators! ? :
Its very handy for keeping code clear and concise at the same time. If you ever
see something like:
Qdeathstar:
in your example though wouldn't that cause all the LED's to become a group, eg, they can ALL either be blinking, or they ALL can be OFF, or they ALL can be OFF.
No. There is a separate control for each - for example ledBlink[n] can have as many elements as needed - one for each LED. Likewise there can be different blink intervals for each. I have assumed equal off and on times - but you could extend the idea to have separate off and on intervals.
You could probably tidy up the code by using a struct to define all the data for one LED and then just have an array of structs. But that may actually be more verbose.
which means that new variables are created every time blinker() is called. You can do one of 2 things - add the word static before the definition static unsigned long ledPrevMillis[4]; or move the declarations to the top of your code so they are global variables (which would be my personal preference).
Also, I don't understand why you are using a time test before you call blinker(). It should be called in every iteration of loop().
Please explain, in English, what you think this test is doing. It almost certainly isn't doing that, but fixing it to do what you think it is supposed to do depends on what you think it is supposed to do.
also; use brackets to clearly identify your conditions and the logical operations between them. - || (logial or) is not the same as | (binary or), like && (logical and) is not the same as & (binary and) - i suspect you mean to write:
Robin, I moved the arrays to the top of my sketch, and changed "byte ledInterval" to long int, but it still didn't have how fast the buttons were blinking.
Please explain, in English, what you think this test is doing. It almost certainly isn't doing that, but fixing it to do what you think it is supposed to do depends on what you think it is supposed to do.
For each value in buttonArray, if that value equals 2 or 3, and if it is time, blink the lights.
It appeared to be working (but the blinking was fast). I've updated it to ardiri's suggest, as that seems to make more since