Arduino 不从串行执行命令并且执行命令很慢

Arduino does not execute command from serial and slow to execute commands

我已经有一段时间没有使用堆栈了,所以我对此深表歉意,社区一直在帮助我,真的要首先感谢所有人。

我在使用 arduino 项目时遇到问题 我正在尝试让 arduino 使 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;   
  }

}
}

谢谢!

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

一些陷阱:

  • 糟糕的格式

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

  • 评论:

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

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

  • Serial.print 的很多常量字符串。您可以使用 F("some flash string") 助手将这些字符串仅存储在闪存中(否则必须将其复制到 RAM 中并从中显示)

而且如果你在 "hour" 中检查和处理一次输入命令(打个比方),那么 "fast".

是相当困难的

可以这样用(只是简图):

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