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