Well, the for loop is useless. Then you need to plan your time cycle. For instance what you want is
time(ms) LED3 LED4
0 127 254
500 254 127
1000 -> back to 0
So implement this. One way can be the one suggested by st2000:
int LED3 = 5;
int LED4 = 6;
// just one tracking, since there is only one sequence
unsigned long previousMillis=0;
int endOfPhase1 = 500;
int endOfPhase2 = 1000;
void setup() {
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
}
void loop() {
unsigned long currentMillis = millis();
if ((currentMillis - previousMillis) < endOfPhase1) {
analogWrite(LED3, 127);
analogWrite(LED4, 254);
}
else if ((currentMillis - previousMillis) < endOfPhase2) {
analogWrite(LED3, 254);
analogWrite(LED4, 127);
}
else
previousMillis += endOfPhase2;
}
Just a small note about this: I tend to prefer summing the period instead of setting previousMillis to millis because this way small errors do not accumulate.
This way is the simplest, but the state machine oriented is the one I suggest you, since it is more powerful and you will reuse this a lot of times. Moreover it will detect the "change" of state, thus avoiding the continuous analogWrite (in the previous code the analogWrite instructions are executed a lot of times).
Just define a state machine as two states (state 1, where LED3 is 127 and LED4 is 254, and state 2, where LED3 is 254 and LED5 is 127). The transitions will be after 500ms in each state, but I'll make two separate constants so you can trim them later. Usually each state has a function to be performed when you enter the state, a function to run continuously, a function when you exit and one or more transitions. The way to perform this is
- calculate if there is a state change
- if there is, perform the exit functions of the previous state and the enter functions of the next state
- advance to the new state
- perform the actions to be done continuously in the new state
In our case just state transition, enter and advance should be implemented. Usually you use a switch case on a variable holding an index for the state. In this case, for instance,
int LED3 = 5;
int LED4 = 6;
// just one tracking, since there is only one sequence
unsigned long previousMillis=0;
int state1duration = 500;
int state2duration = 500;
uint8_t state;
void setup() {
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
state = -1; // Invalid state, used to force a transition at startup
}
void loop() {
unsigned long currentMillis = millis();
// Check the transition
uint8_t nextState = state;
switch (state)
{
case 1: // Transition for state 1
if ((currentMillis - previousMillis) >= state1duration)
{
nextState = 2;
previousMillis += state1duration;
}
break;
case 1: // Transition for state 2
if ((currentMillis - previousMillis) >= state2duration)
{
nextState = 1;
previousMillis += state2duration;
}
break;
default: // When unknown state, go to state 1
nextState = 1;
break;
}
// If there is a change, perform the enter functions
if (nextState != state)
{
switch (nextState) // enter functions of the next state
{
case 1: // Entering state 1
analogWrite(LED3, 127);
analogWrite(LED4, 254);
break;
case 2: // Entering state 2
analogWrite(LED3, 254);
analogWrite(LED4, 127);
break;
}
}
// Advance to the new state
state = nextState;
}
This can be more complicated, but believe me, it is not. Once you get what this means you will be able to solve a lot of problems this way.
According to my experience, never try to simplify this to your particular problem, since it will make things more complicated. Just add, when needed, the exit functions (before switch(nextState) put another switch on state) and the execution (after the advancing, put another switch on state) if needed.