Do you mean that you control the number of characters in each record so that the total number of entries is equal to the total number or charters divided by the number of characters per record? If so, I am still unsure of how to delete specific entries once the total number of entries gets to a desired number. Why is it bad to use myFile.print()? Printing the sensor values directly to the sd card keeps them the same number of characters each entry, so I am confused why I should not do it this way. Check out the files attached to my last post, they show that each entry is the same number of characters. Here is my poorly commented code that records the humidity and temperature from a sensor to the sd card:
#include <SPI.h>
#include <SD.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#include <Wire.h>
#include <HIH61XX.h>
// Library only supports hardware SPI at this time
// Connect SCLK to UNO Digital #13 (Hardware SPI clock)
// Connect MISO to UNO Digital #12 (Hardware SPI MISO)
// Connect MOSI to UNO Digital #11 (Hardware SPI MOSI)
#define RA8875_INT 3
#define RA8875_CS 10
#define RA8875_RESET 9
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;
HIH61XX hih(0x27);
File myFile;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;
float x = 0;
float y = 0;
int temp = 0;
char string[8] = "0.00000";
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.begin(9600);
Wire.begin();
Serial.println("RA8875 start");
/* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
if (!tft.begin(RA8875_800x480)) {
Serial.println("RA8875 Not Found!");
while (1);
}
Serial.println("Found RA8875");
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(SS, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
pinMode(RA8875_INT, INPUT);
digitalWrite(RA8875_INT, HIGH);
tft.touchEnable(true);
tft.textMode();
}
void loop()
{
// start the sensor
hih.start();
// request an update of the humidity and temperature
hih.update();
Serial.print("Humidity: ");
Serial.print(hih.humidity(), 5);
Serial.print(" RH (");
Serial.print(hih.humidity_Raw());
Serial.println(")");
Serial.print("Temperature: ");
Serial.print(hih.temperature(), 5);
Serial.println(" C (");
Serial.print(hih.temperature_Raw());
Serial.println(")");
myFile = SD.open("HUMIDITY.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.println(hih.humidity(), 3);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening HUMIDITY.txt");
}
myFile = SD.open("TEMP.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.println(hih.temperature()*1.8+32);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening TEMP.txt");
}
//open the file for reading:
myFile = SD.open("x1.txt");
if (myFile) {
Serial.println("x1.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
x = (myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening x1.txt");
}
myFile = SD.open("y1.txt");
if (myFile) {
Serial.println("y1.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
y = (myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening y1.txt");
}
float xScale = 1024.0F/tft.width();
float yScale = 1024.0F/tft.height();
/* Wait around for touch events */
if (! digitalRead(RA8875_INT))
{
if (tft.touched())
{
tft.touchRead(&tx, &ty);
/* Draw a circle */
tft.fillCircle((uint16_t)(tx/xScale), (uint16_t)(ty/yScale), 4, RA8875_WHITE);
}
}
Serial.println(x);
Serial.println(y);
tft.fillCircle(x, y, 4, RA8875_GREEN);
tft.textSetCursor(10, 10);
tft.textColor(RA8875_WHITE, RA8875_RED);
tft.textWrite("Temperature: ");
tft.textWrite(string);
delay (1000);
}