Left, Right, Center

I got it generating the letters but they generate all in the spot’s, I want different letters (L,R,C) to generate in those spot’s, how would I do such a thing?

char LRC[3] = {'L', 'R', 'C'};

void loop() {
  //char c = 'a' + (char)random(0,26);
  char c = LRC[random(0,3)];
  Serial.println(c);
  delay(2000);

if(lcd_key == btnSELECT) {
        lcd.clear();
        lcd.setCursor(2, 0);
        lcd.print (c);
        lcd.setCursor(8, 0);
        lcd.print(c);
        lcd.setCursor(14,0);
        lcd.print(c);
    }

}

tarbear123:
I got it generating the letters but

Pretend that nobody has the foggiest idea what "it" is or that they know what you're talking about, and give a bit more info. (Including a full sketch.)

I’ve built a dice roller, and working on Left, Right, Center (one of the games) using a lcd keypad shield and I want different letters (L,R,C) to generate in different spot’s on the lcd screen
here’s what I have so far

#include <LiquidCrystal.h>
LiquidCrystal lcd (8, 9, 4, 5, 6, 7);
    // define some values used by the panel and buttons
    int lcd_key     = 0;
    int adc_key_in  = 0;
  #define btnRIGHT  0
  #define btnUP     1
  #define btnDOWN   2
  #define btnLEFT   3
  #define btnSELECT 4
  #define btnNONE   5
    // read the buttons
    int read_LCD_buttons() {
      adc_key_in = analogRead(0);      // read the value from the sensor
      // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
      // we add approx 50 to those values and check to see if we are close
      //if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
      // For V1.1 us this threshold
  
      if (adc_key_in > 1000) { return btnNONE; }
  
      // For V1.0 comment the other threshold and use the one below:
  
      if (adc_key_in < 50)   { return btnRIGHT; }
  
      if (adc_key_in < 195)  { return btnUP; }
  
      if (adc_key_in < 380)  { return btnDOWN; }
  
      if (adc_key_in < 555)  { return btnLEFT; }
  
      if (adc_key_in < 790)  { return btnSELECT; }
  
      return btnNONE;  // when all others fail, return this...
    }
void setup() {
  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0, 0);
  lcd.print("press select to dice"); // print a simple message
  randomSeed (analogRead (1));
}
void loop(){
  char LRC[3] = {'L', 'R', 'C'};
  char c = LRC[random(0,3)];
lcd_key = read_LCD_buttons();  // read the buttons

    if(lcd_key == btnSELECT) {
        lcd.clear();
        lcd.setCursor(2, 0);
        lcd.print (c);
        lcd.setCursor(8, 0);
        lcd.print(c);
        lcd.setCursor(14,0);
        lcd.print(c);
    }
        analogRead(lcd_key == btnSELECT);
        delay(50);
        analogRead(lcd_key == btnSELECT);
        delay(50); 
}

I'm still not sure what you mean.

Do you need to hit select and randomly choose one of the letters L, C or R, and then display that randomly somewhere at one of the 16x2 available positions?

you hit select an it generates the random letters and it shows the letters in the spot’s

what "spots"?

these spots on the lcd screen

        lcd.clear();
        lcd.setCursor(2, 0);
        lcd.print (c);
        lcd.setCursor(8, 0);
        lcd.print(c);
        lcd.setCursor(14,0);
        lcd.print(c);

I'm having to read between the lines here, still not clear to me, but do you mean that if the letter is L, it must only appear once, at 2,0 and if it's C it must only appear at 8,0 and the R only at 14,0? (Whereas now, it prints L L L or C C C or R R R I guess.)

So you just need to do an "if" surely, to position the cursor based on the character, then print it once only:

if (c== 'L') lcd.setCursor(2, 0);
if (c== 'C') lcd.setCursor(8, 0);
if (c== 'R') lcd.setCursor(14, 0);
lcd.print(c);

This is very odd:

        analogRead(lcd_key == btnSELECT);

If the value of lcd_key, which was assigned by calling read_LCD_buttons() is equal to btnSELECT is calls analogRead() with 1 which reads A1, if not it calls analogRead() with 0 which reads A0
But then nothing is done with the analogRead() value.

Do you have anything hooked up to A1 ?

Seems like an odd thing to do.

--- bill

the buttons (on the lcd keypad shield) are hooked to A0

