Arduino不执行来自串行的命令而执行命令的速度慢

时间:2016-08-06 18:05:37

标签: arduino rgb arduino-uno led arduino-ide

我有一段时间没有上堆,所以我为此做了一次极端分析,社区一直帮助我,并且真的要首先感谢所有人。

我遇到了一个arduino项目的问题我试图让arduino make rgb灯在用户输入时以不同的颜色亮起。这听起来很基本但不幸的是,arduino在某些情况下不会这样做,或者从用户接收输入的速度很慢。

我无法弄清楚代码或arduino是否存在问题。

以下是我认为问题的代码,当尝试此菜单命令执行一次但不会丢弃循环并将我扔回主菜单。

请注意,代码实际上非常长,所以这只是它的一小部分,完整的代码如下所示。

(FIY ARDUINO UNO R3 ATMEGA 328)

//Party  
  if(mode == "Party" || mode == "party" || mode == "Party mode" || mode == "party mode" || mode == "Party Mode" || mode == "party Mode" || mode == "c"){
    Serial.println("Party mode");
    Serial.println("For selecting another mode and go back please type back");
  int y;

  int w;
  Serial.println("How many times? note: 1 time = 20 seconds");
while(Serial.available()==0){
  direct = Serial.readString();

  if(direct == "Back" || direct == "back" ){
    break;
  }

  y = 0;
  w = Serial.parseInt();

 if(y <= w){ 

for(int i = 0; i < w; i = i+1){
  Serial.println("Initializing");
  Serial.println(w);

  for(int fadeValue = 0; fadeValue <= 250; fadeValue +=50){
   analogWrite(ledRR1, fadeValue);
   analogWrite(ledBR1, fadeValue);
   delay(500);
   analogWrite(ledGR1, fadeValue);
   analogWrite(ledGL1,fadeValue);
   analogWrite(ledBL1, 0);
   analogWrite(ledRR1,0);
   analogWrite(ledGL2,0);
   analogWrite(ledRL2,0);
   analogWrite(ledBL2,0); 
   analogWrite(ledGR2,0);
   analogWrite(ledRR2,0);
   analogWrite(ledBR2,0);


    delay(500);
  }
  analogWrite(ledRR1, 250);
  analogWrite(ledGR2, 250);
     analogWrite(ledBR1, 0);
     analogWrite(ledGR1, 0);
  analogWrite(ledBL1, 0);
     analogWrite(ledRL1,0);
   analogWrite(ledGL2,0);
   analogWrite(ledRL2,0);
   analogWrite(ledBL2,0); 
   analogWrite(ledGR2,0);
   analogWrite(ledRR2,0);
   analogWrite(ledBR2,0);
  delay(1000);
  analogWrite(ledBR1, 250); 
  analogWrite(ledRL1, 250);
  delay(500);
  analogWrite(ledGR1, 250);
  analogWrite(ledRR1, 250);
  analogWrite(ledBL1, 250);
  analogWrite(ledGL1, 150);
  analogWrite(ledRL1, 50);
  delay(1000);
      analogWrite(ledGR1, 0);
  analogWrite(ledRR1, 0);
  analogWrite(ledBL1, 0);
  analogWrite(ledGL1, 0);
  analogWrite(ledRL1, 0);
  delay(500);
    analogWrite(ledGR1, 250);
  analogWrite(ledRR1, 250);
  analogWrite(ledBL1, 250);
  analogWrite(ledGL1, 150);
  analogWrite(ledRL1, 50);
  delay(500);
      analogWrite(ledGR1, 0);
  analogWrite(ledRR1, 0);
  analogWrite(ledBL1, 0);
  analogWrite(ledGL1, 0);
  analogWrite(ledRL1, 0);
  delay(1000);
    analogWrite(ledGR1, 250);
  analogWrite(ledRR1, 250);
  analogWrite(ledBL1, 250);
  analogWrite(ledGL1, 150);
  analogWrite(ledRL1, 50);
  delay(2000);

  analogWrite(ledGL1,250);
  analogWrite(ledGR1, 250);
  delay(1000);
    analogWrite(ledGL1,0);
  analogWrite(ledGR1, 0);
  delay(500);
    analogWrite(ledRR1,250);
  analogWrite(ledRL1, 250);
  delay(1000);
    analogWrite(ledRR1,0);
  analogWrite(ledRL1, 0);
  delay(500);
    analogWrite(ledGL1,250);
  analogWrite(ledGR1, 250);
  analogWrite(ledBR1,250);
  analogWrite(ledBL1,250);
  delay(2000);
    analogWrite(ledGL1,0);
  analogWrite(ledGR1, 0);
  delay(1000);

    analogWrite(ledRR1, 0);
  analogWrite(ledBR1, 0);
  analogWrite(ledGR1, 0);
  analogWrite(ledGL1,0);
  analogWrite(ledBL1, 0);
  analogWrite(ledRL1,0);
  analogWrite(ledGL2,0);
  analogWrite(ledRL2,0);
  analogWrite(ledBL2,0); 
  analogWrite(ledGR2,0);
  analogWrite(ledRR2,0);
  analogWrite(ledBR2,0);

  y = y +1;
}
 }
 else if(y >= w){
   Serial.println("Please type main to go back to main menu or select another value");
   break;
 }
}

  }

