// This code demonstrates the correct use of while statements and sequences that neaten your code.
void CODEsequence(); // Part of the setup of your sequences
void CODEsequence2();
const int switchPin = 0; // Sets the pin of your variable
int switchState = 0; // Sets the inital value of the switch
const int switchPin2 = 0;
int switchState2 = 0;
void setup() {
// put your setup code here, to run once:
pinMode(switchPin, INPUT); // Sets switchPin as an input
pinMode(switchPin2, INPUT); // Sets switchPin as an input
}
void loop() {
switchState = digitalRead(switchPin); // Outlines what the variable 'switchState' is.
switchState2 = digitalRead(switchPin2);
if (switchState == HIGH) {
// put your main code here, to run repeatedly:
CODEsequence();
CODEsequence2();
CODEsequence2();
} else if (switchState == LOW) {
// put your main code here, to run repeatedly:
CODEsequence();
CODEsequence();
CODEsequence2();
}
}
void CODEsequence(){
/*
Insert the code you want in this sequence, for example:
digitalWrite(ledPin, HIGH);
delay(2000)
digitalWrite(ledPin, LOW);
delay(2000)
*/
}
void CODEsequence2(){
while (digitalRead(switchState2) == HIGH) { // The code will only run whilst this statement is true.
//Insert the code you want in this sequence.
}
}