An example code created to help beginners with the syntax of using sequences in their code

// 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.
}
}

Welcome to the forum!
Thank you for your contribution.
it would be more valuable if you formatted the code with tags, as defined by the forum guidelines.

2 Likes

Isn't it enough to just provide a function CODEsequence2 for this?

Why you need so much unnecessary code?

This is a great example to follow up with a tutorial on non-blocking fide methods

Or not.

while (digitalRead(switchState2) == HIGH)

Maybe you meant switchPin, or switchPin2, but those are both zero and that is not a good place for a switch on some Arduino boards.

When you publish a demo, take a few minutes to test it first.

a7

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.