Writing Array Line by Line in a .txt file

For my project I am collecting diameters on one board and sending them over to another to do temp compensation calculations. On the secondary board I also have a SD module plugged in to then write each individual diameter on a line in a .txt file. This will then be used later down the line to upload into excel. Can someone point me in the right direction to go about this. My current code is:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#include <SD.h>
#include <ezButton.h>

#define PIN_SPI_CS 53
File file;

ezButton button (6);
unsigned long lastCount = 0;
unsigned long count = 0;

float dL = 0;
float dT = 0;
float a1 = 0;
float a2 = 0;
float a3 = 0;
float a4 = 0;
float a5 = 0;
float diaAdjusted = 0;
float TF = 0; //Temp of part
float RF = 0; //Room Temp
float diameterin;
int i = 0;
char myData[6] = {0};
int Length[10];
float Diameter[6];
const int ledPin1 = 4; //Material 1
const int ledPin2 = 9; //Material 2
const int ledPin3 = 8; //Material 3
const int ledPin4 = 7; //Material 4
const int ledPin5 = 5; //Material 5

LiquidCrystal_I2C lcd(0x27, 20, 21);
dht DHT;
#define DHT11_PIN 3
void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
  lcd.init();
  lcd.backlight();

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);

  button.setDebounceTime(50);
  button.setCountMode(COUNT_FALLING);

  if (!SD.begin(PIN_SPI_CS)) {
    Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
    while (1);
  }
  SD.remove("Diameter.txt");
  file = SD.open("Diameter.txt", FILE_WRITE);

 
}

void loop() {
   if (Serial2.available() > 0)
  {
    byte m = Serial2.readBytesUntil('\n', myData, 6);
    myData[m] = '\0';
    diameterin = atof(myData);
    //Serial.println(diameterin,4);
    memset(myData, 0, 6);
  }
  
  button.loop();
  count = button.getCount();
  if (count != lastCount) {
    //Serial.println(count);
    int countIn6 = count % 6 + 1;
    switch (countIn6) {

      case 1:
        //Serial.println("button 1");
        digitalWrite(ledPin1, HIGH);
        dT = TF - RF;
        dL = diameterin * dT * a1;
        diaAdjusted = diameterin - dL;
        lcd.clear();
        lcd.setCursor(4, 0);
        lcd.print("Material 1");
        lcd.setCursor(0, 1);
        lcd.print("Dia(in)= ");
        lcd.setCursor(9, 1);
        lcd.print(diaAdjusted, 4);

        break;

      case 2:
        //Serial.println("button 2");
        digitalWrite(ledPin2, HIGH);
        dT = TF - RF;
        dL = diameterin * dT * a2;
        diaAdjusted = diameterin - dL;
        lcd.clear();
        lcd.setCursor(4, 0);
        lcd.print("Material 2");
        lcd.setCursor(0, 1);
        lcd.print("Dia(in)= ");
        lcd.setCursor(8, 1);
        lcd.print(diaAdjusted, 4);

        break;

      case 3:
        //Serial.println("button 3");
        digitalWrite(ledPin3, HIGH);
        dT = TF - RF;
        dL = diameterin * dT * a3;
        diaAdjusted = diameterin - dL;
        lcd.clear();
        lcd.setCursor(4, 0);
        lcd.print("Material 3");
        lcd.setCursor(0, 1);
        lcd.print("Dia(in)= ");
        lcd.setCursor(8, 1);
        lcd.print(diaAdjusted, 4);

        break;

      case 4:
        //Serial.println("button 4");
        digitalWrite(ledPin4, HIGH);
        dT = TF - RF;
        dL = diameterin * dT * a4;
        diaAdjusted = diameterin - dL;
        lcd.clear();
        lcd.setCursor(4, 0);
        lcd.print("Material 4");
        lcd.setCursor(0, 1);
        lcd.print("Dia(in)= ");
        lcd.setCursor(8, 1);
        lcd.print(diaAdjusted, 4);

        break;

      case 5:
        //Serial.println("button 5");
        digitalWrite(ledPin5, HIGH);
        dT = TF - RF;
        dL = diameterin * dT * a5;
        diaAdjusted = diameterin - dL;
        lcd.clear();
        lcd.setCursor(4, 0);
        lcd.print("Material 5");
        lcd.setCursor(0, 1);
        lcd.print("Dia(in)= ");
        lcd.setCursor(8, 1);
        lcd.print(diaAdjusted, 4);

        break;

      case 6:
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        digitalWrite(ledPin4, LOW);
        digitalWrite(ledPin5, LOW);
    }
    lastCount = count;
  }
    for (int i = 0; i < 6; i++); {
      if (diameterin > 0) {
        Diameter[i] = diameterin;
        if(file){
          file.write(Diameter[6]);
        }
        else{
          Serial.print(F("SDCard: error on opening file Diameter.txt"));
        }
        i += 1;
      }
      else {
        i == i;
      }
    }
      file.close();
    
    if (Diameter[1] > 0) {
      if (Diameter[2] > 0) {
        if (Diameter[3] > 0) {
          if (Diameter[4] > 0) {
            if (Diameter[5] > 0) {
              Serial.println("1st: ");
              Serial.println(Diameter[1]);
              Serial.println("2nd: ");
              Serial.println(Diameter[2]);
              Serial.println("3rd: ");
              Serial.println(Diameter[3]);
              Serial.println("4th: ");
              Serial.println(Diameter[4]);
              Serial.println("5th: ");
              Serial.println(Diameter[5]);
            }
          }
        }
      }
    }
    
    
    int chk = DHT.read11(DHT11_PIN);
    TF = (DHT.temperature * 1.8) + 32;
    lcd.setCursor(0, 2);
    lcd.print("T= ");
    lcd.setCursor(4, 2);
    lcd.print(TF);
    //Serial.println(TF);
   

  }

