Turn ON LED when the LAST LINE of .TXT file is reached.

This is a continuation of my project here Display text by line from SDCARD - Project Guidance - Arduino Forum

i want the LED to turn on whenever the last line of the text file in the sd card is reach..

example:

abcd1
abcd2
abcd3 --- > LED ON

so far this is part of the code that i have.. and this only displays line by line on a button press.. how will the program know that its the end of the line of the text file so it can digitalWrite led pin HIGH.?

String b="";
int counter=0;
int usedcounter=0;


void get_license() { 
char line[25];
int n;
SdFile rdfile("license.txt", O_READ);
if (!rdfile.isOpen()) {
error("error reading license.txt file");}
Serial.println(F("Selecting license number."));
while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
if (line[n - 1] == '\n') {
counter++;
if (counter == usedcounter){
b=line;} }}
counter = 0;
Serial.println(b);
rdfile.close(); }

help?

String b = "";
int counter = 0;
int usedcounter = 0;

void get_license()
{
  char line[25];
  int n;
  SdFile rdfile("license.txt", O_READ);
  if (!rdfile.isOpen())
  {
    error("error reading license.txt file");
  }
  Serial.println(F("Selecting license number."));
  while ((n = rdfile.fgets(line, sizeof(line))) > 0)
  {
    if (line[n - 1] == '\n')
    {
      counter++;
      if (counter == usedcounter)
      {
        b = line;
      }
    }
  }
  counter = 0;
  Serial.println(b);
////////////////////////////// . TURN ON THE LED HERE
  rdfile.close();
}

johnwasser:

String b = "";

int counter = 0;
int usedcounter = 0;

void get_license()
{
  char line[25];
  int n;
  SdFile rdfile("license.txt", O_READ);
  if (!rdfile.isOpen())
  {
    error("error reading license.txt file");
  }
  Serial.println(F("Selecting license number."));
  while ((n = rdfile.fgets(line, sizeof(line))) > 0)
  {
    if (line[n - 1] == '\n')
    {
      counter++;
      if (counter == usedcounter)
      {
        b = line;
      }
    }
  }
  counter = 0;
  Serial.println(b);
////////////////////////////// . TURN ON THE LED HERE
  rdfile.close();
}

ok let me try this.. thanks !

johnwasser:

String b = "";

int counter = 0;
int usedcounter = 0;

void get_license()
{
  char line[25];
  int n;
  SdFile rdfile("license.txt", O_READ);
  if (!rdfile.isOpen())
  {
    error("error reading license.txt file");
  }
  Serial.println(F("Selecting license number."));
  while ((n = rdfile.fgets(line, sizeof(line))) > 0)
  {
    if (line[n - 1] == '\n')
    {
      counter++;
      if (counter == usedcounter)
      {
        b = line;
      }
    }
  }
  counter = 0;
  Serial.println(b);
////////////////////////////// . TURN ON THE LED HERE
  rdfile.close();
}

This does not detect if its the last license in my text file.. it just turn ON the led everytime the license number is displayed.. what i want to do is to turn ON the LED only if its the last licence number.. meaning the last line on the text file..

example:

abcd1
abcd2
abcd3 ----> last line ----> turn LED ON

Sorry, but you didn't include the part of your sketch where get_license() is called. How do YOU determine that you have reached the end of the file?

johnwasser:
Sorry, but you didn't include the part of your sketch where get_license() is called. How do YOU determine that you have reached the end of the file?

what part of the code do u need sir? sorry i just want to make it simple so i dont post the entire code.. let me know what you need..

Well i guess you can start with posting all your code :wink:

ok here are all the part of the code that has something to do with the text line.. i just need to turn the LED ON or maybe do something else.. whenever the last line of the text is reached or displayed on the LCD..

abcd1
abcd2
abcd3 ----> last line ----> turn LED ON

#include <SPI.h>
#include "SdFat.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);


String b="";
int counter=0;
int usedcounter=0;
int wrtcounter=0;
int buttoncounter=0;
int a=0;
int b1=2; //button1
int b2=3; //button2


void get_license() { 
char line[25];
int n;
SdFile rdfile("license.txt", O_READ);
if (!rdfile.isOpen()) {
error("error reading license.txt file");}
Serial.println(F("Selecting license number."));
while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
if (line[n - 1] == '\n') {
counter++;
if (counter == usedcounter){
b=line;} }}
counter = 0;
Serial.println(b);
rdfile.close(); }

void get_license_counter() { 
char line[25];
int n;
Serial.println(F("Getting License Counter"));
SdFile rdfile("batch1_license.txt", O_READ);
if (!rdfile.isOpen()) {
error("error batch1_license.txt");}
while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
if (line[n - 1] == '\n') {
usedcounter=0;
inString = line;
usedcounter = (inString.toInt());
} }
counter = 0;
rdfile.close(); }

void write_license_counter(){
Serial.println(F("Writing License Counter"));
myFile = SD.open("batch1_license.txt", FILE_WRITE);
if (myFile) {
wrtcounter = usedcounter+1;
myFile.println(wrtcounter);
myFile.close(); } }


void setup(void) { 

Serial.begin(9600);
SD.begin(10)
lcd.backlight();
lcd.begin(20, 4);
lcd.setCursor(0,0);
lcd.print(F(" Welcome "));



void loop() { 

int button1 = digitalRead(b1);
int button2 = digitalRead(b2); 

if (button1 == LOW) {
delay(500);
get_license_counte();
write_license_counter();
Serial.println(F("Generating License"));
lcd.setCursor(0, 0);
lcd.print(F("Generating License"));

get_license();
write_license_counter();
get_license_counter();

a= a - 1;

buttoncounter=2;
lcd.setCursor(0, 1);
lcd.print(F("Press CLEAR Button"));


if (buttoncounter == 0){
if (button3 == LOW) {
delay(500);
Serial.println(F("show code button pressed"));
lcd.clear(); 
if (a==0){
lcd.setCursor(0, 0);
lcd.print(F(" Press to generate again!"));

}
if(a = 1){
r1hr_get_counter();
r1hr_write_counter();
r1hr_get_code();
a= a - 1;
lcd.setCursor(0, 0);
lcd.print(F("Lisence:"));
lcd.setCursor(8, 0);
lcd.print(b);
delay(5000);
buttoncounter=2;
lcd.setCursor(0, 1);
lcd.print(F("Press again to CLEAR"));
}}

Bringamosa:
Well i guess you can start with posting all your code :wink:

This part gets the last line that has been displayed.. but i posted the rest of the code in the comment box.

void get_license_counter() {
char line[25];
int n;
Serial.println(F("Getting License Counter"));
SdFile rdfile("batch1_license.txt", O_READ);
if (!rdfile.isOpen()) {
error("error batch1_license.txt");}
while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
if (line[n - 1] == '\n') {
usedcounter=0;
inString = line;
usedcounter = (inString.toInt());
} }
counter = 0;
rdfile.close(); }

johnwasser:
Sorry, but you didn't include the part of your sketch where get_license() is called. How do YOU determine that you have reached the end of the file?

i posted the whole code

help anyone?

I can't get your "whole code" to compile. There are some undeclared variables and functions (like 'error()'".