Using Serial imput to run a code

I am writing a code that will sort M&M's. I am trying to get it so that after it runs through the initialization process, it will require me to type in a start command in the serial monitor. If it is incorrect, the serial monitor will show the command and say " - Wait 10 Seconds" and after ten seconds it will show "Accepting new imput". The problem is, whatever I try, it will not accept my start command.

Here is a copy of my code:

#include <LiquidCrystal.h>

const int s0 = 4;
const int s1 = 5;
const int s2 = 6;
const int s3 = 7;
const int out = 3;

int red = 0;
int green = 0;
int blue = 0;

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;

LiquidCrystal lcd(8, 9, 10, 11, 12, 13 );

void setup() {
  Serial.begin(9600);
  Serial.flush();
  delay(2);
  Serial.println("<Initializing...>");
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("<Initializing..>");
  lcd.setCursor(0, 1);
  lcd.print("M&M Sorter - DH");
  delay(1000);
  Serial.println("M&M Sorter - D.H.");
  lcd.clear();
  delay(95);
  Serial.println("- Awaiting Start Commmand Prompt -");
  delay(2);
}

void loop() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  if (Serial.available() > 0) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
    if (newData == true) {
      Serial.println(receivedChars);
      if (receivedChars == 'start') {
        color();
        Serial.print(" R:  ");
        Serial.print(red, DEC);
        Serial.print("   | G:  ");
        Serial.print(green, DEC);
        Serial.print("   | B:  ");
        Serial.print(blue, DEC);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("R:");
        lcd.print(red, DEC);
        lcd.print("  G:");
        lcd.print(green, DEC);
        lcd.print("  B:");
        lcd.print(blue, DEC);
        lcd.setCursor(0, 1);


        if (red >= 17 && red <= 23 && green >= 23 && green <= 28 && blue >= 23 && blue <= 28)  {
          Serial.println(" - (Yellow)");
          lcd.print(" - (Yellow)");
        }

        else if (red >= 23 && red <= 30 && green >= 38 && green <= 49 && blue >= 26 && blue <= 34)  {
          Serial.println(" - (Red)");
          lcd.print(" - (Red)");
        }

        else if (red >= 30 && red <= 40 && green >= 27 && green <= 39 && blue >= 25 && blue <= 31)  {
          Serial.println(" - (Green)");
          lcd.print(" - (Green)");
        }

        else if (red >= 38 && red <= 45 && green >= 32 && green <= 42 && blue >= 17 && blue <= 26)  {
          Serial.println(" - (Blue)");
          lcd.print(" - (Blue)");
        }

        else if (red >= 18 && red <= 22 && green >= 36 && green <= 47 && blue >= 28 && blue <= 34)  {
          Serial.println(" - (Orange)");
          lcd.print(" - (Orange)");
        }

        else if (red >= 31 && red <= 39 && green >= 40 && green <= 48 && blue >= 28 && blue <= 35)  {
          Serial.println(" - (Brown)");
          lcd.print(" - (Brown)");
        }

        else {
          Serial.println("  - (No Color)");
          lcd.print(" - (No Color)");
        }
        delay(500);
      }
      else if (receivedChars != 'start') {
        Serial.println("<INNCORRECT> - Wait 10 Seconds");
        delay(10000);
        Serial.println("Accepting new imput");
        delay(10);
      }
      //else {
        //Serial.println("no imput");
        //delay(100);
      //}
      newData = false;
    }
  }
}

void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}

If anyone has any idea what I am doing wrong that would be great.

Thanks, DH

No matter what i type, it always responds with " - Wait 10 Seconds". I am not sure why it will not run the code.

What exactly are you doing with static byte ndx = 0; ?

to be perfectly honest, i am not quite sure :frowning:

I beleive it has something to do with weather or not the arduino has received any new serial info.

Move ndx outside of loop( ) and remove the static.

I probably sound like a noob, but I am confused what you want me to do.

Thanks for being so willing to help though!

Read serial input basics (again). Keep the functions as they are.

You can not compare receivedChars the way you do; use strcmp

  Serial.begin(9600);
  Serial.flush();

