Well first of all let me tell you what I want the whole thing to do. I have it connected to a matrix keypad and when any digits are entered, followed by a pound, it saves the string of numbers on to the sd card. That part I have it down. It is the first sketch. Second thing I want it to do is when I connect to it via serial and send a command I want it to print the saved data to the serial. I have a sketch that does that by just reading the sd every time it starts up. So for it to read on my command I just reset the Arduino with sending an R and it just reads the SD card again. I made sketch for that. I just want to combine both sketches to run together. I greatly appreciate any help I can get.
Im using a Adruino Pro with the SparkFun SD shield. I've tried to just combine them, it compiles without any errors but when I upload it nothing happens. Just bunch of hash symbols. And Ive narrowed it down to the line "ArduinoOutStream cout(Serial);" from the second sketch. When I add this line to the Keypad sketch it compiles and uploads but nothing works.
HERE IS THE FIRST SKETCH:
#include <SdFat.h>
#include <Keypad.h>
const int chipSelect = 8;
SdFat sd;
SdFile myFile;
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 7, 6, 5, 4 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 2, 3, 9 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char code[1024];
int index;
#define ledpin 13
void setup()
{
index = 0;
Serial.begin(9600);
if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
}
void writeToFile()
{
// open the file for write at end like the Native SD library
if (!myFile.open("codes.txt", O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening codes.txt for write failed");
}
code[index] = '\0';
myFile.println(code);
// close the file:
myFile.close();
Serial.println("done.");
}
void printDigits(byte digits){
}
void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
Serial.println(key);
switch (key)
{
case '#':
writeToFile();
index = 0;
Serial.print(code);
break;
default:
code[index] = key;
index++;
}
}
}
HERE IS THE SECOND ONE:
#include <SdFat.h>
const int chipSelect = 8;
int incomingByte;
SdFat sd;
ArduinoOutStream cout(Serial);
void setup(){
digitalWrite(10, HIGH); //We need to set it HIGH immediately on boot
pinMode(10,OUTPUT); //We can declare it an output ONLY AFTER it's HIGH
// (( HACKHACKHACKHACK ))
Serial.begin(9600); //So you can watch the time printed
char c;
Serial.begin(9600);
if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
// open file in current working directory
ifstream file("codes.TXT");
if (!file.is_open()) sd.errorHalt("open failed");
// copy the file to Serial
while ((c = file.get()) >= 0) cout << c;
cout << "Done" << endl;
}
void loop(){
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte == 'R')
{
Serial.println("RESET");
digitalWrite(10, LOW);
}
}
}
Moderator edit: I went back to this post, hit "modify", highlighted each section of the code, then hit the "#" icon on the toolbar.
I then hit "save".
It has taken me longer to write this comment than it did to make the amendement.