Problem with split() and serialEvent

Sure! I just needed to add the println(inString) as you mentioned and incremented the delay in Processing. Here both codes. You’ll see that I added extra things but for personal decision.

Arduino code:

//Constants(won't change), variables (change):
const int buttonPin = 8;   
const int flexPin = A0; //pin A0 to read flex sensor - analog input
const int speakerPin = 13;
bool takeAnalogReadings(uint16_t* p_numReadings = nullptr, uint16_t** p_analogVals = nullptr);

int buttonState = 0; //counts the number of button presses
int sensorValue = 0; //save flex sensor - analog value
int sensorMin = 20;  // minimum sensor value
int sensorMax = 600;   // maximum sensor value
 

void setup(){
  Serial.begin(9600);       //Begin serial communication
  

  // calibrate flex sensor during the first three seconds
  while (millis() < 1000) {
      sensorValue = analogRead(flexPin);
    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }
    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
 }

 //Serial.println("\nBegin\n");

}

void loop(){
  sensorValue = analogRead(flexPin);         //Read and save analog value from flex sensor
  buttonState = digitalRead(buttonPin);
  sensorValue = map (sensorValue, sensorMin, sensorMax, 10, 500);
  sensorValue = constrain(sensorValue, 10, 500);
  
    if (buttonState == HIGH){  
      uint16_t numReadings;
      uint16_t* analogVals;
      bool readingsDone = takeAnalogReadings(&numReadings, &analogVals);
      if (readingsDone)
      {
        Serial.println();
      }
    }
   }
  
bool takeAnalogReadings(uint16_t* p_numReadings, uint16_t** p_analogVals) {
  static const uint16_t NUM_READINGS = 9;
  static uint16_t i = 0; // index  
  static uint16_t analogVals[NUM_READINGS];
  
  bool bufferIsFull = false; // set to true each time NUM_READINGS have been taken
  analogVals[i] = sensorValue;
  Serial.print(analogVals[i]); Serial.print(",");
  delay(1000);
  i++;
  if (i >= NUM_READINGS)
    {
      bufferIsFull = true;
      i = 0; // reset to beginning of array, so you don't try to save readings outside of the bounds of the array
    }

  // Assign the user-passed-in pointers so that the user can retrieve the data if they so desire to do it this way
  if (p_numReadings != nullptr){
    *p_numReadings = NUM_READINGS;
  }
  else if (p_analogVals != nullptr){
    *p_analogVals = analogVals;
  }
  return bufferIsFull;
  }
  

and Processing code:

import processing.serial.*;

Serial myPort; //Serial port
String inString; //Input string from serial port
int lf = 10; //ASCII linefeed
float x1, x2, x3, x4, x5, x6, x7, x8, x9;
boolean dataReceived;



void setup() {
  //printArray(Serial.list());
  String portName = Serial.list()[1]; 
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil(lf);
  
  size (800,800);
  delay(2000);
  
  x1 = 0;
  x2 = 0;
  x3 = 0;
  x4 = 0;
  x5 = 0;
  x6 = 0;
  x7 = 0;
  x8 = 0;
  x9 = 0;
  
  dataReceived = false;
  
  fill(random(255), random(255), random(255), 100);
  
}
 
 
 
void draw() {
    if (dataReceived){
      //println (x1, x2, x3, x4, x5, x6, x7, x8, x9);
      dataReceived = false;
    }
    background (240);
    noStroke();
    
    ellipse(200, 200, x1, x1);
    ellipse(400, 200, x2, x2);
    ellipse(600, 200, x3, x3);
    ellipse(200, 400, x4, x4);
    ellipse(400, 400, x5, x5);
    ellipse(600, 400, x6, x6);
    ellipse(200, 600, x7, x7);
    ellipse(400, 600, x8, x8);
    ellipse(600, 600, x9, x9);
      
     
}


void serialEvent(Serial p){
  inString = p.readString();
  if (inString!= null) {
    dataReceived = true;
    println(inString);
    //split values with comma
    String[] list = split(inString, ',');
    //if (list.length > 1){
    //assign values in processing

    x1 = (float(list[0]) * 1.3);
    x2 = (float(list[1]) * 1.3);
    x3 = (float(list[2]) * 1.3);
    x4 = (float(list[3]) * 1.3);
    x5 = (float(list[4]) * 1.3);
    x6 = (float(list[5]) * 1.3);
    x7 = (float(list[6]) * 1.3);
    x8 = (float(list[7]) * 1.3);
    //trim the last element in list to remove the line feed character
    x9 = (int(trim(list[8])) * 1.3);
    
    //printArray(list);
    //}
  }
}

Thanks again!

1 Like