Filling a string with characters '#' and space to create bar-graph

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nigelmercier
    New Member
    • Dec 2009
    • 45

    Filling a string with characters '#' and space to create bar-graph

    I want to create a bar-graph display using a string. The string needs to be 14 characters long, filled from the left with either a '#' character or a ' ' depending on the value of a variable, level.

    Each character represents 1/14 of the total (this is my display width), so as level rises from 0 to 14, there will be more # characters on the left, and fewer spaces, viz:

    Code:
    If level < 1, the string should be:  "              "
    If level == 1, the string should be: "#             "
    If level == 7, the string should be: "#######       "
    If level == 14, the string should be: "##############"
    I started by using a for loop, but it looked very messy. Is there a shortcut in ANSI C?

    Note: I'm a newbie, so would appreciate simple suggestions, without pointers if possible!
  • nigelmercier
    New Member
    • Dec 2009
    • 45

    #2
    BTW, in case anyone thinks to suggest a neater display method using points and lines, or other graphics.

    This is for use on a microcontroller and must be done like this. I also lied about using a #. I just can't create the actual character here as it is a self created character (to replace the Yen character in the ASCII set), and it looks like the bars in a bar-graph :)

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      One possible way is to have a hardcoded string, and use memcpy from string.h in the standard library.

      fullStr: "############## "
      if (level > 0)
      memcpy(str, fullStr, level);

      Might be easier if you post your for loop code, and we edited that.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Another way would be to have an array of all 15 possible bar graph strings and compute an array index based on your level variable.

        Comment

        • nigelmercier
          New Member
          • Dec 2009
          • 45

          #5
          Originally posted by jkmyoung
          Might be easier if you post your for loop code, and we edited that.
          In fact my loop code does not work, but here it is:

          Code:
           void upDisp(void)
          { // Update display
            char bargraph[15] = "0123456789ABCD";
            byte level;
          
            fuelRead8 = getFuel();       // get ADC value from channel 0
            ByteToStr(fuelRead8, txt4);  // convert to string for display
            strLCD11XY(txt4, 0, 0);      // display on LCD in large font at 0,0
          
            level = (fuelRead8/18); // DEGUB temp calculation, munged for test!
            ByteToStr(level, txt4); // convert to string for display
            strLCD5XY(txt4, 0 , 3); // display on LCD in small font at 0,3
          
            // come here with 0 <= level <= 14
          
            for (i = 0; i == 13; i++)
            {
              if (level > i)
              {
                bargraph[i] = '#';
              }
              else
              {
                bargraph[i] = '-';
              }
            }
            strLCD5XY(bargraph, 0 , 5); // display on LCD in small font at 0,5
          
          }
          The idea is that in the loop [line 16] the characters in the string barcode get changed. In fact they stay with the initial values.

          I'm going to check on something, because I think the original string may get put in ROM, which would explain why it doesn't work!

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            So you're saying it always displays:
            0123456789ABCD?


            Besides initializing it to all full or all empty, could also break it into 2 for loops.
            Code:
              for (i = 0; i <= level; i++) 
                  bargraph[i] = '#'; 
              for (i = i; i <= 14; i++) 
                  bargraph[i] = '-';

            Comment

            • nigelmercier
              New Member
              • Dec 2009
              • 45

              #7
              Originally posted by jkmyoung
              One possible way is to have a hardcoded string, and use memcpy from string.h in the standard library...
              All sorted. Thanks for giving me the idea where to look in the string library, I also discovered memset:

              Code:
              void upDisp(void)
              { // Update display
                char bargraph[15] = "              ";
                char reserve[15] =  "*RESERVE TANK*";
                byte level;
              // set level here
                if (level == 0)
                {memcpy(bargraph, reserve, 14);}
                else
                {memset(bargraph, 0x5C, level);} // copy block characters into string
                strLCD5XY(bargraph, 0 , 5); // display on LCD in small font at 0,5

              Comment

              • nigelmercier
                New Member
                • Dec 2009
                • 45

                #8
                [QUOTE=nigelmerc ier;3549785]

                Code:
                for (i = 0; [B]i == 13[/B]; i++)
                //should be i <= 13
                I've also found the error in the loop, very surprised none of you guys spotted it!

                For some reason I can't get it into my head that the second expression in a for statement is a while rather than a terminating condition! Must be all that BASIC I did in the 1970s.

                Comment

                Working...