Nov 16, 2014

TextGraph: An Arduino Library for Graphing Numbers in the Monitor Window

TextGraph: a simple Arduino Library


Textgraph lets you add simple dot graphs or bar graphs in your Arduino sketches without needing separate software. Since I plot values all the time, it seemed a good library candidate.
The library with example files is here in a zipped DropBox location.

To use:
  • print a number (without a trailing newline)
  • if necessary, scale the number to a positive integer ranging from 1 to 50 or more, depending on how much resolution you wish to see. Stretch the monitor window as needed.
  • Call dotgraph(n) or barGraph(n) to print the old-school ascii character graph.

Shows barGraph(n) with default chars used, scaled to 50 characters wide


Below is the arduino sketch that uses the library. (FYI, I used hilite.me to format the code so that it can be embedded in the blog.)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Example to show the use of the TextGraph library for Arduino, and as an example of how to create a library
// David Smith, 2014 released for use in Public Domain

// generate an vertical graph using text in the monitor
// print the value of a function or input data 
// then pass the value to dotGraph(n) or barGraph(n) get a graph of the amplitude.
// A quick way to visualize a data stream

// usage: TG.bargraph(n) or TG.barGraph(n, '-', '*') to explicitly pick bar and dot characters
//   where n must be a positive integer, and scaled to the width you want. Values of 50 - 100 are typical. 
//   and TG is the name you assign to the instance of TextGraph 
//   dotGraph() is equivalent to bargraph where the "bar" character is a space
// The map() and constrain() functions are often helpful.
// To keep the graph aligned horizontally, the numerical value must be a constant width; 
// Using the decimal-to-string function is a good way to format a floating point number

#include <TextGraph.h>

// global variables
static char cbuf[16];

TextGraph TG(1);  // create an instance of TextGraph named "TG"
// -----------------------------------------------------------------------
void setup(void) {
  Serial.begin(57600);
  Serial.println("\nPrint a numerical value and add a graph");
}

void loop(void) {
  unsigned int i;
  static float tstep = 0.01;
  static float t = 0;
  float y;  
  int y_scaled;

  y = 90*sin(PI*3.0*t)-50*cos(PI*7.1*t-1.0)+40;  // another function to graph
  dtostrf(y, 6, 2, cbuf);               // -nnnnnn.nn for consistent format  //dtostrf(FLOAT,WIDTH,PRECISION,BUFFER); gist.github.com/2343665 
  Serial.print(cbuf);                   // print the actual function's value
  y_scaled = map(y, -100, 100, 1, 50);  // scale function to fit Monitor window: set window at least as wide as graph range
  TG.barGraph(y_scaled, '-', '+');      // useGraph function, and select chars for bar and dot
  t = t + tstep;
  delay(200);                           //control output rate to screen
}

(Thanks to Andy Hutton and Suranjit Adhikari for helping me with the C++ class syntax.)


My tutorial for creating a library uses TextGraph as a basis. See also ascii text graphing in this earlier post