如上所述,代码真的很长,所以这里是所有的代码:

//List of pins and corresponding colors

//RIGHT SIDE
//Red1 5
//Green1 3
//Blue1 6

//Red4 A4
//Green4 A3
//Blue4 A5


////////////////////////////////////////////


//Left Side
//Red2 9
//Green2 11
//Blue2 10

//Red3 A1
//Green3 A0
//Blue3 A2


//Right Side
int ledRR1 = 5;
int ledGR1 = 3;
int ledBR1 = 6;

int ledRR2 = A4;
int ledGR2 = A3;
int ledBR2 = A5;

///////////////////

//Left Side
int ledRL1 = 9;
int ledGL1 = 11;
int ledBL1 = 10;

int ledRL2 = A1;
int ledGL2 = A0;
int ledBL2 = A2;

//////////////////


//Strings

String mode;
String node;
String moving;
String direct;
int x;



void setup(){

  Serial.begin(9600);

//PinMode

pinMode(ledRR1, OUTPUT);
pinMode(ledGR1, OUTPUT);
pinMode(ledBR1, OUTPUT);
pinMode(ledRR2, OUTPUT);
pinMode(ledGR2, OUTPUT);
pinMode(ledBR2, OUTPUT);
pinMode(ledRL1, OUTPUT);
pinMode(ledGL1, OUTPUT);
pinMode(ledBL1, OUTPUT);
pinMode(ledRL2, OUTPUT);
pinMode(ledGL2, OUTPUT);
pinMode(ledBL2, OUTPUT);

//START IN THIS STATE:

//Right
analogWrite(ledRR1, 0);
analogWrite(ledGR1, 0);
analogWrite(ledBR1, 0);

analogWrite(ledRR2, 0);
analogWrite(ledGR2, 0);
analogWrite(ledBR2, 0);

//Left
analogWrite(ledRL1, 0);
analogWrite(ledGL1, 0);
analogWrite(ledBL1, 0);

analogWrite(ledRL2, 0);
analogWrite(ledGL2, 0);
analogWrite(ledBL2, 0);

x = 0;


}

