Yes, you understood correctly. Unfortunately I can't communicate in a different way. Tried NewSoftSerial but had lots of problems.
This is the code running in MCU#1 (ATMEGA 2560)
#include <SD.h>
bool firstSDaccessCall = true;
uint8_t ii = 0;
void setup()
{
#define PD4 74
#define PE2 73
#define PL3 46
#define PD5 75
#define MAINSDCS 70
#define PD3 18
pinMode(PD3,INPUT);
pinMode(PD4,OUTPUT);
pinMode(PE2,OUTPUT);
pinMode(PL3,OUTPUT);
pinMode(PD5,OUTPUT);
pinMode(MAINSDCS,OUTPUT);
Serial.begin(9600);
delay(2000);
}
void requestSDaccess()
{
delay(10);
digitalWrite(PL3,LOW); //this calls MCU#3 routine to create the "path" to the SD card
digitalWrite(PL3,HIGH);
digitalWrite(PD5,HIGH); //this warns MCU#2 that MCU#1 is using the card
if(firstSDaccessCall)
{
Serial.println(SD.begin(MAINSDCS),DEC);
firstSDaccessCall = false;
}
}
void loop()
{
Serial.println(millis()/1000);
if((millis()/1000) % 20 == 0)
{
while(digitalRead(PD3) == HIGH); //checks if MCU#2 is using SD card
requestSDaccess(); //ask access to the card
Serial.print("opening file...");
File submit = SD.open("submit.txt",FILE_WRITE);
if(submit == NULL)
Serial.println("NULL!");
submit.print("sub ");
submit.println(ii,DEC);
ii++;
submit.close();
// pinMode(50, OUTPUT); //miso
// pinMode(51, INPUT); //mosi
// pinMode(52, INPUT); //sck
// pinMode(MAINSDCS, INPUT); //ss
digitalWrite(PD5,LOW);
firstSDaccessCall = true; // this forces the routine to call SD.begin() again (I receive an error with this call)...
digitalWrite(PD4,LOW); //this activates an interruption on MCU#2 so it know that has something to read
digitalWrite(PD4,HIGH);
delay(1000);
}
}
MCU#2 (ATMEGA328P has the following code)
#include <SD.h>
#include <PinChangeInt.h>
bool firstSDaccessCall = true;
bool harvestSubmissionFlag = false;
void PC5handler()
{
SubmissionFlag = true;
}
void setup()
{
#define SSPIN 10
#define PD2 2
#define PD3 3
#define PC4 18
#define PC5 19
#define PD5 5
#define PD6 6
pinMode(SSPIN,OUTPUT);
pinMode(PD2,OUTPUT);
pinMode(PD3,OUTPUT);
pinMode(PC4,INPUT);
pinMode(PC5,INPUT);
pinMode(PD5,OUTPUT);
pinMode(PD6,INPUT);
digitalWrite(PD2,LOW);
digitalWrite(PD3,LOW);
digitalWrite(PD5,LOW);
PCintPort::attachInterrupt(PC5, PC5handler,RISING);
Serial.begin(9600);
}
void requestSDaccess()
{
delay(10);
digitalWrite(PD5,LOW);
digitalWrite(PD5,HIGH);
digitalWrite(PD2,HIGH);
if(firstSDaccessCall)
{
while(!SD.begin(SSPIN));
firstSDaccessCall = false;
}
}
void readFile()
{
Serial.println("=================");
Serial.println("readFile()");
Serial.println("=================");
File testfile;
char ch;
while(digitalRead(PC4) == HIGH);
requestSDaccess();
testfile = SD.open("submit.txt",FILE_READ);
while(testfile.available())
{
ch = testfile.read();
Serial.print(ch);
}
Serial.println();
testfile.close();
// pinMode(12, OUTPUT); //miso
// pinMode(11, INPUT); //mosi
// pinMode(13, INPUT); //sck
// pinMode(SSPIN, INPUT); //ss
digitalWrite(PD2,LOW);
// delay(10000);
}
void loop()
{
Serial.println(SubmissionFlag,DEC);
if(SubmissionFlag)
{
readFile();
SubmissionFlag = false;
}
}
and, finally MCU#3 (ATMEGA328P) runs this code:
#include <PinChangeInt.h>
//#define PC0 14
#define PC1 15
#define PC3 17
//#define PC4 18
void setup()
{
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
// pinMode(PC0,OUTPUT);
pinMode(PC1,INPUT);
pinMode(PC3,INPUT);
// pinMode(PC4,OUTPUT);
PCintPort::attachInterrupt(PC3, SDaccessMCU1,RISING);
PCintPort::attachInterrupt(PC1, SDaccessMCU2,RISING);
}
void SDaccessMCU1()
{
digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}
void SDaccessMCU2()
{
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
}
void loop()
{
}