reversing the direction of a bar chart in C

98 Views Asked by At

I want to programme my own version of a variometer to represent the climb and sink in metres/second. The values are displayed on to a LCD module screen. The climb and sink values are scaled down from a potentiometer that I can rotate to change the values while the program is running. The climb values to be displayed range from climb: 0 m/s to climb: 300 m/s and the sink values range from sink: 0 m/s to sink: -300 m/s. This bit is fine.

What I want to now do is display these in the form of a bar chart with zero point in the centre. As climb increases to 300 the blocks on the right of the zero point are filled accordingly from left to right. When the sink value decreases to -300 I want the blocks on the left hand side of the zero point to fill accordingly from right to left, essentially reversing the direction of the right hand side.

   climb = (DELVAL*600ul + 1023/2)/1023;               
   sink =  (DELVAL*600ul + 1023/2)/1023;

   climb2 = ((DELVAL-511.5)*600ul + 1023/2)/1023;                        
   sink2 =  ((DELVAL+511.5)*600ul + 1023/2)/1023;

   if (DELVAL > 511.5)
   {
        sprintf(buf, "Climb: +%d  m/s ", climb-300);                
        lcd_putxy(2,0,buf);
        for (delay = 0; delay < 50000; delay++);                   
   }
   if (DELVAL < 511.5)
   {
        sprintf(buf, "Sink: -%d   m/s ", ~sink+300);                
        lcd_putxy(2,0,buf);
        for (delay = 0; delay < 50000; delay++);
   }
   character1 = climb2 / 35;
   character2 = sink2 / 35;

      lcd_move(1,10);                                  

   for (i = 0; i < character1; i++)                            
   {

      lcd_putchar(0xD0);                                      
   }
      lcd_putstr("  ");                                      

  for (delay = 0; delay < 50000; delay++);            

  lcd_move(1,2);                                      

  for (i = 0; i < character2; i++)                     
  {    
       lcd_putchar(0xD0);         
  } 
   for (delay = 0; delay < 50000; delay++);

DELVAL is the data read from the potentiometer. I can get the right hand side of the bar graph to work but I cant get the left hand side working. Can anyone let me know any issues with my code and explain the best method to get the left side working.

The part that is not working in my code is the for loop containing character2.

1

There are 1 best solutions below

9
kkrambo On

You didn't describe what is not working. But I'm guessing that the left side is drawn from left to right instead of right to left as you intended. Where did you intend to reverse the direction of the left side?

Try this for reversing the left side. Note that if you used more descriptive variable names, comments, and fewer magic numbers then your code would be a lot easier to understand.

int left_bar_length = character2;
int max_bar_length = 300 / 35;
int left_scale_edge = 2;
int scale_center = left_scale_edge + max_bar_length;

// Move cursor to left end of left bar.
lcd_move(1, scale_center - left_bar_length);                                      

// Draw the left bar.
for (i = 0; i < left_bar_length; i++)                     
{    
     lcd_putchar(0xD0);         
}