void loop(){

Serial.println("Main menu");

 while(Serial.available()==0){

  mode = Serial.readString();
  node = Serial.readString();
  moving = Serial.readString();
  Serial.setTimeout(50);


  while( x <= 0){

  Serial.println("Welcome to Andy Mood'O Matic light machine");
  Serial.println("");
  Serial.println("Please select the mode according to your mood");
  Serial.println("");
  Serial.println("");
  Serial.println("Commands and available modes");
  Serial.println("Commands: Main Menu or just main");
  Serial.println("");
  Serial.println("Please type modes to see the available modes");
  Serial.println("");
  Serial.println("SOMETIMES THE ARDUINO IS SLOW AND YOU HAVE TO INPUT THE COMMAND TWICE!");
  x = x+1;
  }

  if(node == "modes" || node == "Modes"){
    Serial.println("Available modes, NOTE: YOU CAN USE SIMPLE COMMANDS!");
    Serial.println("");
    Serial.println("Relax /SIMPLE COMMAND: a");
      Serial.println("   Automated Relax mode");
    Serial.println("");
    Serial.println("Intimate /SIMPLE COMMAND: b");
      Serial.println("  Automated intimate mode");
    Serial.println("");
    Serial.println("Party mode or Party /SIMPLE COMMAND: c");
      Serial.println("  This is user controlled, set the amount of time you want the lights to go for");
      Serial.println("  Please check description in party mode to see available options");
    Serial.println("");
    Serial.println("Night /SIMPLE COMMAND: d");
      Serial.println("  Automated night mode");
      Serial.println("  User controlled night mode, set how dim the lights should be and time");
    Serial.println("");
    Serial.println("Off /SIMPLE COMMAND o");
      Serial.println("   Turns everything off");


  }

  if(moving == "Main Menu" || moving == "main menu" || moving == "Main menu" || moving == "main Menu" || moving == "main menu" || moving == "main" || moving == "Main"){
    //This is to go back to main main, for each stop loop you have to add this to repeat the println
  x = x-1;
  }




//Relax
  if(mode == "Relax" || mode == "relax" || mode == "a" ){
    Serial.println("This is automatic relax mode");

   for(int fadeValue = 0; fadeValue <= 250; fadeValue +=10){

    analogWrite(ledGR1,fadeValue);
    analogWrite(ledGL1, fadeValue);
    analogWrite(ledBL1, 250);
    analogWrite(ledBR1, 250);
    analogWrite(ledGR2, fadeValue);
    analogWrite(ledGL2,fadeValue);
    analogWrite(ledBR2,fadeValue); 
    analogWrite(ledBL2,fadeValue); 

    delay(60000);
  }


  for (int fadeValue = 250; fadeValue >= 0; fadeValue -= 10){
    // sets the value (range from 0 to 255):
    analogWrite(ledBL1, 250);
    analogWrite(ledBR1, 250);
    analogWrite(ledGR2,fadeValue); 
    analogWrite(ledGL2, fadeValue); 
    analogWrite(ledGR1, fadeValue);
    analogWrite(ledGL1,fadeValue);
    analogWrite(ledBL2,fadeValue); 
    analogWrite(ledBR2,fadeValue); 

    // wait for 30 milliseconds to see the dimming effect
    delay(60000);
  }



   for(int fadeValue = 0; fadeValue <= 250; fadeValue +=10){

    analogWrite(ledGR2,fadeValue);
    analogWrite(ledGL2, 0); 
    analogWrite(ledBL1, 250);
    analogWrite(ledBR1, 250);
    analogWrite(ledGR1, fadeValue);
    analogWrite(ledGL1, 0);
    analogWrite(ledBR2,fadeValue); 
    analogWrite(ledBL2,fadeValue); 

    delay(60000);
  }


  for (int fadeValue = 250; fadeValue >= 0; fadeValue -= 10){
    // sets the value (range from 0 to 255):
    analogWrite(ledBR1, 250);
    analogWrite(ledBL1, 250);
    analogWrite(ledGR2,fadeValue); 
    analogWrite(ledGR1, 0); 
    analogWrite(ledGR2, fadeValue);
    analogWrite(ledGL2, 0);
    analogWrite(ledBL2,fadeValue); 
    analogWrite(ledBR2,fadeValue); 

    // wait for 30 milliseconds to see the dimming effect
    delay(60000);
  }


  analogWrite(ledGL1, 0);
  analogWrite(ledGR1,0);
  analogWrite(ledGR2,0);
  analogWrite(ledGL2,0);
  analogWrite(ledBL1, 250);
  analogWrite(ledBR1, 250);
  analogWrite(ledBL2,0); 
  analogWrite(ledBR2,0); 
}



//Off 
  if(mode == "Off" || mode == "o" || mode == "off"){
    Serial.println("Everything off");

      analogWrite(ledRR1, 0);
      analogWrite(ledGR1, 0);
      analogWrite(ledBR1, 0);

      analogWrite(ledRR2, 0);
      analogWrite(ledGR2, 0);
      analogWrite(ledBR2, 0);


      analogWrite(ledRL1, 0);
      analogWrite(ledGL1, 0);
      analogWrite(ledBL1, 0);

      analogWrite(ledRL2, 0);
      analogWrite(ledGL2, 0);
      analogWrite(ledBL2, 0); 

  }


//Intimate  
  if(mode == "intimate" || mode == "Intimate" || mode == "b"){
    Serial.println("Automatic Intimate mode");
    for(int fadeValue = 0; fadeValue <= 150; fadeValue +=50){

    analogWrite(ledRR1,250);
    analogWrite(ledRL1, 250);
    analogWrite(ledBR1, fadeValue);
    analogWrite(ledBL1, fadeValue);
    analogWrite(ledRL2, fadeValue);
    analogWrite(ledGR1,fadeValue);
    analogWrite(ledBL2,fadeValue);
    analogWrite(ledRR2,fadeValue);
    analogWrite(ledBR2,fadeValue);

    delay(60000);
  }

  for (int fadeValue = 250; fadeValue >= 0; fadeValue -= 50){
    // sets the value (range from 0 to 255):
    analogWrite(ledRR1,250);
    analogWrite(ledRL1, 250);
    analogWrite(ledBL1, fadeValue);
    analogWrite(ledBR1, 250);
    analogWrite(ledRL2, fadeValue);
    analogWrite(ledGR1,fadeValue);
    analogWrite(ledBL2, 250);
    analogWrite(ledRR2,fadeValue);
    analogWrite(ledBR2,150);

    // wait for 30 milliseconds to see the dimming effect
    delay(60000);
  }

      for (int fadeValue = 250; fadeValue >= 150; fadeValue -= 50){
    // sets the value (range from 0 to 255):
    analogWrite(ledRR1, 250);
    analogWrite(ledRL1, 250);
    analogWrite(ledBL1, 0);
    analogWrite(ledBR1, 0);
    analogWrite(ledRL2, 0);
    analogWrite(ledGR1,0);
    analogWrite(ledBL2,fadeValue);
    analogWrite(ledRR2,0);
    analogWrite(ledBR2, 150);
    delay(60000);
      }

    for (int fadeValue = 150; fadeValue >= 0; fadeValue -= 50){
    // sets the value (range from 0 to 255):
    analogWrite(ledRR1,250);
    analogWrite(ledRL1, 250);
    analogWrite(ledBL1, 0);
    analogWrite(ledBR1, 0);
    analogWrite(ledRL2, 0);
    analogWrite(ledGR1,0);
    analogWrite(ledBL2,fadeValue);
    analogWrite(ledRR2,0);
    analogWrite(ledBR2,fadeValue);
    delay(60000);
    }

  for (int fadeValue = 250; fadeValue >= 0; fadeValue -= 50){
    // sets the value (range from 0 to 255):
    analogWrite(ledRR1,fadeValue);
    analogWrite(ledRL1, fadeValue);
    analogWrite(ledBL1, 0);
    analogWrite(ledBR1, 0);
    analogWrite(ledRL2, 0);
    analogWrite(ledGR1,0);
    analogWrite(ledBL2,0);
    analogWrite(ledRR2,0);
    analogWrite(ledBR2,0);

    // wait for 30 milliseconds to see the dimming effect
    delay(60000);
  }

  for(int fadeValue = 0; fadeValue <= 150; fadeValue +=50){

    analogWrite(ledRR1,fadeValue);
    analogWrite(ledRL1, fadeValue);
    analogWrite(ledBL1, fadeValue);
    analogWrite(ledBR1, fadeValue);
    analogWrite(ledRL2, fadeValue);
    analogWrite(ledGR1, 0);
    analogWrite(ledBL2,fadeValue);
    analogWrite(ledRR2,fadeValue);
    analogWrite(ledBR2,fadeValue);

    delay(60000);
  }
    for(int fadeValue = 0; fadeValue <= 250; fadeValue +=50){

    analogWrite(ledRR1, fadeValue);
    analogWrite(ledRL1, fadeValue);
    analogWrite(ledBL1, 150);
    analogWrite(ledBR1, 150);
    analogWrite(ledRL2, fadeValue);
    analogWrite(ledGR1, 0);
    analogWrite(ledBL2, 150);
    analogWrite(ledRR2, fadeValue);
    analogWrite(ledBR2, 150);

    delay(60000);
  }
    analogWrite(ledRR1, 0);
    analogWrite(ledRL1, 0);
    analogWrite(ledBL1, 150);
    analogWrite(ledBR1, 150);
    analogWrite(ledRL2, 0);
    analogWrite(ledGR1, 0);
    analogWrite(ledBL2, 150);
    analogWrite(ledRR2, 0);
    analogWrite(ledBR2, 150);

 }


//Party  
  if(mode == "Party" || mode == "party" || mode == "Party mode" || mode == "party mode" || mode == "Party Mode" || mode == "party Mode" || mode == "c"){
    Serial.println("Party mode");
    Serial.println("For selecting another mode and go back please type back");
  int y;

  int w;
  Serial.println("How many times? note: 1 time = 20 seconds");
while(Serial.available()==0){
  direct = Serial.readString();

  if(direct == "Back" || direct == "back" ){
    break;
  }

  y = 0;
  w = Serial.parseInt();

 if(y <= w){ 

for(int i = 0; i < w; i = i+1){
  Serial.println("Initializing");
  Serial.println(w);

  for(int fadeValue = 0; fadeValue <= 250; fadeValue +=50){
   analogWrite(ledRR1, fadeValue);
   analogWrite(ledBR1, fadeValue);
   delay(500);
   analogWrite(ledGR1, fadeValue);
   analogWrite(ledGL1,fadeValue);
   analogWrite(ledBL1, 0);
   analogWrite(ledRR1,0);
   analogWrite(ledGL2,0);
   analogWrite(ledRL2,0);
   analogWrite(ledBL2,0); 
   analogWrite(ledGR2,0);
   analogWrite(ledRR2,0);
   analogWrite(ledBR2,0);


    delay(500);
  }
  analogWrite(ledRR1, 250);
  analogWrite(ledGR2, 250);
     analogWrite(ledBR1, 0);
     analogWrite(ledGR1, 0);
  analogWrite(ledBL1, 0);
     analogWrite(ledRL1,0);
   analogWrite(ledGL2,0);
   analogWrite(ledRL2,0);
   analogWrite(ledBL2,0); 
   analogWrite(ledGR2,0);
   analogWrite(ledRR2,0);
   analogWrite(ledBR2,0);
  delay(1000);
  analogWrite(ledBR1, 250); 
  analogWrite(ledRL1, 250);
  delay(500);
  analogWrite(ledGR1, 250);
  analogWrite(ledRR1, 250);
  analogWrite(ledBL1, 250);
  analogWrite(ledGL1, 150);
  analogWrite(ledRL1, 50);
  delay(1000);
      analogWrite(ledGR1, 0);
  analogWrite(ledRR1, 0);
  analogWrite(ledBL1, 0);
  analogWrite(ledGL1, 0);
  analogWrite(ledRL1, 0);
  delay(500);
    analogWrite(ledGR1, 250);
  analogWrite(ledRR1, 250);
  analogWrite(ledBL1, 250);
  analogWrite(ledGL1, 150);
  analogWrite(ledRL1, 50);
  delay(500);
      analogWrite(ledGR1, 0);
  analogWrite(ledRR1, 0);
  analogWrite(ledBL1, 0);
  analogWrite(ledGL1, 0);
  analogWrite(ledRL1, 0);
  delay(1000);
    analogWrite(ledGR1, 250);
  analogWrite(ledRR1, 250);
  analogWrite(ledBL1, 250);
  analogWrite(ledGL1, 150);
  analogWrite(ledRL1, 50);
  delay(2000);

  analogWrite(ledGL1,250);
  analogWrite(ledGR1, 250);
  delay(1000);
    analogWrite(ledGL1,0);
  analogWrite(ledGR1, 0);
  delay(500);
    analogWrite(ledRR1,250);
  analogWrite(ledRL1, 250);
  delay(1000);
    analogWrite(ledRR1,0);
  analogWrite(ledRL1, 0);
  delay(500);
    analogWrite(ledGL1,250);
  analogWrite(ledGR1, 250);
  analogWrite(ledBR1,250);
  analogWrite(ledBL1,250);
  delay(2000);
    analogWrite(ledGL1,0);
  analogWrite(ledGR1, 0);
  delay(1000);

    analogWrite(ledRR1, 0);
  analogWrite(ledBR1, 0);
  analogWrite(ledGR1, 0);
  analogWrite(ledGL1,0);
  analogWrite(ledBL1, 0);
  analogWrite(ledRL1,0);
  analogWrite(ledGL2,0);
  analogWrite(ledRL2,0);
  analogWrite(ledBL2,0); 
  analogWrite(ledGR2,0);
  analogWrite(ledRR2,0);
  analogWrite(ledBR2,0);

  y = y +1;
}
 }
 else if(y >= w){
   Serial.println("Please type main to go back to main menu or select another value");
   break;
 }
}

  }


//Night  
  if(mode == "Night" || mode == "night" || mode == "d"){
    Serial.println("Automatic night mode");

      analogWrite(ledRR1, 250);
      analogWrite(ledGR1, 250);
      analogWrite(ledBR1, 250);

      analogWrite(ledRR2, 250);
      analogWrite(ledGR2, 250);
      analogWrite(ledBR2, 250);


      analogWrite(ledRL1, 250);
      analogWrite(ledGL1, 250);
      analogWrite(ledBL1, 250);

      analogWrite(ledRL2, 250);
      analogWrite(ledGL2, 250);
      analogWrite(ledBL2, 250); 
      delay(3600000);


  for(int fadeValue = 250; fadeValue >= 0; fadeValue -=50){

     analogWrite(ledRR1, fadeValue);
      analogWrite(ledGR1, fadeValue);
      analogWrite(ledBR1, fadeValue);

      analogWrite(ledRR2, fadeValue);
      analogWrite(ledGR2, fadeValue);
      analogWrite(ledBR2, fadeValue);


      analogWrite(ledRL1, fadeValue);
      analogWrite(ledGL1, fadeValue);
      analogWrite(ledBL1, fadeValue);

      analogWrite(ledRL2, fadeValue);
      analogWrite(ledGL2, fadeValue);
      analogWrite(ledBL2, fadeValue);

      delay(120000);
     Serial.println("Good Night");


  }
        analogWrite(ledRR1, 0);
      analogWrite(ledGR1, 0);
      analogWrite(ledBR1, 0);

      analogWrite(ledRR2, 0);
      analogWrite(ledGR2, 0);
      analogWrite(ledBR2, 0);


      analogWrite(ledRL1, 0);
      analogWrite(ledGL1, 0);
      analogWrite(ledBL1, 0);

      analogWrite(ledRL2, 0);
      analogWrite(ledGL2, 0);
      analogWrite(ledBL2, 0); 
   break;   
  }

}
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我对你的代码非常慢(对于usart命令)并不感到惊讶。那里有很多延误。每次通过循环都需要短暂的延迟。例如,如果您想要快速响应,请使用状态机。

一些问题:

  • 格式错误

  • while (Serial.available() == 0)非常奇怪。如果有可用的数据,它将始终跳过整个循环。

  • 评论:

        // wait for 30 milliseconds to see the dimming effect
        delay(60000);
    

    不是30ms而是60000ms(60s)

  • Serial.print的许多常量字符串。您可以使用F(&#34;某些flash字符串&#34;)帮助程序将这些字符串仅存储在闪存中(否则它必须复制到RAM中并从中显示)

如果你在&#34;小时&#34;中检查和处理命令。 (比喻说),很快就会很快&#34;。

可以使用这样的东西(它只是简短的草图):

// prototypes:
uint32_t  main_menu_fsm(byte & state);
uint32_t  mode_menu_fsm(byte & state);
uint32_t   off_mode_fsm(byte & state);
uint32_t relax_mode_fsm(byte & state);

// global variables for FSM
byte                         glob_state = 0;
uint32_t                     next_check = 0;
uint32_t (*state_machine)(byte & state) = &main_menu_fsm;

#define SET_FSM(fsm) Serial.println("Setting: " #fsm); glob_state=0; next_check=0; state_machine = &fsm


// global variables for Serial processing
String inputString = "";         // a string to hold incoming data

// RGBLed class
template <byte PinR, byte PinG, byte PinB>
class RGBLed {
  public:
    void begin(byte r = 0, byte g = 0, byte b = 0) {
      pinMode(PinR, OUTPUT);
      pinMode(PinG, OUTPUT);
      pinMode(PinB, OUTPUT);
      set(r,g,b);
    }

    void set(byte r = 0, byte g = 0, byte b = 0) {
      analogWrite(PinR, r);
      analogWrite(PinG, g);
      analogWrite(PinB, b);
    }
};

RGBLed<9,10,11>  Led1;

void setup() {
  Serial.begin(57600);
  Led1.begin(10,10,10);
  inputString.reserve(50); // should be enough for all commands


}

void loop() {
  uint32_t curr = millis();
  if (curr > next_check) {
    next_check = curr + state_machine(glob_state);
/*  // debug:
    Serial.print(curr);
    Serial.print(" "); 
    Serial.print(next_check);
    Serial.print(" ");
    Serial.println(glob_state);
*/
  }
}

void serialEvent() {
  while (Serial.available()) {
    char ch = (char)Serial.read();
    if (ch == '\r' || ch == '\n') {
      if (inputString.length()>0) {
        inputString.toLowerCase();
        Serial.println(inputString);

        if (inputString.length()==1) {
          switch (inputString[0]) {
            case 'a': SET_FSM(relax_mode_fsm); break;
            case 'o': SET_FSM(off_mode_fsm);   break;
          }
        } else if (inputString.startsWith("main")) {
          SET_FSM(main_menu_fsm);
        } else if (inputString.startsWith("mode")) {
          SET_FSM(mode_menu_fsm);
        }
        inputString = ""; // reset string
        break; // process command - interrupt processing
      }
    } else {
      inputString += ch;
    }
  }
}

uint32_t  main_menu_fsm(byte & state) {
  if (state == 0) {
    Serial.println(F("Welcome to Andy Mood'O Matic light machine"));
    Serial.println(F(""));
    Serial.println(F("Please select the mode according to your mood"));
    Serial.println(F(""));
    Serial.println(F(""));
    Serial.println(F("Commands and available modes"));
    Serial.println(F("Commands: Main Menu or just main"));
    Serial.println(F(""));
    Serial.println(F("Please type modes to see the available modes"));

    ++state;
  }
  return 5000UL;
}

uint32_t  mode_menu_fsm(byte & state) {
  if (state == 0) {
    Serial.println(F("Available modes, NOTE: YOU CAN USE SIMPLE COMMANDS!"));
    Serial.println(F(""));
    Serial.println(F("Relax or a [works only 'a' and just brief implementation]"));
      Serial.println(F("   Automated Relax mode"));
    Serial.println(F(""));
    Serial.println(F("Intimate or b [not implemented]"));
      Serial.println(F("  Automated intimate mode"));
    Serial.println(F(""));
    Serial.println(F("Party mode or Party or c [not implemented]"));
      Serial.println(F("  This is user controlled, set the amount of time you want the lights to go for"));
      Serial.println(F("  Please check description in party mode to see available options"));
    Serial.println(F(""));
    Serial.println(F("Night or d"));
      Serial.println(F("  Automated night mode [not implemented]"));
      Serial.println(F("  User controlled night mode, set how dim the lights should be and time"));
    Serial.println(F(""));
    Serial.println(F("Off or o [works only 'o']"));
      Serial.println(F("   Turns everything off"));

    ++state;
  }
  return 5000UL;
}

uint32_t   off_mode_fsm(byte & state) {
  if (state == 0) {
    Serial.println(F("Entering Off Mode"));
    Led1.set(0,0,0);
    ++state;
  }
  return 5000UL;
}

uint32_t relax_mode_fsm(byte & state) {
  if (state == 0) {
    Serial.println(F("Entering Off Mode"));
  }

  if (++state == 8) state = 1;

  Led1.set((1 & state) * 255, (1 & (state >> 1)) * 128, (1 & (state >> 2)) * 128);

  return 2000UL;
}