Yes, pin 10 is for ChipSelect, I got all my information from the adafruit tutorial.
There is still a Serial.print() in my interrupt routine, but it normally works.
Let me explain the two tests that I ran, because the problem arose from trying to read back the information stored in the SD Card.
-
My first tests (where I was unaware of a certain problem). This tested my intended code, and everything worked except the reading back of the data stored within the SD Card.
-
My second test excluded the I2C communication and simply tested the timeStamp() functionality. This test can be seen by the commented out line " //timeStamp("chicken", 2);" in the buttonCheck() function. When I tested it, it performed perfectly as intended, it stored data properly as well as retrieved the entire contents of the files that I requested. Doing this test, I noted that, while my previous test had not been able to retrieve the data stored within the SD Card, it was indeed storing data properly.
In my attempt to fix the first problem, you may see the 3 lines of greyed out code in the timeStamp() function where I close and reopen the SD File (I figured this was necessary as the initial file was opened with the FILE_WRITE argument, also it was this way in the examples). Instead of fixing this problem, it somehow destroys the entire program (even parts that come before it). Even though there are Serial.print() functions that are called before timeStamp() is ever called, no such things are printed to the Serial Monitor, this can be seen with the following outputs (Test 2 is not listed as it is not necessary):
Original Test 1 Output:
Initializing SD card...initialization done.
Button ON
Button OFF
1
Reading Over.
GD
Opening Status.txt
2
Reading Over.
GP
Opening Status.txt
Note: If I throw the switch again here the output is repeated, as it should be.
Proposed Revision to Test 1:
Initializing SD card...initialization done.
Button ON
Button OFF
Note: The device seems to be stuck somewhere, because if I throw the switch again, nothing happens.
As you can see, even the Serial.print() contained within the receiveEvent() work "normally", but when I thought I "fixed" the code, for some reason those Serial.print() statements cease to function, even though the timeStamp() function is not called until AFTER those print statements...what the heck. My code for the two Arduinos is as follows:
Arduino 1:
#include <Wire.h>
#include <SD.h>
#define pinSelect (10)
#define checker_button (2)
File Errors;
File Status;
int code = 3;
String stuff;
void setup() {
Wire.begin(1);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
pinMode(pinSelect, OUTPUT);
pinMode(checker_button, INPUT);
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
}
Serial.println("initialization done.");
}
byte x = 0;
//does nothing but wait for the switch to be thrown.
void loop() {
button_check();
delay(100);
}
//On receiving data, data is read, then a timeStamp command is sent
//timeStamp command is sent based on what code is received.
void receiveEvent(int howMany) {
stuff = "";
while(Wire.available()) {
code = Wire.read();
Serial.println(code);
}
delay(100);
Serial.println("Reading Over.");
if (code == 0) {
stuff = "Who knows";
Serial.println(stuff);
timeStamp(stuff, 0);
} else if (code == 1) {
stuff = "GD";
Serial.println(stuff);
timeStamp(stuff, 1);
} else if (code == 2) {
stuff = "GP";
Serial.println(stuff);
timeStamp(stuff, 1);
}
delay(200);
}
//checks to see if the switch has been thrown
//waits to act until the switch is reset.
void button_check() {
if (digitalRead(checker_button)) {
Serial.println("Button ON");
while (digitalRead(checker_button)) {
}
Serial.println("Button OFF");
//timeStamp("chicken", 2);
initiateGC();
}
}
//simply sends a command to the second Arduino
void initiateGC() {
Wire.beginTransmission(2);
Wire.write("GC");
Wire.endTransmission();
}
//Depending on what codeB is given, either Status.txt. or Errors.txt is chosen
//codeA is then printed to said .txt file as well as a millis()
//after writing data, all data contained within said file is supposed to be read back
void timeStamp(String codeA, int codeB) {
if (codeB == 1) {
Status = SD.open("Status2.txt", FILE_WRITE);
if (Status) {
Serial.println("Opening Status.txt");
Status.print(codeA);
Status.print(" - ");
Status.println(millis());
//Status.close();
//delay(2);
//Status = SD.open("Status2.txt");
while (Status.available()) {
Serial.write(Status.read());
}
Status.close();
} else {
Serial.println("Error Opening Status.txt");
Status.close();
}
} else {
Errors = SD.open("Errors2.txt", FILE_WRITE);
if (Errors) {
Serial.println("Opening Errors.txt");
Errors.print(codeA);
Errors.print(" - ");
Errors.println(millis());
Errors.close();
delay(2);
Errors = SD.open("Errors2.txt");
while (Errors.available()) {
Serial.write(Errors.read());
}
Errors.close();
} else {
Serial.println("Error Opening Status.txt");
Errors.close();
}
}
}
Arduino 2:
#include <Wire.h>
String tester;
String command = "";
byte code;
int x;
void setup() {
Wire.begin(2);
Wire.onReceive(receiveEvent);
}
//As to your suggestion, the "replies" are in loop() and not in the interrupt.
//two codes are sent 5 seconds apart (I chose this number during later tests,
//but the original delay was very short, say 100ms)
void loop() {
if (command == "GC") {
code = 1;
Wire.beginTransmission(1);
Wire.write(code);
Wire.endTransmission();
delay(5000);
code = 2;
Wire.beginTransmission(1);
Wire.write(code);
Wire.endTransmission();
delay(5000);
}
command = "";
delay(100);
}
//reads in the command and stores it.
void receiveEvent(int howMany) {
command = "";
code = 3;
while(Wire.available())
{
char c = Wire.read();
command += c;
}
delay(500);
}