I have a function which reads text file from a sd card. What i want is, while it is still running, somehow i want to change the speed of the charecters read inside the function. How can i do it? It is similarly like when i press a button the music is fast forwarded. Similarly like that, here i want to increase the speed of the text file read by the function. Till now my program is this much:
#include <SPI.h>
#include <SD.h>
#include <stdio.h>
File myFile;
char line[50];
int i = 0;
unsigned long startTime, currentTime;
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
int count = 0;
int low = 1;
int t0 = 0;
bool startTime_on = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
startTime = millis();
pinMode(ledPin,OUTPUT);
pinMode(buttonPin,INPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
}
void file(int n) {
Serial.print("inside file");
Serial.println(n);
String file_n, last_s, first_s;
first_s = "test";
last_s = ".txt";
file_n = String();
file_n = 1 + first_s + n + last_s;
myFile = SD.open(file_n);
if (myFile) {
Serial.println(file_n);
// read from the file until there's nothing else in it:
while (myFile.available()) {
char c = myFile.read();
Serial.print(c);
if(c=='\n') {
Serial.println();
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// put your main code here, to run repeatedly:
currentTime = millis();
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH && currentTime-t0 > 100) {
digitalWrite(ledPin, HIGH);
if(low == 1) {
count++;
t0 = millis();
low = 0;
}
if(startTime_on == false) {
startTime = millis();
startTime_on = true;
}
}
else {
digitalWrite(ledPin, LOW);
low = 1;
}
if(currentTime-startTime > 1500) {
if(count==1) {
file(count);
}
else if(count==2) {
file(count);
}
else if(count==3) {
file(count);
}
else if(count!=0) {
Serial.println(count);
}
count = 0;
startTime_on = false;
}
}
Here file() is the function, in which the text file is being read by the arduino. I’m not getting any idea, how is this possible till now.