As soon as you open the door, wait for all the stuff you've lined up by the door to be taken away.

Pray, tell, where have you lined up anything by the door?

      Serial.println(receivedChars);

Anonymous printing sucks. Not sharing the output sucks even more.

How are you sending the serial data? If you are using the Serial Monitor application, do you have it set to send carriage returns and/pr line feeds?

      if (receivedChars == 'start') {

Single quotes are for single characters. Please post a picture of your keyboard with the start key circled.

That is NOT how to compare strings. You failed to read ALL of Robin2's post, where he does NOT make that mistake.

strcmp() IS how to compare strings.

sterretje:
Read serial input basics (again). Keep the functions as they are.

You can not compare receivedChars the way you do; use strcmp

+1

When using Cstrings you must use strcmp() to compare values rather than ==

...R
Serial Input Basics - simple reliable ways to receive data.

I am sorry, but i cannot figure out how to use strcmp(). I am lost if not working in the Arduino language. I re-read through all of Robin2's posts on it but i just dont understand it. I havent done much serial monitor work in the past.

Thanks for the effort guys

strcmp() is not "serial monitor work". It is C language.
Learn it.

Ok, so I changed my code to read this:

#include <LiquidCrystal.h>
#include <stdio.h>
#include <string.h>
const int s0 = 4;
const int s1 = 5;
const int s2 = 6;
const int s3 = 7;
const int out = 3;

int red = 0;
int green = 0;
int blue = 0;

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;
char key[] = "start";
char buffer[80];

char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

LiquidCrystal lcd(8, 9, 10, 11, 12, 13 );

void setup() {
  Serial.begin(9600);
  Serial.flush();
  delay(2);
  Serial.println("<Initializing...>");
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("<Initializing..>");
  lcd.setCursor(0, 1);
  lcd.print("M&M Sorter - DH");
  delay(1000);
  Serial.println("M&M Sorter - D.H.");
  lcd.clear();
  delay(95);
  Serial.println("- Awaiting Start Commmand Prompt -");
  delay(2);
}

void loop() {
  recvWithEndMarker();
  if (newData == true) {
    strcpy(tempChars, receivedChars); //Example 5 Serial Input Basics-Robin2
    // this temporary copy is necessary to protect the original data
    // because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    newData = false;
    //Serial.println(receivedChars);
    if (strcmp (key, buffer) != 0) {
      Serial.println("M&M Sorter - Start!");
      delay(100);
      for (;;) {
        color();
        Serial.print(" R:  ");
        Serial.print(red, DEC);
        Serial.print("   | G:  ");
        Serial.print(green, DEC);
        Serial.print("   | B:  ");
        Serial.print(blue, DEC);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("R:");
        lcd.print(red, DEC);
        lcd.print("  G:");
        lcd.print(green, DEC);
        lcd.print("  B:");
        lcd.print(blue, DEC);
        lcd.setCursor(0, 1);


        if (red >= 17 && red <= 23 && green >= 23 && green <= 28 && blue >= 23 && blue <= 28)  {
          Serial.println(" - (Yellow)");
          lcd.print(" - (Yellow)");
        }

        else if (red >= 23 && red <= 30 && green >= 38 && green <= 49 && blue >= 26 && blue <= 34)  {
          Serial.println(" - (Red)");
          lcd.print(" - (Red)");
        }

        else if (red >= 30 && red <= 40 && green >= 27 && green <= 39 && blue >= 25 && blue <= 31)  {
          Serial.println(" - (Green)");
          lcd.print(" - (Green)");
        }

        else if (red >= 38 && red <= 45 && green >= 32 && green <= 42 && blue >= 17 && blue <= 26)  {
          Serial.println(" - (Blue)");
          lcd.print(" - (Blue)");
        }

        else if (red >= 18 && red <= 22 && green >= 36 && green <= 47 && blue >= 28 && blue <= 34)  {
          Serial.println(" - (Orange)");
          lcd.print(" - (Orange)");
        }

        else if (red >= 31 && red <= 39 && green >= 40 && green <= 48 && blue >= 28 && blue <= 35)  {
          Serial.println(" - (Brown)");
          lcd.print(" - (Brown)");
        }

        else {
          Serial.println("  - (No Color)");
          lcd.print(" - (No Color)");
        }
        delay(500);
      }
    }
    else if (receivedChars != 'start') {
      Serial.println("<INNCORRECT> - Wait 10 Seconds");
      delay(10000);
      Serial.println("Accepting new imput");
      delay(10);
    }
  }
  newData = false;
}
//========================================================
void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
//========================================================
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}
//=========================================================
void showNewData() {
  if (newData == true) {
    Serial.println(receivedChars);
    newData = false;
  }
}
//========================================================
void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

