How to do that?

Hey everyone :slight_smile:
It's me, again :smiley:

So, here's the code

/*-----( Import needed libraries )-----*/
#include <LiquidCrystal.h>

/*-----( Declare objects )-----*/
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //These are the pins used on this shield

/*-----( Declare Constants )-----*/
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

/*-----( Declare Variables )-----*/
int lcd_key       = 0;
int adc_key_in    = 0;
int adc_key_prev  = 0;
byte numbChar      = '0';
int lettChar      = 'a';

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  lcd.begin(16, 2);              // start the lcd object
  
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  lcd.setCursor(9, 0);    //set cursor to (9, 0)
  lcd.write(lettChar);    //print "a"
  
  lcd.setCursor(9, 1);    //set cursor to (9, 1);
  lcd.write(numbChar);    //print "0"
  
  adc_key_prev = lcd_key ;       // Looking for changes
  lcd_key = read_LCD_buttons();  // read the buttons

  lcd.setCursor(0,1);            // move to the begining of the second line

  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
  case btnRIGHT:
    {
      lcd.setCursor(10, 1);
      lcd.print("  ");
      numbChar++;
      lcd.setCursor(9, 1);
      lcd.write(numbChar);
      delay(250);
      if (numbChar == '9')
      {
        delay(250);
        numbChar = '0';
      }
      break;
    }
  case btnLEFT:
    {
      lcd.setCursor(10, 1);
      lcd.print("  ");
      numbChar--;
      lcd.setCursor(9, 1);
      lcd.write(numbChar);
      delay(250);
      if (numbChar == '0')
      {
        delay(250);
        numbChar = '9';
      }
      break;
    }
  case btnUP:
    {
      lcd.setCursor(10, 0);
      lcd.print("  ");
      lettChar++;
      lcd.setCursor(9, 0);
      lcd.write(lettChar);
      if (lettChar == 'z')
      delay(250);
      {
        delay(250);
        lettChar = 'a';
      }
      break;
    }
  case btnDOWN:
    {
      lcd.setCursor(10, 0);
      lcd.print("  ");
      lettChar--;
      lcd.setCursor(9, 0);
      lcd.write(lettChar);
      if (lettChar == 'a')
      delay(250);
      {
        delay(250);
        lettChar = 'z';
      }
      break;
    }
  case btnSELECT:
    {
      lcd.print("SELECT");
      break;
    }
  case btnNONE:
    {
      lcd.print("NONE  ");
      break;
    }
  }/* --(end switch )-- */

}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/

int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor 
  delay(5); //switch debounce delay. Increase this delay if incorrect switch selections are returned.
  int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise
  if (5 < abs(k)) return btnNONE;  // double checks the keypress. If the two readings are not equal +/-k value after debounce delay, it tries again.
  // 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
  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...
}

How to make that when numbChar reaches 9, lcd will write 0, and rerversed...

How to make that when lettChar reaches z, lcd will print a, and reversed...

Thanks for help :wink:

numbChar++;

I am sure that you know that this increments the numbChar variable.
So would

numbChar += change;

if you set change to 1. If change were -1 then it would decrement numbChar.

Now, suppose that you were to have a variable in your program named change set to 1 and when numbChar got to 9 you set change to -1 and when numbChar got to 0 you set change it to 1

Does that give you some ideas to work with ?

For one thing, this is broke

      if (lettChar == 'z')
      delay(250);
      {
        delay(250);
        lettChar = 'a';
      }

and there's another like it farther down.

If you want 'z' to increment to 'a' then you need to find the increment happens and make it so.
Like if it is supposed to increment and if the letter is 'z' then it gets set to 'a' else it increments.
And where it decrements, check first for letter 'a' to set to 'a' else decrement the letter.

Same with '0' to '9'.

simpler example, not tested, to print 0 to 9 again and again forever.

byte count = 9;

void loop()
{
if ( count > 8 )
{
count = 0;
}
else
{
count++;
}
Serial.println( count );
}

Thanks u for a help, I will try to fix it when I will be able to do that, because now I will go out and I won't have a computer :slight_smile:

GoForSmoke:
For one thing, this is broke

      if (lettChar == 'z')

delay(250);
     {
       delay(250);
       lettChar = 'a';
     }




and there's another like it farther down.

If you want 'z' to increment to 'a' then you need to find the increment happens and make it so.
Like if it is supposed to increment and if the letter is 'z' then it gets set to 'a' else it increments.
And where it decrements, check first for letter 'a' to set to 'a' else decrement the letter.

Same with '0' to '9'.


simpler example, not tested, to print 0 to 9 again and again forever.

byte count = 9;

void loop()
{
if ( count > 8 )
{
count = 0;
}
else
{
count++;
}
Serial.println( count );
}

Question - why count is a btye, not a int?

why count is a btye, not a int?

Because it is the smallest type of numeric variable that will hold the range of values required. An int uses 2 bytes so one of them would be wasted and it is always good to keep memory use to a minimum.

A byte will hold 0 to 255 and only needs 1 byte of memory.

An int will hold -32768 to +32767 and needs 2 bytes of memory.

An UNO only has 2048 bytes of RAM for everything.

So I think it is a BAD ARDUINO HABIT to use int where a byte or char will do.
Honest, most programmer finger reflexes learn to type int before the brain gets the thought out.

What happens when you need 500 numbers in an array? Will you suddenly develop a new habit?
What often happens is the first time you know better is after spending a long time searching for a bug you will tend to be self-blind to the cause of. I know this from decades experience. Now you do.

Thanks for quick answer :slight_smile: