user63826: i want to connect  SPI 2 arduino master and slave through the MUX , to write  and read in external memory , when the master write in memory slave read .
pls help . 
code to master
#include <SPI.h>
//-------FM------------------
#define WENB 0x06
#define RDSR 0x05
#define WRSR 0x01
#define PPROGRAM 0x02
#define SERASE 0x20
#define BERASE 0xd8
#define READ 0x03
#define RDSCUR 0x2B
#define CERASE 0x60//or 0xC7

//------------Specify the address-----------
int address = 0x00ff0000;
byte x, Y;
int IN = 5;
int SS1 = 9;
/*char data[]=("don't be afraid ");
char recived[15];*/
//--------------------------
void setup() {
    //------------------------------------------
    // pinMode(EN,OUTPUT);
    pinMode ( SS, OUTPUT ); //ss pin 10
    pinMode ( SS1, OUTPUT );
    digitalWrite ( SS1, HIGH );
    pinMode ( IN, OUTPUT );
    digitalWrite ( IN, LOW ); // MUX connect A
    //digitalWrite(CS,low);
    Serial.begin ( 9600 );
    SPI.begin();//start the SPI connection with the flash memory
    SPI.setClockDivider ( SPI_CLOCK_DIV8 );
    SPI.setBitOrder ( MSBFIRST );
    digitalWrite ( SS, HIGH );
    delay ( 10 );
    //-------erase the sector--//-----------
    digitalWrite ( SS, LOW );
    SPI.transfer ( WENB );
    digitalWrite ( SS, HIGH );
    //-------------------------------------
    digitalWrite ( SS, LOW );
    SPI.transfer ( SERASE );
    SPI.transfer ( ( address >> 32 ) & 0xff );
    SPI.transfer ( ( address >> 16 ) & 0xff );
    SPI.transfer ( ( address >> 8 ) & 0xff );
    SPI.transfer ( address & 0xff );
    digitalWrite ( SS, HIGH );
    delay ( 5000 );
    //------------------------------------------
    //Serial.println("okay fine")
    //-----Program Data Into Memory Array-----.
    //-----------interrupts the slave----------
    /* digitalWrite(SS1,LOW);
     SPI.transfer(0xff);//are you there?
     digitalWrite(SS1,HIGH);
     delay(1000);

     digitalWrite(IN,HIGH);*/
}

void loop()

{
    char c;
    int i = 0;
    char data[] = "Hello!:";
    char recived[7];
    byte x;
    //----------Enable the write in the memory-----.
    digitalWrite ( SS, LOW );
    SPI.transfer ( WENB );
    digitalWrite ( SS, HIGH );
    delay ( 20 );
    //-----Program Data Into Memory Array-----.
    digitalWrite ( SS, LOW );
    SPI.transfer ( PPROGRAM );
    SPI.transfer ( ( address >> 32 ) & 0xff );
    SPI.transfer ( ( address >> 16 ) & 0xff );
    SPI.transfer ( ( address >> 8 ) & 0xff );
    SPI.transfer ( address & 0xff );

    for ( int i = 0; i < sizeof ( data ); i++ ) {
        SPI.transfer ( data[i] );
        // Serial.print(data[i]);
    }

    // disable Slave Select
    digitalWrite ( SS, HIGH );
    //--------------------------------------------
    delay ( 1000 );
    //--------------------------- ----------
    i = 0;
    Serial.print ( "It's okay saved: " );

    while ( data[i] != NULL ) {
        Serial.println ( data[i] );
        i++;
    }

    // delay(100);
    //-----------interrupts the slave----------
    digitalWrite ( SS1, LOW );
    SPI.transfer ( 0xff ); //are you there?
    digitalWrite ( SS1, HIGH );
    delay ( 1000 );
    digitalWrite ( IN, HIGH ); // MUX connect B
}

code to slave 
#include <SPI.h>
//-------FM------------------
#define WENB 0x06
#define RDSR 0x05
#define WRSR 0x01
#define PPROGRAM 0x02
#define SERASE 0x20
#define BERASE 0xd8
#define READ 0x03
#define RDSCUR 0x2B
#define CERASE 0x60//or 0xC7

//------------Specify the address-------------
int address = 0x00ff0000;

int SS1 = 9;
char data[] = "Hello!:";
char buff[7];

