-1

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 Mathematica Exchange post and in here

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

     #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 not able to determine why. It is suppose to stream a feed continously. enter image description here

3

1 Answer 1

0

Jose! As I already mentioned in Mathematica SE, you need to remove the delay statements out of your code and additionally send the data to serial only if new data has arrived.

1
  • Thank you for the feedback. I posted a update in Mathematica Exchange with a revised code with you suggestions. But so far no getting the dynamic stream to maintain continuously. Sep 22, 2023 at 23:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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