0

My program does not read the contents of the memory card, in this case the file "objects.txt". What I wanted to do was: the program reads the objects contained in the text file and allocates the table, then organizes the table alphabetically.

Something is escaping me. Another thing, how do I put a function recognizing if the user pressed a button and if pressed, the program continues?

#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
int x = 0;
String files, memo, memo1;
int sizef = 0;
int sizef1 = 0;
int sizef2 = 0;
int y = 0;
int z = 0;
struct objeto_celeste_s {
 char nome[20];
 float ar;
 float dec;
};
typedef struct objeto_celeste_s objeto_celeste;
char * tab1 [6];
int myCompareFunction (const void * arg1, const void * arg2) {
 return strcmp (*(char **) arg1, *(char **) arg2);
}
File root;
File file;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
char * list[] = {"1.Modo Normal ", "2.Modo Teste ", "3.Modo GoTo ", "4.Modo Manual "};
// read the buttons
int read_LCD_buttons() {
 adc_key_in = analogRead(0);
 if (adc_key_in > 1000) return btnNONE;
 if (adc_key_in < 50) return btnRIGHT;
 if (adc_key_in < 250) return btnUP;
 if (adc_key_in < 400) return btnDOWN;
 if (adc_key_in < 450) return btnLEFT;
 if (adc_key_in < 650) return btnSELECT;
 return btnNONE; // when all others fail, return this...
}
void setup() {
 Serial.begin(9600);
 Serial1.begin(9600);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for native USB port only
 }
 Serial.println ("Welcome!");
 Serial.println ("Selecione um modo no lcd.");
 lcd.begin (16, 2);
 lcd.setCursor (0, 0);
 lcd.print (" Selecione um");
 lcd.setCursor (0, 1);
 lcd.print (" modo ");
 delay (1000);
 lcd.clear ();
 lcd.setCursor(0, 0);
 lcd.print(">");
 lcd.println(list[0]);
 lcd.setCursor(0, 1);
 lcd.print (" ");
 lcd.println(list[1]);
 if (!SD.begin(4)) {
 Serial.println("initialization failed!");
 while (1);
 }
}
void loop() {
 lcd_key = read_LCD_buttons(); // read the buttons
 switch (lcd_key) {
 // depending on which button was pushed, we perform an action
 case btnRIGHT:
 break;
 case btnLEFT:
 break;
 case btnUP:
 if (x == 1) {
 x--;
 lcd.clear ();
 lcd.setCursor(0, 0);
 lcd.print(">");
 lcd.println(list[0]);
 lcd.setCursor(0, 1);
 lcd.print (" ");
 lcd.println(list[1]);
 }
 break;
 if (x == 3) {
 x--;
 lcd.clear ();
 lcd.setCursor(0, 0);
 lcd.print(">");
 lcd.println(list[2]);
 lcd.setCursor(0, 1);
 lcd.print (" ");
 lcd.println(list[3]);
 }
 break;
 case btnDOWN:
 if (x == 0) {
 x++;
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print(">");
 lcd.println(list[1]);
 lcd.setCursor(0, 1);
 lcd.println(" ");
 }
 break;
 if (x == 2) {
 x++;
 lcd.clear ();
 lcd.setCursor(0, 0);
 lcd.print(">");
 lcd.println(list[3]);
 lcd.setCursor(0, 1);
 lcd.println(" ");
 }
 break;
 case btnSELECT:
 if (x == 0) {
 lcd.clear ();
 lcd.print ("Modo Normal");
 Serial.println ("Selecionou o modo normal!");
 delay (1000);
 if (SD.exists("objectos.txt")) {
 Serial.println("O ficheiro 'objetos' foi encontrado!");
 lcd.clear ();
 lcd.setCursor (0, 0);
 lcd.print ("Modo Normal");
 delay (1000);
 file = SD.open("objectos.txt", FILE_READ);
 if (file) {
 while (file.available()) {
 String line = file.readStringUntil('\n');
 sizef++;
 }
 } else {
 //se houver erro a abrir o ficheiro
 Serial.println("Erro a abrir o ficheiro 'objectos.txt'.");
 }
 objeto_celeste *tab = (objeto_celeste *)malloc((sizef) * sizeof(objeto_celeste));
 Serial.print ("Contados ");
 Serial.print (sizef);
 Serial.println (" objetos no ficheiro objectos.txt.");
 if (tab == NULL) {
 lcd.clear ();
 lcd.print ("Erro a alocar memoria");
 Serial.println ("Erro a alocar memória");
 while (1) { };
 } else {
 Serial.print ("Memória alocada para ");
 Serial.print (sizef);
 Serial.println (" objectos.");
 lcd.setCursor (0, 1);
 lcd.print ("Alocados ");
 lcd.print (sizef);
 lcd.print (" obj");
 }
 file.close();
 char * line1 = (char*) malloc(15);
 file = SD.open("objectos.txt", FILE_READ);
 if (file) {
 Serial.println ("Carregando informação para a memória:");
 while (file.available()) {
 if (file.read() == '\n') {
 char * justafun = strtok (line1, " "); //divide em tokens
 while (justafun != NULL) {
 if (y == 0) {
 strcpy (tab[sizef2].nome, justafun);
 Serial.print ("[");
 Serial.print (sizef2);
 Serial.print ("] ");
 Serial.print(tab[sizef2].nome);
 Serial.print (" ");
 y++;
 }
 if (y == 1) {
 tab[sizef2].ar = atof (justafun);
 Serial.print (tab[sizef2].ar);
 Serial.print (" ");
 y++;
 }
 if (y == 2) {
 tab[sizef2].dec = atof (justafun);
 Serial.println (tab[sizef2].dec);
 sizef2++;
 y = 0;
 }
 justafun = strtok (NULL, " ");
 }
 }
 }
 }
 file.close ();
 delay (1500);
 lcd.clear ();
 Serial.println ("Proceda ao alinhamento manual do telescópio.");
 lcd.setCursor (0, 0);
 lcd.print ("Proceda ao ");
 lcd.setCursor (0, 1);
 lcd.print ("Alinhamento man.");
 Serial.println ("Depois de o alinhamento estar concluido, pressione o botão Select para prosseguir o programa.");
 //colocar aqui função para reconhecer quando pressiona select
 if (Serial1.available()) {
 int inByte = "V"; //Se o alinhamento está concluido, 0 para não e 1 para sim
 Serial1.write (inByte);
 }
 while (!Serial1) {
 if (Serial1.available()) {
 int inByte = "V";
 Serial1.write (inByte);
 int inByte1 = "e";
 Serial1.write (inByte1);
 }
 }
 delay (1000);
 Serial.println ("Tabela ordenada alfabeticamente:");
 qsort (tab->nome, sizef, sizeof (char *), myCompareFunction);
 for (int i = 0; i < sizef; i++)
 Serial.println (tab[i].nome);
 delay (1000);
 lcd.clear ();
 lcd.print ("Carregue Select");
 lcd.setCursor (0, 1);
 lcd.print ("Para modo manual");
 Serial.println ("Carregue em Select para ir para o modo manual e assim continuar com o programa.");
 } else {
 Serial.println ("O ficheiro 'objetos' não existe.");
 lcd.clear ();
 lcd.print ("Erro nao encontrado");
 delay (2000);
 while (1) { };
 }
 }
 if (x == 1) {
 lcd.clear ();
 lcd.print ("Modo Teste");
 Serial.println ("Selecionou o modo teste!");
 delay (2000);
 if (SD.exists("teste.txt")) {
 Serial.println("O ficheiro 'teste.txt' foi encontrado!");
 lcd.clear ();
 lcd.setCursor (0, 0);
 lcd.print ("Modo Teste");
 lcd.setCursor (0, 1);
 lcd.print ("Ficheiro encontrado");
 delay (2000);
 file = SD.open("teste.txt", FILE_READ);
 if (file) {
 while (file.available()) {//ler o ficheiro
 String line = file.readStringUntil('\n');
 sizef++;
 }
 files = "Contados " + String (sizef) + " objetos no ficheiro teste.txt.";
 Serial.println (files);
 objeto_celeste *tab = (objeto_celeste *)malloc((sizef) * sizeof(objeto_celeste));
 memo = "Alocados " + String (sizef) + " obj ";
 memo1 = "Memória alocada para " + String (sizef) + " objetos.";
 if (tab == NULL) {
 lcd.clear ();
 lcd.print ("Erro alocar memoria");
 Serial.println ("Erro a alocar memória");
 while (1) { };
 } else {
 Serial.println (memo1);
 lcd.setCursor (0, 1);
 lcd.print (memo);
 }
 file.close();
 } else {
 //se houver erro a abrir o ficheiro
 Serial.println("Erro a abrir o ficheiro 'teste.txt'.");
 }
 } else {
 Serial.println ("O ficheiro 'teste.txt' não foi encontrado.");
 lcd.clear ();
 lcd.print ("Erro nao encontrado");
 delay (2000);
 while (1) { };
 }
 }
 break;
 case btnNONE:
 break;
 }
 delay (200);
}
dda
1,5951 gold badge12 silver badges17 bronze badges
asked Apr 18, 2018 at 16:42
11
  • You are never using anything of the content oft objects.txt. in the first read you only count the lines in the file and in the second read loop you only do something, when a newline is read, nothing else Commented Apr 18, 2018 at 16:53
  • Thank you, so in the second read i should do what? Commented Apr 18, 2018 at 17:02
  • How about actually reading the lines you counted previously and then extracting the data you need from them. Commented Apr 18, 2018 at 17:03
  • How i do that? :/ Commented Apr 18, 2018 at 17:06
  • 1
    @1manonearth, your code is very poorly indented. ... most of it is just a blob of text ... it is difficult to follow the program flow because of the way it is. ... it requires reformatting before it can be looked at .... most people do not have the time to format your code so that it can be debugged .... you may get a good answer if you spend some time and clean up your code Commented Apr 19, 2018 at 2:11

1 Answer 1

1

I would say you can't add a button that restarts/continues the program. Because of all the delay(...) in your code. The arduino chip cant read your input and your button, while it is in a delay mode and hence no reading from the button. Read more about non-blocking delay

You need to keep the main loop method ongoing so the microcontroller-cpu can do it's work. Instead of waiting in a delay loop, doing nothing.

So don't use any delay, they are good for beginner to jump start into Arduino.

No delay

answered Apr 25, 2018 at 21:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.