Variable - to understand

Good day to All. Can you help me understand Arduino sketch what I try implement to my project.

In my project I use 4x4 keypad, and function srtFeedingTime is to enter timer time HH:MM:SS.

The result is displayed on LCD display.

I have problem to understand part of sketch “char j”, I do not know what it is and how it works. On the very beginning is declaration long int j.

Can you help me how this j works here.

Thx in advance

void setFeedingTime()

{

feed = true;

int i=0;

lcd.clear();

lcd.setCursor(0,0);

lcd.print("Set feeding Time");

lcd.clear();

lcd.print("HH:MM:SS");

lcd.setCursor(0,1);

while(1){ // endless loop

key = kpd.getKey();

char j;

if(key!=NO_KEY)

{

lcd.setCursor(j,1);

lcd.print(key);

r[i] = key-48;

i++;

j++;

if (j==2 || j == 5)

{

lcd.print(":");

j++;

}

delay(500);

}

if (key == 'D')

{key=0;

break;

}

}

lcd.clear();

}

In this context, j is a local variable, only "visible" within the { braces } of the while loop.

It will not conflict with other variables named j, but it will hide them, so to speak.

It would probably be clearer if you renamed that variable.

It would also be a very good idea to initialise it in the declaration,

 char localJ = 0;

Just come up with a better name than *loclaJ *. :expressionless:

After that, it's just a variable in the block and looks like it is used to count or keep track of something, so read on with your finger on the code to see what it does for you.

HTH

a7

To add to what @alto777 posted,

j is being used to control the cursor position on the LCD as characters are entered.

And to add to what @groundFungus posted, this all comes under "variable scope", which might amuse you and keep you busy for awhile when it is learning time:

google

variable scope in c++

and poke around a bit.

a7

Scope tutorial with Arduino examples.

there were several issues with the posted code

  • feed is unused and can be commented out
  • key is undefined. could be defined local - int key = kpd.getKey ();
  • j is defined within the loop and is used to determine where to put colons. but its' value is not properly initialized and it should be defined outside the loop like "i"
  • j would make more sense as an int or byte
  • why is the a half sec delay?
  • why is key set to 0 when exiting the loop
  • should the display be cleared at the end of the loop or the next time it is updated

But why is here declared as char? and not int etc.

don't know. poor coding

Usually, I use char as a data type when the associated variable is to store ASCII code of a character (A-Z, a - z, 0 - 9, and punctuation marks) of the English Language.

In your case, j is to store the cursor position; so, its data type is more likely to be byte/int instead of char though it can be used very well as its range is: -128 to 127.

Thx you. I'm working on this code, and not have all answer. It is not mine, and perhaps I rewrite all so all is understandable. Thx for your input.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.