Can anyone help me with a little coding example? What I'd like to do is declare a variable, say x and increment it in the loop and print the value of x to my lcd so I can see it's value changing. Later I'll add a push button to maybe zero the x var or whatever. I searched the arduino site for any information on how to do that but came up empty. This coding isn't for anything in particular but rather just an exercise for me to learn from. Any help will be much appreciated.
Thanks in advance
jessey
int x = 0; //declare x
x = x + 1; //increment x
lcd.setCursor(12,1); //position x
lcd.print(x()/1000); //print x
When I try and compile it gives me this error:
Lcd_Shield_LiquidCrystal.ino: In function 'void loop()':
Lcd_Shield_LiquidCrystal:53: error: 'x' cannot be used as a function
The error message that I included in my post tells me that x() is not a function. What is a function in c++? When I Google the error message I get no useful information. What I'm looking for is an example code snip to accomplish printing an incrementing variable to my lcd.
I guess it thinks anything with () on the end is a function, like loop(), jim(), sin(theta), digitalWrite(13, HIGH) and of course x().
Try this... assuming you have the rest of the code as well of course. (Rule of thumb: post the whole code, not just a snippet from the middle... 8) Oh, and use the # button above the smilies to put it in the code box as below. )
int x = 0; //declare x
x = x + 1; //increment x
lcd.setCursor(12,1); //position x
lcd.print(x); //print x
I'm not sure what effect the x/1000 will have on how it displays... rather just go with "x" for a first test.
Edit.... "x" is a variable, not a function btw, hence no () on the end. A function is a bunch of code which does some task or other. Can be built-in (like sin() ) or created in your code as a shorthand way of calling the same code over and over.
The error message that I included in my post tells me that x() is not a function. What is a function in c++? When I Google the error message I get no useful information. What I'm looking for is an example code snip to accomplish printing an incrementing variable to my lcd.
Thanks
jessey
If you Google the "What is a function in c++?" you will get ...
I thought I had already tried "lcd.print(x);" but I guess not because it works a treat, many thanks! You talk about that a function "Can be built-in (like sin() ) or created in your code as a shorthand way of calling the same code over and over". Would that be like creating (writing) a sub routine and calling (gosub) on it to jump out of the loop to implement the code with a return back to the loop? I did some basic code years ago but have forgotten much of it.
I remember I wrote a little code snip in basic years ago (actually in PicbasicPro) that allowed the user to increment and or decrement a variable rather quickly, actually it starts out normally incrementing and or decrementing but the longer the user holds down the button the faster the variable changed. Anyway, I thought it was pretty good and got a kick out of doing it. Thanks...
Thanks for the code snips, it will be good to know when I see it used in a program and am trying to follow the flow of what's happening. I think I am a little out of my league with this c++ as compared to basic but I like it that the code is open source and there are so many code examples for neat projects available.