Starting the loop after Serial.read

Hey guys, I'm just wondering if there is any way I could replace:

void loop() {
command = Serial.read();
switch(command){
case '1':
pin1();
pin2();
break;
case '0':
brightness = 0;
brightness2 = 0;
fade = 3;
fade2 = 10;
break;
}
delay(30);
}

With something that will ask for input and then on switch statement (Or any other way) start the loop. So that I don't have to use a loop in C# to write to this one every 30 MS... Thanks!

Do not do this

command = Serial.read();

until you've done this

if (Serial.available()>0){

// your stuff

}

Then loop() can be doing other stuff until a byte comes in.

Falcuun:
Hey guys, I'm just wondering if there is any way I could replace:

void loop() {
command = Serial.read();
switch(command){
case '1':
pin1();
pin2();
break;
case '0':
brightness = 0;
brightness2 = 0;
fade = 3;
fade2 = 10;
break;
}
delay(30);
}

With something that will ask for input and then on switch statement (Or any other way) start the loop. So that I don't have to use a loop in C# to write to this one every 30 MS... Thanks!

What?

If you want your task to be repetitive you must loop. Otherwise you don't have to loop. What is your project?

Falcuun:
Hey guys, I'm just wondering if there is any way I could replace:

With something that will ask for input and then on switch statement (Or any other way) start the loop. So that I don't have to use a loop in C# to write to this one every 30 MS... Thanks!

It makes no sense to start loop() after loop() is started. To delay starting loop() until an input arrives, wait for the input in the function that happens before loop(): setup()

void setup()
{
  int command;
  do
  {
    command = Serial.read();
  }
  while (command != '0' && command != '1');


  switch (command)
  {
    case '1':
      pin1();
      pin2();
      break;
      
    case '0':
      brightness = 0;
      brightness2 = 0;
      fade = 3;
      fade2 = 10;
      break;
  }
}


void loop()
{
  delay(30);
}

This is C++, not C#

DH12043:
This is C++, not C#

That is a cryptic comment

...R

Robin2:
That is a cryptic gnomic comment

AWOL:

That is a cryptic gnomic comment

Sorry, but that is even more cryptic - at least for my limited experience.

...R