Skip to main content
2 of 3
Copy edited (e.g. ref. <https://en.wiktionary.org/wiki/of#Preposition>, <https://en.wiktionary.org/wiki/integer#Noun>, and <https://en.wiktionary.org/wiki/continuously#Adverb>). Moved meta information to the end in order to deemphasise it - but it really ought to be in comments.

Wolfram Mathematica reads but closes stream feeds from Arduino Serial port after a few data sets

The Arduino sketch monitor a sensor and returns a set of three integers.

#include <Wire.h>
#include "Adafruit_AS726x.h"

//create the object
Adafruit_AS726x ams;

//buffer para leer valores en bruto
uint16_t sensorValues[AS726x_NUM_CHANNELS];

//buffer para  guardar los valores calibrados( no esta siendo utilizado en este codigo)
//float calibratedValues[AS726x_NUM_CHANNELS];

void setup() {
  Serial.begin(9600);
  while(!Serial);

  // inicializa el pin digital LED_BUILTIN como un output.
  pinMode(LED_BUILTIN, OUTPUT);

  //inicia y permite la comunicacion con el sensor
  if(!ams.begin()){
    Serial.println("could not connect to sensor! Please check your wiring.");
    while(1);
  }
}

void loop() {

  //lee la temperatura del sensor
  uint8_t temp = ams.readTemperature();

  //ams.drvOn(); // descomentar esto si quieres usar el led del sensor para hacer medidas
  ams.startMeasurement(); //begin a measurement

  //permite que el sensor lea la data cuando este disponible
  bool rdy = false;
  while(!rdy){
    //delay(1000);
    rdy = ams.dataReady();
  }
  //ams.drvOff(); //descomentar esto si quieres usar el led del sensor para hacer medidas

  //lee los valores!
  ams.readRawValues(sensorValues);
  //ams.readCalibratedValues(calibratedValues);

  //Serial.print("{");
  //Serial.print("Temp: ");
  //Serial.print(temp);
  //Serial.print(",");
  //Serial.print(" Violet: ");
  //Serial.print(sensorValues[AS726x_VIOLET]);
  //Serial.print(",");
  //Serial.print(" Blue: ");
  Serial.print(sensorValues[AS726x_BLUE]);
  Serial.print(",");
  //Serial.print(" Green: ");
  //Serial.print(sensorValues[AS726x_GREEN]);
  //Serial.print(",");
  //Serial.print(" Yellow: ");
  Serial.print(sensorValues[AS726x_YELLOW]);
  Serial.print(",");
  //Serial.print(" Orange: ");
  //Serial.print(sensorValues[AS726x_ORANGE]);
  //Serial.print(",");
  //Serial.print(" Red: ");
  Serial.print(sensorValues[AS726x_RED]);
  //Serial.print("}");

  Serial.println();
  //Serial.println();
  delay(400);

The Mathematica notebooks is

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

parseData[{val1__, 44, val2__, 44, val3__}] :=
 ToExpression@FromCharacterCode@# & /@ {{val1}, {val2}, {val3}}
parseData[___] := Sequence[]
rawReadings = {}
task = SessionSubmit[
  ScheduledTask[
   AppendTo[rawReadings,
    DeviceReadBuffer[dev, "ReadTerminator" -> 10]], {1, 100},
   "AutoRemove" -> True]]
test1 = parseData /@ rawReadings
ListLinePlot[Transpose[parseData /@ rawReadings],
  PlotLegends -> Automatic] // Dynamic
DeviceClose[dev]

But the Notebooks is not returning a dynamic feed of data from Arduino. It closes, and I am not able to determine why. It is supposed to stream a feed continuously.

Enter image description here

This is a Wolfram Mathematica / Arduino serial feed problem, but I am posting here just in for any suggestions on enhancing the Arduino code or the Mathematica notebook. The original post is here here and in here.