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.

Detailed Description

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.

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.

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.

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.

Wrap Up

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.

Start typing and press Enter to search