writing variable to LCD problem

Discussion in 'Programmer's Corner' started by Bear_2759, Feb 16, 2012.

  1. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    hey guys, I'll try and make this short and sweet. using Arduino Uno R3 and nokia 6100 LCD Shield from sparkfun.
    I have integer Y which for this example increases by 1 each loop while the program is running. each time it increases I need to write to the screen.
    Code ( (Unknown Language)):
    1.  
    2. void    loop()
    3. {
    4.     Y += 1;
    5.     LCDClear(x);
    6.     setStr(Y, 0, 0, RED, x);
    7.     delay(1000)
    8. }
    9.  
    this does not work, returns an error while compiling.
    "Cannot convert 'string' to 'char*' for arg '1' to 'void setStr(char*, int, int, int, int)'"

    if I set the value in the setStr line then it works fine. e.g.
    Code ( (Unknown Language)):
    1.  setStr("Hello World", 0, 0, RED, x);
    with Y it does not work, I have also tried putting Y in inverted commas e.g. \"Y\"
    I've also tried converting it from an integer to a string which also didn't work.
    any ideas? the setStr code is as follows.

    Code ( (Unknown Language)):
    1. void setStr(char *pString, int x, int y, int fColor, int bColor)
    2. {
    3.   x = x + 16;
    4.   y = y + 8;
    5.   // loop until null-terminator is seen
    6.   while (*pString != 0x00) {
    7.     // draw the character
    8.     setChar(*pString++, x, y, fColor, bColor);
    9.     // advance the y position
    10.     y = y + 8;
    11.     // bail out if y exceeds 131
    12.     if (y > 131) break;
    13.   }
    14. }
    most of it is just copy/paste out of examples.
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    The line "Y += 1;" is treating Y as if it is a number (an integer or such).

    The line "setStr(Y, 0, 0, RED, x);" is treating Y as a pointer to a character (char*).

    Also, a 'string' is an array of characters.

    You cannot just add one (+= 1) to a character string and expect some magic to happen. You really need something like a new counter variable to increment.

    That number needs to be converted to a string,I'm not an Arduino guy so someone else will need suggest a good way to do that conversion on that platform (it's some library thing).
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    You need to convert Y into a string. You can write this function yourself or you can use a library function such as num2str( ) or sprintf( ).
     
  4. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    problem is I did try to convert it to a string. I also tried, as a test creating a string and putting that in the code and it returned the same error.
    lets say this is in place of Y.

    Code ( (Unknown Language)):
    1. String mystring = "Hello World";
    2. setStr(mystring, 0, 0, RED, x);
    3.  
    exactly the same error is returned, that it cannot convert 'string' to 'char*'...

    excuse me for being a bit of a noob at programming. I will learn quick ;).
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Try using &mystring
     
  6. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    compiling still fails. same err. I've also tried setting the string as the following to make sure the inverted commas are included without success.

    Code ( (Unknown Language)):
    1. String mystring = "\"Hello World\"";
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Declare the string as
    char mystring[20];
     
  8. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    Code ( (Unknown Language)):
    1. char mystring[] = "Hello World";
    2. setStr(&mystring, 0, 0, RED, x);
    same error, this time though it's cannont convert 'char (*)[12]' to 'char*' for argument '1'....
     
  9. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    would it be possible to write my own setStr function? the only thing I dont fully understand in that function is what *pString does.

    what I have in my head at the moment is something a little more simple. because the
    Code ( (Unknown Language)):
    1. char mystring[]
    is an array, could I just pass them individually to the setChar function that the setStr funciton uses?

    I know this code isn't right, but to give you the general idea

    Code ( (Unknown Language)):
    1. for each char in mystring
    2.     setChar(char, x, y, fcolor, bcolor)
    3.     y=y + 8
    4. next
    I'm more familiar with VBS so it's the way my head works at the moment lol. trying to change the way I think.
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    I will get back to you later unless someone else figures out the problem. Don't despair.
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Did you try? :

    Code ( (Unknown Language)):
    1. char mystring[] = "Hello World";
    2. setStr(mystring, 0, 0, RED, x);
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    That is the silliest compile erroe I have ever seen. It is saying "you told me you will pass a pointer to a char, but the thing you are pointing to has 12 characters." That is truly a "WTF?"

    Perhaps try:

    Code ( (Unknown Language)):
    1. char mystring[] = "Hello World";
    2. setStr(&mystring[0], 0, 0, RED, x);
    That is a one character string.

    What compiler is this? All the previous suggestions worked using Hi-Tech C.
     
  13. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    thanks guys.

    Code ( (Unknown Language)):
    1. char mystring[] = "Hello World";
    2.  setStr(&mystring[0], 0, 0, RED, x);
    this works, a mate of mine was helping me with it today and I logged on here to tell you a solution had been found lol.

    now I just need to know, how do I convert an integer, to a string. during each loop the integer will increase by 1 and then needs to have the value of the integer copied to the string.

    ErnieM, to answer your question I'm using the Arduino complier beta build 023. this may have been fixed when they released 1.0 recently but I have to use 023 for backwards compatability with the libraries for the LCD. unless I want to re-write the libraries and my skills aren't quite up to that yet.
     
  14. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Sorry I did a little searching but didn't come up with a library reference for that. But teh interwebs are full of conversion functions, from tthe simple to the ridiculously complicated foolproof ones. Here's a simple one:

    Code ( (Unknown Language)):
    1. char* itoa(int val){
    2.   #define MAX_BUF 6      // set to hold maximum characters (2 for 0-99, 3 for 0-999, etc)
    3.     static char buf[MAX_BUF+1];
    4.     buf[MAX_BUF] = 0;
    5.     int i = MAX_BUF-1;
    6.     for(; val && i ; --i, val /= 10)
    7.         buf[i] = '0' + (val % 10);
    8.   return &buf[i+1];
    9. }[/i]


    It returns a char* (pointer) to the string to print out.
     
  15. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    (I post this here from another thread since this is the same problem.)

    As in any programming problem such as this one, you need to come up with a clear, precise algorithm. This particular problem is not so difficult.

    8-bit binary can represent unsigned integer values from 0 to 255. Hence you will need three decimal digits to cover the range. The result you are seeking is

    N = D2 x 100 + D1 x 10 + D0 x 1

    where N = 8-bit binary and D2, D1, D0 are the resulting decimal digits.

    If your MCU has an efficient integer division instruction you can use this to find D2, D1, D0 by dividing by 100 and by 10.

    If you want to try this without a divide operation the algorithm is as follows.

    1. Find how many times (D2) you can subtract 100 from N without underflowing. Save the remainder R.

    2. Find how many times (D1) you can subtract 10 from R without underflowing. The remainder is D0.

    This is a classic programming exercise for every student learning to program in ASM and C.
    Try to do it in both languages.
     
  16. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    thanks guys, itoa worked well for me. now counting up on the screen.

    mrchips, I'll try that out as soon as I have some free time.
     
Loading...