neiklot:
I'm having to read between the lines here, still not clear to me, but do you mean that if the letter is L, it must only appear once, at 2,0 and if it's C it must only appear at 8,0 and the R only at 14,0? (Whereas now, it prints L L L or C C C or R R R I guess.)

So you just need to do an "if" surely, to position the cursor based on the character, then print it once only:

if (c== 'L') lcd.setCursor(2, 0);

if (c== 'C') lcd.setCursor(8, 0);
if (c== 'R') lcd.setCursor(14, 0);
lcd.print(c);

neiklot:
I'm having to read between the lines here, still not clear to me, but do you mean that if the letter is L, it must only appear once, at 2,0 and if it's C it must only appear at 8,0 and the R only at 14,0? (Whereas now, it prints L L L or C C C or R R R I guess.)

So you just need to do an "if" surely, to position the cursor based on the character, then print it once only:

if (c== 'L') lcd.setCursor(2, 0);

if (c== 'C') lcd.setCursor(8, 0);
if (c== 'R') lcd.setCursor(14, 0);
lcd.print(c);

I ran into a problem
see attached pic

20190406_122055[932].jpg

tarbear123:
I ran into a problem
see attached pic

3826884468c6f23fa8f164b415711d7fdc153c81.jpg
Ah! The dreaded "DFRobot board.

Did you read this warning thread?


if (c== 'L') lcd.setCursor(2, 1); 
if (c== 'C') lcd.setCursor(8, 1);
if (c== 'R') lcd.setCursor(14, 1);
lcd.print(c);

I got it generating the letters but when I press the button it shows up in the top left corner (only one letter)
I've tried your code and putting the setCursor below each code and it still pops up in the same spot

tarbear123:
I got it generating the letters but when I press the button it shows up in the top left corner (only one letter)
I've tried your code and putting the setCursor below each code and it still pops up in the same spot

I've no idea what that means, but surely you realise that to stop it looking like this:

... you need to do a clear between visits to the screen with new stuff?

But that all said, I do have one of those same boards lurking around somewhere, so I can try your latest code sometime if you like.

(And yes @Paul__B mine suffers from that malaise, and years ago I added that kluge for the backlight. I know adafruit have a nice one that's I2C with the screen and the buttons all on the bus. I keep meaning to get one.)

neiklot:
you need to do a clear between visits to the screen with new stuff?

Not such a good idea! :roll_eyes:

Certainly not a good idea if you are updating frequently because a lcd,clear() is actually very slow and causes annoying flicker - a common complaint here. I suspect that is the actual cause of

tarbear123:
I got it generating the letters but when I press the button it shows up in the top left corner (only one letter)

What is important is carefully planning what it is you want to see. My suggestion was to put the “L - C - R” on the bottom line if you want to use the top line for messages. But I - keeping it simple - omitted to mention the need (if you did not clear the screen) for erasing the character not wanted when printing a new one, simply by printing a space at that cursor position. There there is no need to clear the screen; it is much faster and a smoother display.

When overwriting a piece of test, you print over it a text of the same (or of course, longer) length; it is easiest to make all coincident messages the same length padded with spaces where required.

Paul__B:
Not such a good idea! :roll_eyes:

Agreed, bad wording on my part. I didn’t mean “a” clear, as in lcd.clear, but rather clear the previous character with an over-write when the new one gets positioned.

let me know if you can fix the problem, plz and thank-you

tarbear123:
let me know if you can fix the problem

Post your current code then, please-and-thank-you

tarbear123:
let me know if you can fix the problem, plz and thank-you

I don’t believe it is possible for anybody to help you fix the “problem” as we don’t fully understand what you are trying to do.
This was pointed out and asked in the very first response (post #1) and again in posts, #3, #5, #7 and we still don’t know for sure what you are trying to do.

We can’t even tell what you are trying to do from looking at the code you have posted.
So far the only full code we have seen is in post #2 and that cannot create the display shown in post #10. The code you showed us in post #2, will flicker with a single random letter appearing in all 3 positions constantly changing.
In the code, you are also doing some bogus analog reads as I pointed out int post #8.

I think at this point, you should take a big step back and explain to us what you are attempting to do.
I.e. explain in words the specific and exact behavior you are wanting and how the buttons affect the display.

When you say words like:

you hit select an it generates the random letters and it shows the letters in the spot’s

it doesn’t help since it doesn’t make sense to us. And attempting to show us code to try and explain isn’t helping.
Tell us in words what you are trying to do, once we understand that, we can likely help you.

— bill