I'm currently doing a project regarding translating, and I need to read the txt file in a SD card and then translate it, but the translation takes time and the Arduino reads the txt files too fast, so I think that the solution should be a delay after reading each line but I could not figure out how to do it, could anyone please help me as soon as possible, this is for school
Rick_Sanches:
...I think that the solution should be a delay after reading each line...
Sort of, yes. You should complete the translation of each line before you proceed to the next.
How are you trying to do this (i.e. where is your current code)?
I'm using one Arduino to read the SD card and send the text by Serial to the one that does the translating so
#include <SD.h>
File myFile;
void setup() {
Serial.begin(4800);//78492
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
return;
}
myFile = SD.open("test.txt");
}
void loop() {
while (myFile.available()) {
delay(2000);
Serial.print(myFile.readStringUntil('\n'));
}
}
If you want a fixed delay() between readings simply insert it after sending the data to the second Arduino. It would be much better to have the second Arduino signal the first one when it has translated a line of text.
Better still, do it all on one Arduino.
What software are you using to do the translation ?
UKHeliBob:
If you want a fixed delay() between readings simply insert it after sending the data to the second Arduino. It would be much better to have the second Arduino signal the first one when it has translated a line of text.
kind of like sending the translated message back?
pseudo...
void loop()
{
if (readyForTranslation)
{
sendMessageForTranslation(message, Serial1)); // message is a pointer to the next message to send.
readyForTranslation = false;
}
if (char* translatedMessage = getTranslation(Serial1))
{
Serial.println(translatedMessage);
message = getNextMessage(); // message is a pointer to the next message to send.
readyForTranslation = true;
}
}
If your translation process takes long, there is no need for delay; the process already does the delaying.
void loop()
{
while (myFile.available())
{
Serial.print(translate(myFile.readStringUntil('\n')));
}
}
The Arduino will only read the next line from the SD card when the translate function is finished.
The Arduino will only read the next line from the SD card when the translate function is finished
The snag is that the translation is done on a second Arduino, but we don't know why.
UKHeliBob:
The snag is that the translation is done on a second Arduino, but we don't know why.
Missed that. Anyway, translate() can happily wait for the reply
When (if) we find out how and why the translation is being done on a second Arduino we may be able to offer more solutions
Well, I'm using another code to translate the data into special characters and it will display each characters on some LEDS for 2 seconds. And I've tried using one Arduino but it creates more problems than it actually solves.
I think that all I need is the txt file be read slowly, any suggestions?
I think that all I need is the txt file be read slowly, any suggestions?
Use millis() for timing the interval between reading each line from the SD and you can do whatever you want in the meantime. Are you familiar with using millis() for timing ? If not then read Using millis() for timing. A beginners guide and Several things at the same time and look at the BlinkWithoutDelay example in the IDE.
I still think that you should consider doing all of this on one Arduino. What problems did you have ?
If you wrote the translation code then if you use a second Arduino it would be preferable for it to signal the first one when translation is finished so that another line can be read and sent.
Could you please show me how to use millis() with readStringUntil() I couldn't figure it out.
And using only one Arduino creates conflict (I guess) between the translation and the text reading, using 2 Arduinos made things alot easier, I could use simpler codes but with better results
You're on the wrong track if you try to combine millis() with readStringUntil().
See reply #4 for the approach.
You read a line, send it to the translator and wait for the translator to send a translated message back.
Only after you have received the translated message back, you read the next line and send it to the translator and wait again for the translated message to come back.
If that whole process is still too fast, you can consider to slow it down using delay or a millis() based approach.
Could you please show me how to use millis() with readStringUntil()
As pointed out it is not a matter of using millis() with readStringUntil(). Using millis() is a general purpose timing method like this
unsigned long currentTime;
unsigned long period = 5000;
unsigned long waitStartTime = currentTime + period;
void setup()
{
Serial.begin(115200);
Serial.println("Code to open the file goes here\n");
}
void loop()
{
currentTime = millis();
if (currentTime - waitStartTime >= period) //waited long enough
{
Serial.println("Code here to read a line from the file");
Serial.println("Code here to send the file to other Arduino\n");
waitStartTime = currentTime;
}
}
Thanks alot for the help, I understand the millis() now, but my code sends the data right after it reads from the SD, it reads the file and everything it read gets print to Serial, and everything on the Serial gets sent straight to the other Arduino so I still could not figure out how to delay the process
So why not post your code as it is now
That's more than likely because you don't wait for the translated message before sending the next line.
while (myFile.available()) {
delay(2000);
Serial.print(myFile.readStringUntil());
Everything that get printed to Serial goes to the other Arduino. I want to pause the process of readStringUntil() and I still could not use millis to do it
Post the complete program
Rick_Sanches:
while (myFile.available()) {
delay(2000);
Serial.print(myFile.readStringUntil());Everything that get printed to Serial goes to the other Arduino. I want to pause the process of readStringUntil() and I still could not use millis to do it
It was suggested to wait for the reply.
while (myFile.available())
{
// read and send to other arduino
Serial.print(myFile.readStringUntil());
// wait for reply
while(Serial.available() == 0)
{
}
// delay a lot
delay(10000); // hopefully enough time to receive everything
// read everything
...
...
// do something with the received data
...
...
}
This is just to give you the idea; I would never implement a code like this for myself but it might give you an understanding how to approach. You can add a timeout in the while loop if needed.