#include <LiquidCrystal.h>
#include <stdio.h>
#include <string.h>
#include <Servo.h>
#include <SPI.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 mandmcount = 0;
int buttonState; // LOW or HIGH
int progState = 0; // progState is used as a state variable
int toggleButton[] = {0, 0, 0, 0, 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;
int pos = 0;
Servo bottomservo;
Servo topservo;
LiquidCrystal lcd(10);
//===========================================================
void setup() {
Serial.begin(9600);
Serial.flush();
delay(2);
Serial.println("<Initializing...>");
bottomservo.attach(9);
topservo.attach(1);
pinMode(buttonPin, INPUT);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(out, INPUT);
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
bottomservo.write(0);
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(50);
Serial.println("- Awaiting Start Command Prompt -");
lcd.setCursor(0, 0);
lcd.print("Awaiting Start");
lcd.setCursor(0, 1);
lcd.print("Command 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");
lcd.setCursor(0, 0);
lcd.print("<INNCORRECT>");
lcd.setCursor(0, 1);
lcd.print("Wait 10 Seconds");
delay(10000);
Serial.println("Accepting new imput");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Accepting new");
lcd.setCursor(0, 1);
lcd.print("imput");
delay(5);
}
}
else if (buttonState == LOW) {
Serial.println("<Button>");
buttonState = digitalRead(buttonPin);
if (toggleButton[1] == 1) {
Serial.println ("-Paused-");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("-Paused-");
delay(500);
while (toggleButton[1] == 1) {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
toggleButton[1] = 0;
}
}
}
buttonState = digitalRead(buttonPin);
if (toggleButton[1] == 0) {
mandmStart();
while (toggleButton[1] == 0) {
color();
mandmColor();
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
toggleButton[1] = 1;
delay(500);
}
}
}
}
}
//===========================================================
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)");
bottomservo.write(30);
mandmcount = mandmcount + 1;
}
else if (red >= 23 && red <= 30 && green >= 38 && green <= 49 && blue >= 26 && blue <= 34) {
Serial.println(" - (Red)");
lcd.print(" - (Red)");
bottomservo.write(60);
mandmcount = mandmcount + 1;
}
else if (red >= 30 && red <= 40 && green >= 27 && green <= 39 && blue >= 25 && blue <= 31) {
Serial.println(" - (Green)");
lcd.print(" - (Green)");
bottomservo.write(90);
mandmcount = mandmcount + 1;
}
else if (red >= 38 && red <= 45 && green >= 32 && green <= 42 && blue >= 17 && blue <= 26) {
Serial.println(" - (Blue)");
lcd.print(" - (Blue)");
bottomservo.write(120);
mandmcount = mandmcount + 1;
}
else if (red >= 18 && red <= 22 && green >= 36 && green <= 47 && blue >= 28 && blue <= 34) {
Serial.println(" - (Orange)");
lcd.print(" - (Orange)");
bottomservo.write(150);
mandmcount = mandmcount + 1;
}
else if (red >= 31 && red <= 39 && green >= 40 && green <= 48 && blue >= 28 && blue <= 35) {
Serial.println(" - (Brown)");
lcd.print(" - (Brown)");
bottomservo.write(180);
mandmcount = mandmcount + 1;
}
else {
Serial.println(" - (No M&M)");
lcd.print(" - (No M&M)");
//bottomservo.write(0);
}
lcd.setCursor(12, 1);
lcd.print("#");
lcd.print(mandmcount);
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);
}