Boolean Logic and Words?

This is my code. I’m trying to get my LCD screen to connect to my Arduino, manually input a word, and get another word as an output. Is there a way to do this?

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“Type a genre.”);
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}

void loop()
{
// when characters arrive over the serial port…
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}

if (Serial.read == “pop”) {
lcd.print(“Shut Up and Dance by Walk the Moon”);
}
else if (Serial.read == “edm”) {
lcd.print(“Where are U Now by Jack U, Skrillex, Diplo, JB”);
}
else if (Serial.read == “country”) {
lcd.print (“Sangria by Blake Shelton”);
}
else if (Serial.read == “r&b”) {
lcd.print (“All of Me by John Legend”);
}
else if (Serial.read == “rock”) {
lcd.print (“Centuries by Fall Out Boy”);
}
else if (Serial.read == “alternative”) {
lcd.print (“Dog Days by Florence and the Machine”);
}
}

if (Serial.read == "pop") {

Serial.read() reads one character so this will never be true. In any case you have already emptied the serial buffer and displayed the characters. If you want to compare what you received then you need to save it until it is complete (how will you know ?) and then do the comparison.

See Serial input basics

Serial.read() simply reads a byte at a time. It doesn't wait or store the data. Try something like:

   // inside loop()
   char message[20];   // Probably longer than you need
   int charsRead;


 if (Serial.available()) {
    charsRead = Serial.readBytesUntil('\n', message, sizeof(message) - 1);  // Save room for null
    message[charsRead] = '\0';                    // Now it's a string
 }

if (strcmp(message, "pop") == 0) { 
   lcd.print("Shut Up and Dance by Walk the Moon");
} else {
    if (strcmp(message, "edm") == 0) {
      lcd.print("Where are U Now by Jack U, Skrillex, Diplo, JB"); 
    }
  // and so on...

The readBytesUntil() method reads the Serial object until either the Enter key was pressed or 19 characters are read, whichever comes first. When using character arrays for string, use strcmp() to compare strings.

EDIT: Also read Nick Gammon's two posts at the top of the Forum for guidelines on how to properly post code here using code tags.

Now, my code only prints the name of the genre, and not the song I'm looking for. I followed the steps above. What is happening?

We need to see your code.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Type a genre.");
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }
// inside loop()
   char message[20];   // Probably longer than you need
   int charsRead;


 if (Serial.available()) {
    charsRead = Serial.readBytesUntil('\n', message, sizeof(message) - 1);  // Save room for null
    message[charsRead] = '\0';                    // Now it's a string
 }

if (strcmp(message, "pop") == 0) { 
   lcd.print("Shut Up and Dance");
} else {
  if (strcmp(message, "edm") == 0) {
      lcd.print("Where are U Now"); 
    }
    else {
    if (strcmp(message, "alternative") == 0) {
      lcd.print("Dog Days"); 
    }
    else {
    if (strcmp(message, "r&b") == 0) {
      lcd.print("All of Me"); 
    }
    else {
    if (strcmp(message, "rock") == 0) {
      lcd.print("Centutries"); 
    }
    else {
    if (strcmp(message, "country") == 0) {
      lcd.print("Sangria"); 
    }    }   }}}}

Since I don’t have an LCD display handy, I just duplicated the output for the Serial object. While this code works, you’ll want to change it after you get comfortable with arrays.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Type a genre.");
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  char message[20];   // Probably longer than you need
  int charsRead;

  if (Serial.available()) {
    charsRead = Serial.readBytesUntil('\n', message, sizeof(message) - 1);  // Save room for null
    message[charsRead] = '\0';                    // Now it's a string
  }

  if (strcmp(message, "pop") == 0) {
    lcd.print("Shut Up and Dance");
    Serial.println("Shut Up and Dance");
  } else 
    if (strcmp(message, "edm") == 0) {
    lcd.print("Where are U Now");
    Serial.println("Where are U Now");
  }  else 
    if (strcmp(message, "alternative") == 0) {
    lcd.print("Dog Days");
    Serial.println("Dog Days");
  } else
    if (strcmp(message, "r&b") == 0) {
      lcd.print("All of Me");
      Serial.println("All of Me");
  } else
      if (strcmp(message, "rock") == 0) {
        lcd.print("Centutries");
        Serial.println("Centutries");
  } else
    if (strcmp(message, "country") == 0) {
      lcd.print("Sangria");
      Serial.println("Sangria");
    }

}

As others have said you need a succession of Serial.read()s to get a word.

Have a look at the examples in serial inout basics. They are simple reliable ways to receive data.

...R

@katemarsh: Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>


Please see this:

http://www.gammon.com.au/serial


UKHeliBob:

if (Serial.read == "pop") {

Serial.read() reads one character so this will never be true.

Not only that, but Serial.read doesn’t even call the function! You need Serial.read () to do that.