byte slave;
volatile bool process;
//----------------config slave----------------
void setup() {
    //pinMode(IN,OUTPUT);
    pinMode ( SS, OUTPUT ); //SS pin 10
    pinMode ( MISO, OUTPUT );
    pinMode ( SS1, INPUT );
    pinMode ( MOSI, INPUT );
    //--------------------------------------------------
    Serial.begin ( 9600 );
    ///SPCR |= bit (SPE);
    SPI.begin();//start the SPI connection with the flash memory
    SPI.setClockDivider ( SPI_CLOCK_DIV8 );
    SPI.setBitOrder ( MSBFIRST );
    // digitalWrite(SS,HIGH);
    //-------------master interrupts the slave----------------------------------------------
    // now turn on interrupts
    // SPCR |=bit(SPIE);
    //SPCR |=bit(SPE); //set SPE bit at the SPCR(spi control register) to enable SPI
    SPI.attachInterrupt();
}

ISR ( SPI_STC_vect ) {
    slave = SPDR;

    if ( slave = 0xff ) {
        pinMode ( MISO, INPUT );
        // pinMode(SS1,INPUT);
        pinMode ( MOSI, OUTPUT );
        //digitalWrite(IN,HIGH);
        digitalWrite ( SS, LOW );
        SPI.transfer ( READ );
        SPI.transfer ( ( address >> 32 ) & 0xff );
        SPI.transfer ( ( address >> 16 ) & 0xff );
        SPI.transfer ( ( address >> 8 ) & 0xff );
        SPI.transfer ( address & 0xff );

        for ( int n = 0; n < sizeof ( data ); n++ ) {
            data[n] = SPI.transfer ( 0 );
            Serial.print ( data[n] );
        }

        digitalWrite ( SS, HIGH );
    }
}


user42539: Having a line from Arduino 1 to Arduino 2 to indicate that Arduino 2 should do something is sufficient.  
Simple way: a line that is an input on both sides with internal pullups turned on.  Both sides monitor it. 
When Arduino 1 wants to write, it checks to make sure the line is high, then it makes its pin an output and pulls it low, and when done releases it by going back to an input (using pinMode() command). 
When Arduino 2 see the low, it waits for it to go high, then makes its input and output and pulls it low until it is done reading. 
Arduino 1 sees the low and holds off on any writing until it goes back high again.
This is known as arbitration, and is an example of one simple method.

63826: <p>i want to connect  SPI 2 arduino master and slave through the MUX , to write  and read in external memory , when the master write in memory slave read .
pls help . 
code to master</p>

<pre class="lang-cpp prettyprint-override"><code>#include &lt;SPI.h&gt;
//-------FM------------------
#define WENB 0x06
#define RDSR 0x05
#define WRSR 0x01
#define PPROGRAM 0x02
#define SERASE 0x20
#define BERASE 0xd8
#define READ 0x03
#define RDSCUR 0x2B
#define CERASE 0x60//or 0xC7

//------------Specify the address-----------
int address = 0x00ff0000;
byte x, Y;
int IN = 5;
int SS1 = 9;
/*char data[]=("don't be afraid ");
char recived[15];*/
//--------------------------
void setup() {
    //------------------------------------------
    // pinMode(EN,OUTPUT);
    pinMode ( SS, OUTPUT ); //ss pin 10
    pinMode ( SS1, OUTPUT );
    digitalWrite ( SS1, HIGH );
    pinMode ( IN, OUTPUT );
    digitalWrite ( IN, LOW ); // MUX connect A
    //digitalWrite(CS,low);
    Serial.begin ( 9600 );
    SPI.begin();//start the SPI connection with the flash memory
    SPI.setClockDivider ( SPI_CLOCK_DIV8 );
    SPI.setBitOrder ( MSBFIRST );
    digitalWrite ( SS, HIGH );
    delay ( 10 );
    //-------erase the sector--//-----------
    digitalWrite ( SS, LOW );
    SPI.transfer ( WENB );
    digitalWrite ( SS, HIGH );
    //-------------------------------------
    digitalWrite ( SS, LOW );
    SPI.transfer ( SERASE );
    SPI.transfer ( ( address &gt;&gt; 32 ) &amp; 0xff );
    SPI.transfer ( ( address &gt;&gt; 16 ) &amp; 0xff );
    SPI.transfer ( ( address &gt;&gt; 8 ) &amp; 0xff );
    SPI.transfer ( address &amp; 0xff );
    digitalWrite ( SS, HIGH );
    delay ( 5000 );
    //------------------------------------------
    //Serial.println("okay fine")
    //-----Program Data Into Memory Array-----.
    //-----------interrupts the slave----------
    /* digitalWrite(SS1,LOW);
     SPI.transfer(0xff);//are you there?
     digitalWrite(SS1,HIGH);
     delay(1000);

     digitalWrite(IN,HIGH);*/
}

void loop()

