Skip to main content
added 1452 characters in body
Source Link

Get rid of the delay(1000) and delay(400) statements in your arduino code. Delay has blocking behaviour. Take a look at the examples -> blink without delay. The delay(1000) statement inside the while construction makes no sense to me. it blocks the whole code execution for 1000ms while the boolean variable rdy may have changed to true.

EDIT 1: Ok, here is a minimum working example for you. Since I do not have access to the Adafruit sensor I will use some dummy random numbers and transfer these via Serial. It sends that data every 250ms.

unsigned long previousMillis = 0;  
const long interval = 250;  
int myArray[3];
const int number=100;
bool send=false;

void setup() {
Serial.begin(115200);
}

void loop() {
  unsigned long currentMillis = millis();
  for(byte i=0;i<3;i++){
    myArray[i]=random(-20,20);

  }
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    send=true;    
    }
  if (send){
    Serial.print("{");
    Serial.print(number+myArray[0]);
    Serial.print(",");
    Serial.print(number+myArray[1]);
    Serial.print(",");
    Serial.print(number+myArray[2]);  
    Serial.println("}"); 
    send=false;    
  }

  }

To process the data being sent try the following on the Mathematica side:

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

rawReadings = {}

task = RunScheduledTask[
rawReadings = {rawReadings, DeviceReadBuffer[dev, 3]};
rawReadings = Partition[Flatten@rawReadings, 3], 0.05]

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

RemoveScheduledTask[task]
DeviceClose[dev]
   

Get rid of the delay(1000) and delay(400) statements in your arduino code. Delay has blocking behaviour. Take a look at the examples -> blink without delay. The delay(1000) statement inside the while construction makes no sense to me. it blocks the whole code execution for 1000ms while the boolean variable rdy may have changed to true.

Get rid of the delay(1000) and delay(400) statements in your arduino code. Delay has blocking behaviour. Take a look at the examples -> blink without delay. The delay(1000) statement inside the while construction makes no sense to me. it blocks the whole code execution for 1000ms while the boolean variable rdy may have changed to true.

EDIT 1: Ok, here is a minimum working example for you. Since I do not have access to the Adafruit sensor I will use some dummy random numbers and transfer these via Serial. It sends that data every 250ms.

unsigned long previousMillis = 0;  
const long interval = 250;  
int myArray[3];
const int number=100;
bool send=false;

void setup() {
Serial.begin(115200);
}

void loop() {
  unsigned long currentMillis = millis();
  for(byte i=0;i<3;i++){
    myArray[i]=random(-20,20);

  }
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    send=true;    
    }
  if (send){
    Serial.print("{");
    Serial.print(number+myArray[0]);
    Serial.print(",");
    Serial.print(number+myArray[1]);
    Serial.print(",");
    Serial.print(number+myArray[2]);  
    Serial.println("}"); 
    send=false;    
  }

  }

To process the data being sent try the following on the Mathematica side:

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

rawReadings = {}

task = RunScheduledTask[
rawReadings = {rawReadings, DeviceReadBuffer[dev, 3]};
rawReadings = Partition[Flatten@rawReadings, 3], 0.05]

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

RemoveScheduledTask[task]
DeviceClose[dev]
   
Source Link

Get rid of the delay(1000) and delay(400) statements in your arduino code. Delay has blocking behaviour. Take a look at the examples -> blink without delay. The delay(1000) statement inside the while construction makes no sense to me. it blocks the whole code execution for 1000ms while the boolean variable rdy may have changed to true.