Hi @hackbart77
welcome to the arduino-Forum.
Well done posting your code as a code-section in your very first posting.
So if your if - else is INSIDE the function loop() it will work.
You should use meaningful names instead of numbers.
You did not describe what your IO-pins 3,4,5 are
So I named them at least IO_PinA, IO_PinB, IO_PinC
int switchState = 0;
const byte IO_PinA = 3;
const byte IO_PinB = 4;
const byte IO_PinC = 5;
const byte mySwitchPin = 2;
void setup() {
pinMode(IO_PinA, OUTPUT);
pinMode(IO_PinB, OUTPUT);
pinMode(IO_PinC, OUTPUT);
pinMode(mySwitchPin, INPUT);
}
void loop() {
switchState = digitalRead(mySwitchPin);
if (switchState == LOW) {
digitalWrite(IO_PinA, HIGH);
digitalWrite(IO_PinB, LOW);
digitalWrite(IO_PinC, LOW);
}
else {
digitalWrite(IO_PinA, LOW);
digitalWrite(IO_PinB, LOW);
digitalWrite(IO_PinC, HIGH);
delay(250); // freeze_microcontroller_completely_stop_all_codeexecution_until_freezingtime_is_over
digitalWrite(IO_PinB, High);
digitalWrite(IO_PinC, LOW);
delay(250); // freeze_microcontroller_completely_stop_all_codeexecution_until_freezingtime_is_over
}
} // END of function loop()
You are using function delay().
In two days function delay() will cause problems to proceed any further iif you want to do two things in parallel.
You should learn non-blocking timing right from the start.
Unofrtunately the standard example about non-blocking timing is a real bad one.
This standard example misses to explain the fundamental difference between delay() and non-blocking timing.
The function delay() should have the name
freeze_microcontroller_completely_stop_all_codeexecution_until_freezingtime_is_over()
non-blocking timing makes the code loop very fast all the time and execute code that shall be executed all the time UNconcitional
And the actions that shall be executed only after some time are executed CONDITIONAL with a timing-condition.
Your code does a sequence of steps
step 1 switch on IO-pin 5
step 2 wait for 250 milliseconds
step 3 switch on IO-pin 4
step 4 wait for 250 milliseconds
repeat
if button is pressed then switch on io-pin 3
wait 250 milliseconds
Here is a code that does a sequence to but in a non-blocking manner
step 0 switch all off
if switch is LOW
step 1 switch on IOpin 5
step 2 wait 250 milliseconds
step 3 switch on IO-pin 4
step 4 wait 250 milliseconds
step 5 switch on io-pin 3
step 6 wait 250 milliseconds
repeat
int switchState = 0;
const byte IO_PinA = 3;
const byte IO_PinB = 4;
const byte IO_PinC = 5;
const byte mySwitchPin = 2;
const byte sm_allOff = 0;
const byte sm_switchOnA = 1;
const byte sm_waitWithA_On = 2;
const byte sm_switchOnC = 3;
const byte sm_waitWithC_On = 4;
const byte sm_switchOnB = 5;
const byte sm_waitWithB_On = 6;
byte mysequenceStep = sm_allOff;
unsigned long myTimer;
void setup() {
pinMode(IO_PinA, OUTPUT);
pinMode(IO_PinB, OUTPUT);
pinMode(IO_PinC, OUTPUT);
pinMode(mySwitchPin, INPUT);
}
void loop() {
switchState = digitalRead(mySwitchPin);
myStepSequence();
} // END of function loop()
void myStepSequence() {
switch (mysequenceStep) {
case sm_allOff:
if (switchState == LOW) {
mysequenceStep = sm_switchOnA;
}
break; // IMMIDIATELY jump down to END-OF-SWITCH
case sm_switchOnA:
digitalWrite(IO_PinA, HIGH);
digitalWrite(IO_PinB, LOW);
digitalWrite(IO_PinC, LOW);
myTimer = millis(); // store snapshot of time
mysequenceStep = sm_waitWithA_On;
break; // IMMIDIATELY jump down to END-OF-SWITCH
case sm_waitWithA_On:
// check if waiting-time has gone by
if ( TimePeriodIsOver(myTimer, 250) ) {
mysequenceStep = sm_switchOnC;
}
break; // IMMIDIATELY jump down to END-OF-SWITCH
case sm_switchOnC:
digitalWrite(IO_PinA, LOW);
digitalWrite(IO_PinB, LOW);
digitalWrite(IO_PinC, HIGH);
myTimer = millis(); // store snapshot of time
mysequenceStep = sm_waitWithC_On;
break; // IMMIDIATELY jump down to END-OF-SWITCH
case sm_waitWithC_On:
if ( TimePeriodIsOver(myTimer, 250) ) {
mysequenceStep = sm_switchOnB;
}
break; // IMMIDIATELY jump down to END-OF-SWITCH
case sm_switchOnB:
digitalWrite(IO_PinB, HIGH);
digitalWrite(IO_PinC, LOW);
myTimer = millis(); // store snapshot of time
mysequenceStep = sm_waitWithB_On;
break; // IMMIDIATELY jump down to END-OF-SWITCH
case sm_waitWithB_On:
if ( TimePeriodIsOver(myTimer, 250) ) {
digitalWrite(IO_PinA, LOW);
digitalWrite(IO_PinB, LOW);
digitalWrite(IO_PinC, LOW);
mysequenceStep = sm_allOff;
}
break; // IMMIDIATELY jump down to END-OF-SWITCH
} // END-OF-SWITCH
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
best regards Stefan