gwentwor:
I've read up on Software Serial and updated the code as follows (to the best of my ability) in addition to moving to pins 10/11:
#include <SD.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(10,11);
const int chipSelect = 12;
void setup()
{
// output, even if you don't use it:
pinMode(10, INPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
mySerial.begin(9600);
mySerial.println("Initializing SD card...");
// make sure that the default chip select pin is set to
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
mySerial.println("Card failed, or not present");
// don't do anything more:
return;
}
mySerial.println("card initialized.");
}
int counter = 0;
int particles = 0;
void loop()
{
delay(1000);
if (mySerial.available() >0) {
particles = mySerial.read();
mySerial.println("Arduino Gotcha:");
mySerial.println(particles, DEC); // print as a raw byte value
delay(1000);
mySerial.flush();
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(particles);
dataFile.close();
// print to the serial port too:
mySerial.println(particles);
}
// if the file isn't open, pop up an error:
else {
mySerial.println("error opening datalog.txt");
}
}
It compiles okay and after I upload it nothing appears in the serial monitor. It should print what it receives on Digital Port 10, correct? I'm admittedly a complete novice at coding. Any advice?
I am guessing that your SD is on a shield so you don't know what pins it uses. 10 and 11 are two of the SPI pins and SD uses SPI. I know because my SD is on a module, I have to stick the jumpers in and I'm looking at an UNO with SD, 10 to 13 + GND are filled. I've gone with 2 and 3 for soft serial with SD.
Also there's a debug technique we use when we're not sure what the code is doing. We print short messages to serial monitor from different places in the code to show us what path the code took and what critical values we want to know. It can work very well, check yourself this way as well as your code.
When you have SD, you can log run data to that. If there's a problem, you might have data on how it got there.