Go Down

Topic: Using Serial imput to run a code (Read 2533 times) previous topic - next topic

DH12043

I do not have a I2C chip, i simply have 2 595 shift registers and a basic 16x2 lcd display with no built in shift register. Is there any codes that would allow me to utilize this?

ieee488

Is there any codes that would allow me to utilize this?
Yes, but not if you don't have the pins available.


DH12043

I am curently using it normally without a 595 shift register, however, I needed to add new devices such servos and a small speaker which will put me past my max amount of pins. I figured this would be the easiest way to free up a few pins without purchasing additional hardware. If anyone knows how to accomplish this, please let me know.
Thanks in advance, DH

P.S. I am using an Eelegoo Super Starter Kit Uno R3 Project and a color sensor as well as wall adapter to supply power.

sterretje

Is there any codes that would allow me to utilize this?
Did you do any web searches? Google results for lcd hc595

http://www.instructables.com/id/LCD-Screen-With-74HC595-and-Keypad-Module/ which links to https://playground.arduino.cc/Main/LiquidCrystal

Now I do not know if it's a good idea to replace the original LiquidCrystal library; personally I would not do that and rather install in the libraries directory for 3rd party libraries or in the sketch directory itself and fight a bit to get it working (if needed).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

DH12043

Thanks for the tips, I had been looking stuff up on google, old forums, and everywhere else for the last week. Aparently, i missed the correct serch criterea. Thanks so much. For the benefit of anyone that is having the same problem as i was, use http://fritzing.org/projects/lcd-16x2-74hc595 to control a lcd with 3 pins via a shift register. You will need to replace the library with the one provided.

DH12043

I am trying to add a way to toggle the pushbutton in my code between running the M&M sorter, and a paused state. Unfortuanately, the code refuses to pause when i press the button a second time. is there any easy ways to do this?

Here is a copy of my current code:
Code: [Select]
#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>");
    if (toggleButton[1] = 0) {
      Serial.println ("-Paused-");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print ("-Paused-");
      while (toggleButton[1] = 0) {
        if (buttonState == LOW) {
          toggleButton[1] = 1;
        }
      }
    }
    else if (toggleButton[1] = 1) {
      mandmStart();
      while (toggleButton[1] = 1) {
        color();
        mandmColor();
        if (buttonState == LOW) {
          toggleButton[1] = 0;
        }
      }
    }
  }
}
//===========================================================
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);
}

Sorry, it is rather long as I have been working on it for multiple weeks now.

Thanks, DH

uxomm

#36
Apr 13, 2018, 11:52 pm Last Edit: Apr 14, 2018, 12:07 am by uxomm
Code: [Select]
if (toggleButton[1] = 0) {
Do you mean this:
Code: [Select]
if (toggleButton[1] == 0) {
This occurs several times in your code.

Be aware about the difference between
=
and
==
Always decouple electronic circuitry.

DH12043

My code has been revised to read this:
Code: [Select]
#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>");
    if (toggleButton[1] == 1) {
      Serial.println ("-Paused-");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print ("-Paused-");
      while (toggleButton[1] == 1) {
        if (buttonState == LOW) {
          toggleButton[1] = 0;
          delay(500);
        }
      }
    }
    else if (toggleButton[1] == 0) {
      mandmStart();
      while (toggleButton[1] == 0) {
        color();
        mandmColor();
        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);
}


Now the pausing works but it only goes through the sorting function once and infinetly gives me the same valuse instead of updating and re-runing once a second.

DH12043

I am writing a code that will eventually sort M&M's. It can be started two ways, first, by typing "start" into the serial monitor and hitting enter, and secondly by pressing a pushbutton. My goal is to also be able to pause to code using this button. As of right now, my code will pause and start like i want it to, however, it only runs once when pressed, not until i press it agian like i want it to.

Here is a copy of my current code:
Code: [Select]
#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>");
    if (toggleButton[1] = 0) {
      Serial.println ("-Paused-");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print ("-Paused-");
      while (toggleButton[1] = 0) {
        if (buttonState == LOW) {
          toggleButton[1] = 1;
        }
      }
    }
    else if (toggleButton[1] = 1) {
      mandmStart();
      while (toggleButton[1] = 1) {
        color();
        mandmColor();
        if (buttonState == LOW) {
          toggleButton[1] = 0;
        }
      }
    }
  }
}
//===========================================================
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);
}

PaulS

Code: [Select]
  Serial.begin(9600);
  Serial.flush();

Initialize the outgoing serial buffer, empty. Wait until it is empty. Why the f**k is THAT necessary?

Code: [Select]
  pinMode(out, INPUT);
Well, that looks real smart.

Code: [Select]
  pinMode(buttonPin, INPUT);
You are not using the internal pullup resistor. Why not? So, how IS your external resistor wired? How is your switch wired?

Code: [Select]
    if (toggleButton[1] = 0) {
= is the assignment operator, NOT the equality operator.
The art of getting good answers lies in asking good questions.

PaulS

Code: [Select]
      while (toggleButton[1] == 0) {
        color();
        mandmColor();
        if (buttonState == LOW) {
          toggleButton[1] = 1;
          delay(500);
        }
      }

What will cause the value in buttonState to change, while this loop is running? Hint: Not a damned thing.
The art of getting good answers lies in asking good questions.

Coding Badly


@DH12043, do not cross-post.  Threads merged.


DH12043

My problem is that color() and mandmColor() only run once instead of until the button is pushed again like i want.

Quote
Code: [Select]
pinMode(out, INPUT);
Well, that looks real smart.
"out" is simply the name of a color sensor pin.

Quote
you are not using the internal pullup resistor. Why not?
because the external one is more powerful.

Quote
Code: [Select]
if (toggleButton[1] = 0) {

= is the assignment operator, NOT the equality operator.
It has been changed in my last reply.

Quote
What will cause the value in buttonState to change, while this loop is running?
It should check if the button is pressed.
Code: [Select]
if (buttonState == LOW) {

PaulS

Quote
"out" is simply the name of a color sensor pin.
Why? The fact that it is "out" from the sensor's perspective is NO excuse for naming it "out" on the Arduino end of the connection. colorSensorPin would make a LOT more sense.

Quote
because the external one is more powerful.
And you need a "more powerful" (whatever the hell that means) one because?

Quote
It should check if the button is pressed.
buttonState doesn't check squat. Try again. What ACTUALLY checks to see of the pin is HIGH or LOW?
The art of getting good answers lies in asking good questions.

DH12043

Quote
And you need a "more powerful" (whatever the hell that means) one because?
It cuts down on interference and makes it less likely to randomly read LOW when it is not pressed.


Quote
buttonState doesn't check squat. Try again. What ACTUALLY checks to see of the pin is HIGH or LOW?
Does this look good?
Code: [Select]
  else if (buttonState == LOW) {
    Serial.println("<Button>");
    progState = digitalRead(buttonPin);
    if (toggleButton[1] == 1) {
      Serial.println ("-Paused-");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print ("-Paused-");
      while (toggleButton[1] == 1) {
        if (buttonState == LOW) {
          toggleButton[1] = 0;
          delay(500);
        }
      }
    }
    progState = digitalRead(buttonPin);
    else if (toggleButton[1] == 0) {
      mandmStart();
      while (toggleButton[1] == 0) {
        color();
        mandmColor();
        if (buttonState == LOW) {
          toggleButton[1] = 1;
          delay(500);
        }
      }
    }
  }

Go Up