//=========================================================

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  floatFromPC = atof(strtokIndx);     // convert this part to a float

}

//========================================================

void showParsedData() {
  //Serial.print("Message ");
  Serial.println(messageFromPC);
  //Serial.print("Integer ");
  //Serial.println(integerFromPC);
  //Serial.print("Float ");
  //Serial.println(floatFromPC);
}

Now it allways runs the code. What did I do wrong? I am hoping to have it run when i type "start" into the serial monitor.

Thanks for all the help guys, I just figured it out.
As of the code above, I just had to switch buffer with recievedChars and != with ==.
Thanks again, I coudnt of done it without the help:)

Great.

For your information:

The below is still wrong :wink:

    else if (receivedChars != 'start') {

If you set compiler warnings to All in File -> preferences, you get the below in orange

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_628303\sketch_apr06a.ino:122:31: warning: character constant too long for its type

     else if (receivedChars != 'start') {

                               ^

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_628303\sketch_apr06a.ino: In function 'void loop()':

C:\Users\sterretje\AppData\Local\Temp\arduino_modified_sketch_628303\sketch_apr06a.ino:122:31: warning: ISO C++ forbids comparison between pointer and integer [-fpermissive]

For your project, you can replace else if (receivedChars != 'start') { by a simple else {.

Further, showParsedData() (or showNewData()) is intended to be the function where you can do all your work. You can move the code that you now added to loop() in there and rename it to e.g. runMMSorter() which reflects what the function actually does.

Your code would then look like (including some cleanup of functions that you don't use

#include <LiquidCrystal.h>

const int s0 = 4;
const int s1 = 5;
const int s2 = 6;
const int s3 = 7;
const int out = 3;

int red = 0;
int green = 0;
int blue = 0;

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;
char key[] = "start";
char buffer[80];

char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

LiquidCrystal lcd(8, 9, 10, 11, 12, 13 );

void setup() {
  Serial.begin(9600);
  Serial.flush();
  delay(2);
  Serial.println("<Initializing...>");
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("<Initializing..>");
  lcd.setCursor(0, 1);
  lcd.print("M&M Sorter - DH");
  delay(1000);
  Serial.println("M&M Sorter - D.H.");
  lcd.clear();
  delay(95);
  Serial.println("- Awaiting Start Commmand Prompt -");
  delay(2);
}

void loop() {
  recvWithEndMarker();
  if (newData == true) {
    strcpy(tempChars, receivedChars); //Example 5 Serial Input Basics-Robin2
    // this temporary copy is necessary to protect the original data
    // because strtok() used in parseData() replaces the commas with \0
    parseData();
    runMMSorter();

    newData = false;
  }
}
//========================================================
void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
//========================================================
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

//=========================================================

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  floatFromPC = atof(strtokIndx);     // convert this part to a float

}

//========================================================

void runMMSorter() {
  Serial.println(messageFromPC);


  newData = false;
  //Serial.println(receivedChars);
  if (strcmp (key, buffer) != 0) {
    Serial.println("M&M Sorter - Start!");
    delay(100);
    for (;;) {
      color();
      Serial.print(" R:  ");
      Serial.print(red, DEC);
      Serial.print("   | G:  ");
      Serial.print(green, DEC);
      Serial.print("   | B:  ");
      Serial.print(blue, DEC);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("R:");
      lcd.print(red, DEC);
      lcd.print("  G:");
      lcd.print(green, DEC);
      lcd.print("  B:");
      lcd.print(blue, DEC);
      lcd.setCursor(0, 1);


      if (red >= 17 && red <= 23 && green >= 23 && green <= 28 && blue >= 23 && blue <= 28)  {
        Serial.println(" - (Yellow)");
        lcd.print(" - (Yellow)");
      }

      else if (red >= 23 && red <= 30 && green >= 38 && green <= 49 && blue >= 26 && blue <= 34)  {
        Serial.println(" - (Red)");
        lcd.print(" - (Red)");
      }

      else if (red >= 30 && red <= 40 && green >= 27 && green <= 39 && blue >= 25 && blue <= 31)  {
        Serial.println(" - (Green)");
        lcd.print(" - (Green)");
      }

      else if (red >= 38 && red <= 45 && green >= 32 && green <= 42 && blue >= 17 && blue <= 26)  {
        Serial.println(" - (Blue)");
        lcd.print(" - (Blue)");
      }

      else if (red >= 18 && red <= 22 && green >= 36 && green <= 47 && blue >= 28 && blue <= 34)  {
        Serial.println(" - (Orange)");
        lcd.print(" - (Orange)");
      }

      else if (red >= 31 && red <= 39 && green >= 40 && green <= 48 && blue >= 28 && blue <= 35)  {
        Serial.println(" - (Brown)");
        lcd.print(" - (Brown)");
      }

      else {
        Serial.println("  - (No Color)");
        lcd.print(" - (No Color)");
      }
      delay(500);
    }
  }
  else {
    Serial.println("<INNCORRECT> - Wait 10 Seconds");
    delay(10000);
    Serial.println("Accepting new imput");
    delay(10);
  }
}

Lastly, you might never have a need for it but consider what you have to modify if you ever want to add a stop command. Your code is blocking due to the
* *for(;;);* *
(so will never ever read the serial port again and all the delays will make it non-responsive. But that might be an exercise for another day :wink:

Thanks for doing that, I had already been working of something like that. One problem I was having was that I could no longer use it away from my laptop because there was no serial monitor to start it. So what I did was add a way to start it by pushing a button. However, it is not working how I wanted it to. It simply runs it every time without waiting for an imput by either the serial monitor or the button. Do you have any idea what I might be doing wrong? I will post my code below.

#include <LiquidCrystal.h>
#include <stdio.h>
#include <string.h>

const int s0 = 4;
const int s1 = 5;
const int s2 = 6;
const int s3 = 7;
const int out = 3;
const int buttonPin = 2;

int red = 0;
int green = 0;
int blue = 0;

int buttonState;    // variables to hold the pushbutton states
int progState = 0;  // progState is used as a state variable

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;
char key[] = "start";
char buffer[80];

char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

LiquidCrystal lcd(8, 9, 10, 11, 12, 13 );
//===========================================================
void setup() {
  Serial.begin(9600);
  delay(2);
  Serial.flush();
  delay(2);
  Serial.println("<Initializing...>");
  pinMode(buttonPin, INPUT);
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("<Initializing..>");
  lcd.setCursor(0, 1);
  lcd.print("M&M Sorter - DH");
  delay(1000);
  Serial.println("M&M Sorter - D.H.");
  lcd.clear();
  delay(95);
  Serial.println("- Awaiting Start Commmand Prompt -");
  delay(2);
}
//===========================================================
void loop() {
  buttonState = digitalRead(buttonPin);
  recvWithEndMarker();
  if (newData == true) {
    strcpy(tempChars, receivedChars); //Example 5 Serial Input Basics-Robin2
    // this temporary copy is necessary to protect the original data
    // because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    newData = false;
    if (strcmp (key, receivedChars) == 0) {
      mandmStart();
      for (;;) {
        color();
        mandmColor();
      }
    }
    else {
      Serial.println("<INNCORRECT> - Wait 10 Seconds");
      delay(10000);
      Serial.println("Accepting new imput");
      delay(10);
    }
  }
  else if (buttonState == HIGH) {
    mandmStart();
    for (;;) {
      color();
      mandmColor();
    }
    newData = false;
  }
}
//===========================================================
void mandmStart() {
  Serial.println("M&M Sorter - Start!");
  lcd.setCursor(0, 0);
  lcd.print("M&M Sorter");
  lcd.setCursor(0, 1);
  lcd.print("- Start!");
  delay(500);
  lcd.clear();
  delay(2);
}
//===========================================================
void mandmColor() {
  Serial.print(" R:  ");
  Serial.print(red, DEC);
  Serial.print("   | G:  ");
  Serial.print(green, DEC);
  Serial.print("   | B:  ");
  Serial.print(blue, DEC);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("R:");
  lcd.print(red, DEC);
  lcd.print("  G:");
  lcd.print(green, DEC);
  lcd.print("  B:");
  lcd.print(blue, DEC);
  lcd.setCursor(0, 1);

  if (red >= 17 && red <= 23 && green >= 23 && green <= 28 && blue >= 23 && blue <= 28)  {
    Serial.println("  - (Yellow)");
    lcd.print(" - (Yellow)");
  }

  else if (red >= 23 && red <= 30 && green >= 38 && green <= 49 && blue >= 26 && blue <= 34)  {
    Serial.println("  - (Red)");
    lcd.print(" - (Red)");
  }

  else if (red >= 30 && red <= 40 && green >= 27 && green <= 39 && blue >= 25 && blue <= 31)  {
    Serial.println("  - (Green)");
    lcd.print(" - (Green)");
  }

  else if (red >= 38 && red <= 45 && green >= 32 && green <= 42 && blue >= 17 && blue <= 26)  {
    Serial.println("  - (Blue)");
    lcd.print(" - (Blue)");
  }

  else if (red >= 18 && red <= 22 && green >= 36 && green <= 47 && blue >= 28 && blue <= 34)  {
    Serial.println("  - (Orange)");
    lcd.print(" - (Orange)");
  }

  else if (red >= 31 && red <= 39 && green >= 40 && green <= 48 && blue >= 28 && blue <= 35)  {
    Serial.println("  - (Brown)");
    lcd.print(" - (Brown)");
  }

  else {
    Serial.println("  - (No Color)");
    lcd.print(" - (No Color)");
  }
  delay(500);
}
//===========================================================
void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
//===========================================================
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}
//===========================================================
void showNewData() {
  if (newData == true) {
    Serial.println(receivedChars);
    newData = false;
  }
}
//===========================================================
void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}
//===========================================================
void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  floatFromPC = atof(strtokIndx);     // convert this part to a float
}
//===========================================================
void showParsedData() {
  //Serial.print("Message ");
  Serial.println(messageFromPC);
  //Serial.print("Integer ");
  //Serial.println(integerFromPC);
  //Serial.print("Float ");
  //Serial.println(floatFromPC);
}

Thanks for the imput, I made some changes based on it!

DH12043:
Thanks for the imput, I made some changes based on it!

Does it work now? If not, post your latest code.

Does it work now? If not, post your latest code.

The serial monitor works for starting it. However, now that I have added the button to start it as an alternaltive, it is glitchy. It will randomly start it without me doing anything, sometimes it works, sometimes it doesent.

This is a copy of my current code:

#include <LiquidCrystal.h>
#include <stdio.h>
#include <string.h>

const int s0 = 4;
const int s1 = 5;
const int s2 = 6;
const int s3 = 7;
const int out = 3;
const int buttonPin = 2;

int red = 0;
int green = 0;
int blue = 0;

int buttonState;    // variables to hold the pushbutton states
int progState = 0;  // progState is used as a state variable

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;
char key[] = "start";
char buffer[80];

char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

LiquidCrystal lcd(8, 9, 10, 11, 12, 13 );
//===========================================================
void setup() {
  Serial.begin(9600);
  delay(2);
  Serial.flush();
  delay(2);
  Serial.println("<Initializing...>");
  pinMode(buttonPin, INPUT);
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("<Initializing..>");
  lcd.setCursor(0, 1);
  lcd.print("M&M Sorter - DH");
  delay(1000);
  Serial.println("M&M Sorter - D.H.");
  lcd.clear();
  delay(95);
  Serial.println("- Awaiting Start Commmand Prompt -");
  delay(2);
}
//===========================================================
void loop() {
  buttonState = digitalRead(buttonPin);
  recvWithEndMarker();
  if (newData == true) {
    strcpy(tempChars, receivedChars); //Example 5 Serial Input Basics-Robin2
    // this temporary copy is necessary to protect the original data
    // because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    newData = false;
    if (strcmp (key, receivedChars) == 0) { //start command is given
      Serial.println("<Serial>");
      mandmStart();
      for (;;) {
        color();
        mandmColor();
      }
    }
    else { //if wrong command is given
      Serial.println("<INNCORRECT> - Wait 10 Seconds");
      delay(10000);
      Serial.println("Accepting new imput");
      delay(10);
    }
  }
  else if (buttonState == LOW) { //button is pressed
    Serial.println("<Button>");
    mandmStart();
    for (;;) {
      color();
      mandmColor();
    }
  }
}
//===========================================================
void mandmStart() {
  Serial.println("M&M Sorter - Start!");
  lcd.setCursor(0, 0);
  lcd.print("M&M Sorter");
  lcd.setCursor(0, 1);
  lcd.print("- Start!");
  delay(500);
  lcd.clear();
  delay(2);
}
//===========================================================
void mandmColor() {
  Serial.print(" R:  ");
  Serial.print(red, DEC);
  Serial.print("   | G:  ");
  Serial.print(green, DEC);
  Serial.print("   | B:  ");
  Serial.print(blue, DEC);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("R:");
  lcd.print(red, DEC);
  lcd.print("  G:");
  lcd.print(green, DEC);
  lcd.print("  B:");
  lcd.print(blue, DEC);
  lcd.setCursor(0, 1);

  if (red >= 17 && red <= 23 && green >= 23 && green <= 28 && blue >= 23 && blue <= 28)  {
    Serial.println("  - (Yellow)");
    lcd.print(" - (Yellow)");
  }

  else if (red >= 23 && red <= 30 && green >= 38 && green <= 49 && blue >= 26 && blue <= 34)  {
    Serial.println("  - (Red)");
    lcd.print(" - (Red)");
  }

  else if (red >= 30 && red <= 40 && green >= 27 && green <= 39 && blue >= 25 && blue <= 31)  {
    Serial.println("  - (Green)");
    lcd.print(" - (Green)");
  }

  else if (red >= 38 && red <= 45 && green >= 32 && green <= 42 && blue >= 17 && blue <= 26)  {
    Serial.println("  - (Blue)");
    lcd.print(" - (Blue)");
  }

  else if (red >= 18 && red <= 22 && green >= 36 && green <= 47 && blue >= 28 && blue <= 34)  {
    Serial.println("  - (Orange)");
    lcd.print(" - (Orange)");
  }

  else if (red >= 31 && red <= 39 && green >= 40 && green <= 48 && blue >= 28 && blue <= 35)  {
    Serial.println("  - (Brown)");
    lcd.print(" - (Brown)");
  }

  else {
    Serial.println("  - (No Color)");
    lcd.print(" - (No Color)");
  }
  delay(1000);
}
//===========================================================
void color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //count OUT, pRed, RED
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  //count OUT, pGreen, GREEN
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
//===========================================================
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}
//===========================================================
void showNewData() {
  if (newData == true) {
    Serial.println(receivedChars);
    newData = false;
  }
}
//===========================================================
void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}
//===========================================================
void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  strtokIndx = strtok(tempChars, ",");     // get the first part - the string
  strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  integerFromPC = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  floatFromPC = atof(strtokIndx);     // convert this part to a float
}
//===========================================================
void showParsedData() {
  //Serial.print("Message ");
  Serial.println(messageFromPC);
  //Serial.print("Integer ");
  //Serial.println(integerFromPC);
  //Serial.print("Float ");
  //Serial.println(floatFromPC);
}

Thanks for the help, DH

Do you use pill-up or pull-down resistors on the input pin? If not, your input is floating and will give you random readings due to noise; that can explain the behaviour that you observe.