What is your problem, exactly?

Currently, .txt file is being created but nothing is being written to it. I have the write command in the for loop and believe that might be the issue.

What do you try to print to the file?

Diameter[6] is a non-existent element of the array, arrays in the C counts from zero, you array contains elements 0-5 only

I had updated it to:

file .write(Diameter[i]);

to attempt to print each value as the array indexes.

In addition, your code contains a bunch of errors and absurdities.

In this loop you always put the same value to the all six elements of Diameter array:

Is it what you expected?
(also note the semicolon after the for initialisation)

What it is?

Your switch operator has a six almost the same case blocks. You could be make the code much shorter and understandable, if decide to use the arrays.

Do you want to write the bytes of a float or to print a text string that represents the float?
SD - write() - Arduino Reference versus
SD - println() - Arduino Reference

According to the title, print .txt files.

1 Like

I was thinking that since my other Mega board is always sending diameters from the sensor it would also change the variable "diameterin" as the code continues to loop. The switch operator I was messing around with today in order to do 5 different calculations depending on the material that is being measured and the 6th case is to turn all of the lights off.

I want to write the bytes of a float to a .txt file because the plan is to then convert the .txt files to excel. So, I am attempting to have all of my diameters recorded in one column and then eventually I will have my lengths in another column.

As far I see all five calculations are the same except of parameter a1(a2, a3...) which can be easily put in array, indexed by case number. The same about ledPin1 - ledPin5 pins.

==============================

To convert file to the excel you need to print your floats as strings rather than write as bytes.

1 Like

So, will I need to use the file.print() command?

I think so
And don't forget to fix errors in the code.

1 Like

The code has been running for me, so I haven't noticed any errors. I will go back and make changes, but right now I am attempting to get different things functioning.

Do you not consider in that this line has a error?

or in that the for loop makes all your Diameters the same?

1 Like

It might be a lot easier to send the data directly from the Arduino to Excel, using PLX-DAQ.

I'm attempting to index the for loop in order to fill an empty array with new diameters that are being sent from the master Mega. Then I was hoping to index the array to print to the text as well. I have made the change from Diameter[6] to Diameter[i].

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.