Oscilloscope with time plot? Solved!

Support forum for MegunoLink
Post Reply
Posts: 5
Joined: Wed Feb 17, 2021 5:11 pm

Wed Feb 17, 2021 5:34 pm

Hi there,

I was wondering if it is possible to use the time plot as an oscilloscope by adjusting the speed of the time axis. I am measuring oscillations with a fixed frequency and it would be very helpful if I could some how fix the sine waves in place in the time plot, so that I can see if the phase shift of the data from my two sensors.
Would be great if someone could at least tell me if should keep trying realising this in Megunolink or that I should just give up and buy an oscilloscope :)

Cheers, Tjeerd
Last edited by TuigWerk on Sun Mar 07, 2021 11:35 am, edited 1 time in total.
Posts: 371
Joined: Mon May 26, 2014 10:58 am

Thu Feb 18, 2021 7:44 am

Tricky, not sure this is possible. At least I cant think of a way right now.
Posts: 5
Joined: Wed Feb 17, 2021 5:11 pm

Sun Mar 07, 2021 11:34 am

Hi There!

I found a way to make it work :D
Instead of using a time plot , I used a XY plot, as suggested by Phil in reply to another question I had. The time counter plotted on X resets every time a certain trigger is met.
The trigger I used in the following sketch detects when the value of Y crosses the running average of Y. By adding an additional counter, it is possible to set the amount of cycles that are displayed, before the time jumps back to zero.
The result looks pretty good. It is not as stable as an oscilloscope, but very usable for my application. (To test the set-up eventually I bought an USB oscilloscope :))

This is the sketch that generates the result in the attached screenshot:

Code: Select all

const int sensorPin = A0; 

unsigned long sampleTime = 0;    // Relative time to display on X axis
unsigned long startTime = 0;     // Start time of sine wavein millis
unsigned long currentTime = 0;   // Current time of measurement in millis

byte counter = 0;  // Counter for sine wave cycles

#include "MegunoLink.h"
#include "CommandHandler.h"
#include <RunningAverage.h>

XYPlot Acc1 ("Acc1");

CommandHandler<> Commands;
InterfacePanel AccPanel;

RunningAverage Yavg(100);   // amount of samples used for running average

float Yprevious = 0;       // previous value to check agains average for reset trigger

void setup() {

  Yavg.clear();             // actively clear the running average

void loop() {

  int Yvalue = analogRead(sensorPin); // read sensor 

  Yavg.addValue(Yvalue);             // add read value to running average calculation  
  int Yaverage = Yavg.getAverage();    // get running average for comparison

  if ((Yvalue > Yprevious) && (Yvalue > Yaverage) && (Yaverage > Yprevious))  {  // compare current value and previous value with running average (trigger in upslope at average value)
    if (counter == 2) {                                                          // counter set at 2 to display two cycles
      startTime = millis();                                                      // set start time
      sampleTime = 0;                                                            // reset sample time           
      counter = 0;                                                               // reset counter         
  currentTime = millis();                                                        // set current time 
  sampleTime = currentTime - startTime;                                          // calculate sample time by subtracting the start time from the current time      
  Yprevious = Yvalue;                                                            // make current Y value the previous value 

  Acc1.SendData(F("Acc1"), sampleTime, Yvalue);                                  // send sample time (X) and Y value (Y) to XY plot    

Standing sine wave in XY plot.jpg
Standing sine wave in XY plot.jpg (196.64 KiB) Viewed 6944 times
Post Reply