Dynamic Plot using the LCD #117 - Arduino

copyright, Peter H Anderson, Baltimore, MD, May 10, '07



// LCD_PLOT - Arduino
//
// Tx ---------------------------- To Serial LCD (LCD #117)
//
// Peter H Anderson, Baltimore, MD, May 10, '07


void setup()
{

  Serial.begin(9600);
  delay(100);
  Serial.print("?B40"); // set backlight intensity
  delay(100);

  Serial.print("?D00000000000000000");		// define special characters
  delay(300);   // delay to allow write to EEPROM
  Serial.print("?D1000000000000001f");
  delay(300);
  Serial.print("?D20000000000001f1f");
  delay(300);
  Serial.print("?D300000000001f1f1f");
  delay(300);
  Serial.print("?D4000000001f1f1f1f");
  delay(300);
  Serial.print("?D50000001f1f1f1f1f");
  delay(300);
  Serial.print("?D600001f1f1f1f1f1f");
  delay(300);
  Serial.print("?D7001f1f1f1f1f1f1f");
  delay(300);
}

void loop()
{
   int i, scale32_array[30], temperature, scale32, num_cols;

   Serial.print("?f");
   delay(200);

   num_cols = 0; // number of cols of data initialized

   for (i=0; i<30; i++)
   {
      temperature = meas_temp(i);
      scale32 = scale_temperature(temperature);
      num_cols = add_to_array(scale32_array, scale32, num_cols);
      plot_array(scale32_array, num_cols);
      delay(1000); // **************** Adjust as required
   }
   while(1)
   {
     /* loop */
   }
}

void plot_array(int a[], int num_cols)
{
  int n;
  for (n=0; n<num_cols; n++)
  {
     plot_bar(a[n], n);
  }
}

int add_to_array(int a[], int v, int num_col)
{

   int n, index;

   index = num_col;
   if (index < 20)
   {
       a[index] = v;
       ++index;
       return(index);	// number of elements in the array
   }
   else
   {
       for (n=0; n<19; n++)
       {
          a[n] = a[n+1];  // move to the left
       }
       a[19] = v; // put the new element on the end
       return(20);  // number of elements in the array
   }
}

int meas_temp(int n)
{
// this is a stub which simulates a teamperature reading
   const int temp_readings[30] = {500, 550, 525, 550, 567, 575,
                                  592, 613, 650, 675, 690, 703,
                                  705, 715, 712, 717, 712, 700,
                                  695, 690, 675, 680, 670, 665,
                                  655, 650, 640, 645, 640, 635};
   return(temp_readings[n]);
}

int scale_temperature(int t)
{
  int num_lines;
   num_lines = (28 * t - 28* 490) / (720 - 490);      //  28 * (t - 475) / (750 - 475)
   return(num_lines);
}

void plot_bar(int hght, int col)
{
   int n, row;

   for (n=0; n <4;  n++)
   {
       row = 3 - n;
       Serial.print("?x");
       if(col < 10)
       {
          Serial.print("0"); // be sure it is two characters
       }
       Serial.print(col);

       Serial.print("?y");
       Serial.print(row);

       if (hght >=7)
       {
           Serial.print("?7"); // lay down a full block
           hght = hght - 7;
       }
       else
       {
           Serial.print("?");
           Serial.print(hght);
           hght = 0;
       }
       delay(10);
   }
}