Vl6180x and Ardunio Uno - writting serial data to a textfile

Good morning
i am wondering if anyone could help me with this little code, or point me in the direction on why it is not working.

singular function: Reading range from a lidar, and save it into a text document, to be my Y variable in a pointcloud mesh.

i am trying to use processing to do the processing of the data comming from the serial. and i am able to read the data from the Vl6180x sensor in Processing, so it it receiving data.
i have gotten it to create the textfile, however it is not writing anything in it.

i have checked the forums around on the web, however codes that is working for others, still won’t write into my textfile, what have i done wrong? :slight_smile:

Ardunio Uno code

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

Adafruit_VL6180X vl = Adafruit_VL6180X();

void setup() {

  // wait for serial port to open on native usb devices
  while (!Serial) {
  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  Serial.println("Sensor found!");

void loop() {
  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);


and my Processing code

import processing.serial.*;
Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
PrintWriter output;

void setup() //konfigurasjonen i programmet
//Open port 1, 115200
  String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 115200);
   // Create a new file in the sketch directory
  output = createWriter("data2.txt");
void draw() //Loopen i programmet
  if ( myPort.available() > 0)
  {  // If data is available,
  val = myPort.readStringUntil('\n');         // read it and store it in val
     output.println(val); // Write the data to the textfile
println(val); //console print to verify information

void keyPressed() {
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  exit(); // Stops the program

I was just working on a similar problem yesterday, and so coded up a script to save a file.
This writes to the data folder only and you can choose whether to append or overwrite, it also checks if a file is present and if not creates one.

import java.io.BufferedWriter;
import java.io.FileWriter;

fileOutput output;
String loc = "positions.txt";
void setup() {
  // set boolean to true to append file otherwise it will overwrite
  //output = new fileOutput(loc);
  output = new fileOutput(loc,true);
    // Create a new file in the sketch directory

void draw() {
  point(mouseX, mouseY);

class fileOutput{
    PrintWriter output;
    boolean save,onMouseUp,mdown;
    int counter;
    File file;
    String location;
  fileOutput(String location){
    this.location = location;
  void saveData(){
      checkFile( location);
        output.println(mouseX + ",+ " + mouseY);
  void saveTo(){
      output.flush(); // Writes the remaining data to the file
      output.close(); // Finishes the file
      //exit(); // Stops the program
  boolean onMouseUp(){
    boolean k = false;
    if (pos()&&mousePressed&&!onMouseUp){
      onMouseUp = true;
      k = false;
    }else if(onMouseUp&&!mousePressed){
      k = true;
      onMouseUp = false;
      counter ++;
    return k;
  boolean mdown(){
    boolean k= false;
    if(mdown)k = false;
      mdown = true;
      k = true;
    if(!mousePressed)mdown = false;    
    return k;
  boolean pos(){
    return mouseX>0&&mouseX<width&&mouseY>0&&mouseY<height;
  void checkFile(String location){
     file = dataFile(location);
      if (!file.exists()) {
        output = createWriter("/data/"+location);
      String filePath = file.getPath();
      filePath = filePath.substring(3,filePath.length());
      filePath = "/"+filePath.replace("\\","/");
      file = new File(filePath);
      try {
        FileWriter fw = new FileWriter(file, true);///true = append
        BufferedWriter bw = new BufferedWriter(fw);
        output = new PrintWriter(bw);

       //add code here if you want to separate each write cycle;
        println("hello world!!!!! j'ajoute something au txt");
        output.println("hello world!!!!! j'ajoute something au txt");
      catch(IOException ioe) {
        System.out.println("Exception ");

add more saveData functions if you need other parameters, and of course change the save conditions to suit your needs.

Did this work for you?