Hello. I am new to Aurduino. I started with basic and moved to c++ and the first project I do had to be the most complicated thing I could think of. If part of my project looks like something you wrote, Thanks for the help this far.It isn't just all copy and paste though I have spent hours a day for about 4 months going crazy and it has come to this. Here is a condensed version of my project and my issue.
I am talking to a phone app, that sends to a hc-06 bluetooth a file name, for instance "the cow jumped over the moon". My program adds ".txt" to the end of that, opens the file from an sdcard, then reads the file which contains 2 servo positions written like this " 90a,180b " . Then the program is supposed to read that info and make servoa go one way and servob go another way and turn on a laser at the end. eventually I plan on putting 320 lets say 8 letter 8 word files on the sdcard with different servo positions in each file. The first part of the program works, the second half works but I cant get the second half to read the contents of the file. I can manually enter the servo positions into the serial monitor and the second half of the program works. Sorry if my program is condensed and has little comments. I will comment in where i think the problem starts. Your help will keep me from going crazy, thanks.
#include <Servo.h>
#include <SPI.h> // needs to send a responce to the servos and turn on the laser.
#include "SdFat.h" //
#include <SoftwareSerial.h>
SdFat SD;
String value="";
String txt;
String theFile;
String readString;
int x;
int TxD = 3;
int RxD = 10;
int servoposition;
SoftwareSerial bluetooth(TxD, RxD);
File myFile;
Servo myservoa, myservob;
void setup() {
pinMode(8,OUTPUT);
myservoa.attach(6);
myservob.attach(9);
String txt= ".txt";
bluetooth.begin(9600);
Serial.begin(9600);
(SD.begin(4));
Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
Serial.println();
}
void loop() {
bluetooth.begin(9600);
String txt= ".txt";
if (bluetooth.available())
value = bluetooth.readString();
{
if(value==0){
theFile=String(value);
}
else{
theFile= String(value)+(txt);
{
//Serial.println(theFile);
Serial.readString();
while (!Serial);
if (!SD.begin(4)) {
}
myFile = SD.open(theFile);
if (myFile) {
//Serial.println("theFile:");
while (myFile.available())
Serial.write(myFile.read());
// This is where the program is seperated.-------------------------
//expect single strings like 70a, or 180b, or combined like 180b,70a,
if (Serial.available()) {
char c = (Serial.read()); //gets one byte from serial buffer
//Serial.println(theFile);
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
Serial.println(int n);
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
Serial.println(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
// myFile.close();
}
} // no laser attached yet
}
}
}
(MOD EDIT)