Sometimes it’s useful to show two streams of data on the same graph even if they might have completely different scales. Sure, you can scale the data to get them closer together but a better way is to use a second axis shown on the right hand side of the graph. For example, humidity between 0% and 100% on the left axis and temperature between 0 and 30 degrees celsius shown on the right axis.
In MegunoLink its simple to support a second axis. Just specify the axis for a particular series in the
SendData method. The example discussed on this page is called
TwoAxisPlot and is included in our Arduino library. Lets pull it apart and explain how it works.
First up we define our MegunoLink library objects. In this example we have an instance of our
TimePlot class called
MyPlot. This is what we use to send the plotting messaged to MegunoLink. We also have two
ArduinoTimer instances. These are used to control the timing for sending plotting messages and plotting property messages such as axes limits, title, labels etc.
TimePlot MyPlot; //no channel selected
Next we configure the serial port. In this case to 115200. In this example we are sending plotting messages quite quickly (100mS) so its useful to use this faster baud rate rather than the more typical default of 9600. We also call the
SendPlotProperties function which basically sends MegunoLink our plot tile, xlabel, ylabels (left and right), sets the yaxis limits, and sets the second yaxis to be visible.
MyPlot.SetTitle("Sine and Cosine Function Waveforms");
The final piece of code is in our main loop. Here you can see how we use our timers. The first on triggers every 100mS to calculate and send our sine and consine waveforms to MegunoLink along with all of the series properties including the line colour, line style, marker style, and the axis for the series to use. In this example the sinewave series will use the left axis and the cosinewave will use the right axis.
//Send plotting data every 100mS
seconds = (float)millis() / 1000;
dY = sin(2 * 3.141 * frequency * seconds);
dY2 = cos(2 * 3.141 * frequency * seconds + phase);
//Send Data To MegunoLink Pro
//"Sinewave" = series name, Plot::Blue = line colour, 2 = line width, Plot::Square = marker style
MyPlot.SendData(F("Sinewave"), dY, Plot::Blue, Plot::Solid, 2, Plot::Square, Plot::LeftAxis); // Sinewave = series name, dY = data to plot
MyPlot.SendData(F("Cosinewave"), dY2, Plot::Red, Plot::Solid, 2, Plot::Square, Plot::RightAxis); // By wrapping strings in F("") we can save ram by storing strings in program memory
// Colours include
// Red, Green, Blue, Yellow, Black, Magenta, Cyan, White
// Markers include
// Square, Diamond, Triangle, Circle, Cross, Plus, Star, DownwardTriangle, NoMarker
// Line style
// Solid, Dashed, Dotted, DashDot, DashDotDot
// Axis options
// LeftAxis, RightAxis, DefaultAxis
The second timer fires every 10 seconds. When this timer fires it repeats the plot properties that we discussed earlier. This is useful as if MegunoLink misses the first message telling it what the title and labels etc should be then 10 seconds later it will get this new message and correctly set them.
//Send plotting style message every 10000mS
That it! It’s pretty straightforward to use a second axis in MegunoLink.
As always the best way to make progress in the Arduino world is to use an example and we have plenty in our MegunoLink Arduino Library. Once installed open the Arduino IDE and navigate to File→Examples→MegunoLink→TimePlot→TwoAxisPlot. This will open the example we have discussed on this page and let you try it out for yourself.
Get in touch if you have any issues or suggestions.