Simple LCD programming issue

I have a very simple LCD related problem. I have a detector that I would like to behave in the following way. When nothing is being detected I would like the screen to say Place Block and then have that message clear and then print the color of the lego block once being detected. This would simply loop between waiting for a block and detecting a block.

Here is code I am working with (plz excuse the messy code):

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// Pick analog outputs, for the UNO these three work well
// use ~560  ohm resistor between Red & Blue, ~1K for green (its brighter)
#define redpin 3
#define greenpin 5
#define bluepin 6
// for a common anode LED, connect the common pin to +5V
// for common cathode, connect the common to ground

// set to false if using a common cathode LED
#define commonAnode true

// our RGB -> eye-recognized gamma color
byte gammatable[256];

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL    to analog 5
   Connect SDA    to analog 4
   Connect VDD    to 3.3V DC
   Connect GROUND to common ground */
   
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();

/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup(void) {
  Serial.begin(9600);

  // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
  // Print a message to the LCD.
 lcd.print("Place Brick");
  
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);
  }
  
  // use these three pins to drive an LED
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);

  // thanks PhilB for this gamma table!
  // it helps convert RGB colors to what humans see
  for (int i=0; i<256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;
      
    if (commonAnode) {
      gammatable[i] = 255 - x;
    } else {
      gammatable[i] = x;      
    }
    //Serial.println(gammatable[i]);
  }
}

void loop(void) {


  
  uint16_t red, green, blue, clear, colorTemp, lux;
  tcs.setInterrupt(false);      // turn on LED
  delay(60);  // takes 50ms to read
  tcs.getRawData(&red, &green, &blue, &clear);
  colorTemp = tcs.calculateColorTemperature(red, green, blue);
  lux = tcs.calculateLux(red, green, blue);
  tcs.setInterrupt(true);  // turn off LED
  
  Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
  Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
  Serial.print("R: "); Serial.print(red, DEC); Serial.print(" ");
  Serial.print("G: "); Serial.print(green, DEC); Serial.print(" ");
  Serial.print("B: "); Serial.print(blue, DEC); Serial.print(" ");
  Serial.print("C: "); Serial.print(clear, DEC); Serial.print(" ");
  Serial.println(" ");

  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  //lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print("ClrTmp: "); lcd.print(colorTemp, DEC); lcd.print(" K - ");
      lcd.setCursor(0, 0);
      lcd.setCursor(0, 0);
      lcd.clear();
      lcd.print("Place Brick");
      if ((colorTemp < 2500) && (red > 1000)){
        //lcd.clear();
        lcd.setCursor(0, 1);
        lcd.print("Yellow");
      } else if ((red > blue) && (red > green) && (lux > 5000)){
        //lcd.clear();
        lcd.setCursor(0, 1);
        lcd.print("Red ");
      } else if ((blue > red) && (blue > green)){
        //lcd.clear();
        lcd.setCursor(0, 1); 
        lcd.print("Blue");
      } else if ((green > red) && (green > blue)){
        //lcd.clear();
        lcd.setCursor(0, 1); 
        lcd.print("Green");
      } else
        lcd.clear();
        lcd.setCursor(0, 0);
        cd.print("Place Brick");


  // Figure out some basic hex code for visualization
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
  Serial.print("\t");
  Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
  Serial.println();

  //Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );

  analogWrite(redpin, gammatable[(int)r]);
  analogWrite(greenpin, gammatable[(int)g]);
  analogWrite(bluepin, gammatable[(int)b]);
}

Thnks

edited: fixed if else loop to one that I expected to work instead of debug version

So you display 'Place block' and next clear the display; that does not make sense.

How do you know that no block was placed? Based on the colorTemp variable? Based on the clear variable? I don't have the hardware so can't play and figure it out; I can see that you seem to have based your code on library examples, so I suggest that you play with them to find out how you can detect that.

The basics would be

void loop()
{
  if(condition that a block is detected evaluate to true)
  {
    // display color
  }
  else
  {
    lcd.print("place block");
  }
}

Yes the detection part is good I am simply not understanding the LCD library.

I have used the conditional structure but what prompted the msg either keeps repeating or disappears. I can't seem to get it to simply stay between testing.

Because you keep on clearing the display in this part of loop() ?

  lcd.setCursor(0, 0);
  lcd.clear();
  lcd.print("Place Brick");
  lcd.clear();

How do you detect if a brick is placed or not?

Ahh yeas of course I posted that version. I was doing testing to debug and posted the obvious one that would not work. Below is the version I expected to work that didn’t. Thanks for reply.

 lcd.setCursor(0, 0);
  lcd.clear();
  lcd.print("Place Brick");
  if ((colorTemp < 2500) && (red > 1000)){
    //lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print("Yellow");
  } else if ((red > blue) && (red > green) && (lux > 5000)){
    //lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print("Red ");
  } else if ((blue > red) && (blue > green)){
    //lcd.clear();
    lcd.setCursor(0, 1); 
    lcd.print("Blue");
  } else if ((green > red) && (green > blue)){
    //lcd.clear();
    lcd.setCursor(0, 1); 
    lcd.print("Green");
  } else
    lcd.clear();
    lcd.setCursor(0, 0);
    cd.print("Place Brick");

Below is the version I expected to work that didn’t.

I’m sure that it did work. It’s just that what it did did not match your expectations. So, you need to help us understand what it did, so we can explain why your expectations are wrong.

What values are in colorTemp, red, blue, green, and lux when that code runs?

How about adding some curly braces to the else block in reply #4. It will now unconditionally print 'place brick'.

sterretje: How about adding some curly braces to the else block in reply #4. It will now unconditionally print 'place brick'.

That did it! Thank you - I knew it had to be some dumb error.