{
    char c;
    int i = 0;
    char data[] = "Hello!:";
    char recived[7];
    byte x;
    //----------Enable the write in the memory-----.
    digitalWrite ( SS, LOW );
    SPI.transfer ( WENB );
    digitalWrite ( SS, HIGH );
    delay ( 20 );
    //-----Program Data Into Memory Array-----.
    digitalWrite ( SS, LOW );
    SPI.transfer ( PPROGRAM );
    SPI.transfer ( ( address &gt;&gt; 32 ) &amp; 0xff );
    SPI.transfer ( ( address &gt;&gt; 16 ) &amp; 0xff );
    SPI.transfer ( ( address &gt;&gt; 8 ) &amp; 0xff );
    SPI.transfer ( address &amp; 0xff );

    for ( int i = 0; i &lt; sizeof ( data ); i++ ) {
        SPI.transfer ( data[i] );
        // Serial.print(data[i]);
    }

    // disable Slave Select
    digitalWrite ( SS, HIGH );
    //--------------------------------------------
    delay ( 1000 );
    //--------------------------- ----------
    i = 0;
    Serial.print ( "It's okay saved: " );

    while ( data[i] != NULL ) {
        Serial.println ( data[i] );
        i++;
    }

    // delay(100);
    //-----------interrupts the slave----------
    digitalWrite ( SS1, LOW );
    SPI.transfer ( 0xff ); //are you there?
    digitalWrite ( SS1, HIGH );
    delay ( 1000 );
    digitalWrite ( IN, HIGH ); // MUX connect B
}
</code></pre>

<p>code to slave </p>

<pre class="lang-cpp prettyprint-override"><code>#include &lt;SPI.h&gt;
//-------FM------------------
#define WENB 0x06
#define RDSR 0x05
#define WRSR 0x01
#define PPROGRAM 0x02
#define SERASE 0x20
#define BERASE 0xd8
#define READ 0x03
#define RDSCUR 0x2B
#define CERASE 0x60//or 0xC7

//------------Specify the address-------------
int address = 0x00ff0000;

int SS1 = 9;
char data[] = "Hello!:";
char buff[7];

byte slave;
volatile bool process;
//----------------config slave----------------
void setup() {
    //pinMode(IN,OUTPUT);
    pinMode ( SS, OUTPUT ); //SS pin 10
    pinMode ( MISO, OUTPUT );
    pinMode ( SS1, INPUT );
    pinMode ( MOSI, INPUT );
    //--------------------------------------------------
    Serial.begin ( 9600 );
    ///SPCR |= bit (SPE);
    SPI.begin();//start the SPI connection with the flash memory
    SPI.setClockDivider ( SPI_CLOCK_DIV8 );
    SPI.setBitOrder ( MSBFIRST );
    // digitalWrite(SS,HIGH);
    //-------------master interrupts the slave----------------------------------------------
    // now turn on interrupts
    // SPCR |=bit(SPIE);
    //SPCR |=bit(SPE); //set SPE bit at the SPCR(spi control register) to enable SPI
    SPI.attachInterrupt();
}

ISR ( SPI_STC_vect ) {
    slave = SPDR;

    if ( slave = 0xff ) {
        pinMode ( MISO, INPUT );
        // pinMode(SS1,INPUT);
        pinMode ( MOSI, OUTPUT );
        //digitalWrite(IN,HIGH);
        digitalWrite ( SS, LOW );
        SPI.transfer ( READ );
        SPI.transfer ( ( address &gt;&gt; 32 ) &amp; 0xff );
        SPI.transfer ( ( address &gt;&gt; 16 ) &amp; 0xff );
        SPI.transfer ( ( address &gt;&gt; 8 ) &amp; 0xff );
        SPI.transfer ( address &amp; 0xff );

        for ( int n = 0; n &lt; sizeof ( data ); n++ ) {
            data[n] = SPI.transfer ( 0 );
            Serial.print ( data[n] );
        }

        digitalWrite ( SS, HIGH );
    }
}
</code></pre>
 42539: <p>Having a line from Arduino 1 to Arduino 2 to indicate that Arduino 2 should do something is sufficient.  </p>

<p>Simple way: a line that is an input on both sides with internal pullups turned on.  Both sides monitor it. </p>

<p>When Arduino 1 wants to write, it checks to make sure the line is high, then it makes its pin an output and pulls it low, and when done releases it by going back to an input (using pinMode() command). </p>

<p>When Arduino 2 see the low, it waits for it to go high, then makes its input and output and pulls it low until it is done reading. </p>

<p>Arduino 1 sees the low and holds off on any writing until it goes back high again.</p>

<p>This is known as arbitration, and is an example of one simple method.</p>
