1
$\begingroup$

My WM code reads Arduino data from three analog sensors. My code successfully captured the data from the buffer and add the date of capture using the Now[] function. I am trying to use ListLinePlot[] or StackedDateListPlot[] but not getting a plot.

Here is my code:

Open the Serial device. Substitute the port to the one in your environment

dev = DeviceOpen["Serial", "COM4"]

Create function to reformat the ASCii format data to standard readable data

parseData[{val1__, 9, val2__, 9, val3__}] := 
 ToExpression@FromCharacterCode@# & /@ {{val1}, {val2}, {val3}}
parseData[___] := Sequence[]

This line reads the buffer and adds the time each buffer data line is captured. This is scheduled every second for 20 second

rawReadings = {};
task = SessionSubmit[
  ScheduledTask[
   AppendTo[rawReadings, 
    Flatten[{Now, 
      DeviceReadBuffer[dev, "ReadTerminator" -> 10]}]], {1, 20}, 
   "AutoRemove" -> true]]

Finally, here is the plot command

ListLinePlot[Transpose[parseData /@ rawReadings], 
  PlotLegends -> Automatic] // Dynamic

Close the device (TaskRemove[task];)

DeviceClose[dev]

here is my output enter image description here

You will see the error at the plot. I checked the Arduino serial monitor and confirm data is sent. Please, help me getting a correct plot and with your suggestions.

UPDATE 1

My Arduino sketch is

//***************************
// Credit original code to
//  Aleksandr Berezutskii 
//***************************
    int raw1 = 0, raw2 = 0, raw3 = 0;
        int x = 0, y1 = 0, y2 = 0, y3 = 0;
        #define APin1 A11
        #define APin2 A12
        #define APin3 A13
        void setup() 
        {
        Serial.begin(9600);
        pinMode( APin1, INPUT );
        pinMode( APin2, INPUT );
        pinMode( APin3, INPUT );
        analogReference(DEFAULT);
        }

        void loop() 
        {
        while (x<=6000)
        { 
        raw1 = analogRead( APin1 );
        y1=raw1;
        raw2 = analogRead( APin2 );
        y2=raw2;
        raw3 = analogRead( APin3 );
        y3=raw3;
        x++;               
        Serial.print( y1 );
        Serial.print("\t"); 
        Serial.print( y2 );
        Serial.print("\t");                
        Serial.print( y3 );
        Serial.println();
        delay(400);
        }
        }
$\endgroup$
  • 2
    $\begingroup$ This is not plotting problem. The error in the plot comes from trying to transpose an empty list. You should check rawReadings before trying to plot. if it is non-empty, the error is in parseData. $\endgroup$ – m_goldberg Jan 20 at 14:20
  • $\begingroup$ @ m_goldberg Thank you .. I tested the code without the Now[] and produced the plot. I do understand why adding the date to the data does not plot. Also, not sure why you are getting empty list, am getting the date data in each row of the data matrix. $\endgroup$ – Jose Enrique Calderon Jan 20 at 19:41
1
$\begingroup$

The problem is with the location of the Now() function. This inserts a Wolfram Language object (the date object) inside the rawReadings list. But the list is further process by parsedata which handles the elements as been ASCii coded intergers.

the solution is to extract and remove the date object from the rawReadings list to a separate list (dates1). After the rawReadings data is parsed to DEC format, the date list is re-inserted. This will allow plot function to execute.

rawReadings
(* Drop the date object from the list *)
subraw = Drop[#, {1}] & /@ rawReadings

test1 = parseData /@ subraw

ListLinePlot[Transpose@%, PlotLegends -> Automatic]


(* Crate a separate list holding dates*)
dates1 = Table[rawReadings[[x]][[1]], {x, Length[rawReadings]}]


(* Thread the dates to the processed raw data*)
MapThread[Append, {test1, dates1}] // MatrixForm

(* Create Time Line plot*)
TimelinePlot[Map[<|Thread[# -> dates1]|> &, test1\[Transpose]]]

(* Create Line  plot*)
TimelinePlot[MapThread[<|#1 -> #2|> &, {test1, dates1}]]


(* Create Time plot*)
td = TemporalData[test1, {dates1}]

DateListPlot[td, GridLines -> Automatic]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.