Multiple Arduino SPI communication

Hey guys

I have three arduinos connected together for SPI as below. The master has a pot connected to A1 and it should send the mapped value to S1 and the reversed mapped value to S2 (I'm reversing it because I don't have a second Pot on hand). This doesn't work thou, I think it might have something to do with the SS pins 9 and 10 out of the master.

This is a early test for a project were I need three steppers each connected to their own arduinos and values for positioning is sent over SPI from the master. Let me know if this is the best way of going about things

Thanks

Ian

M-->S1->S2

09------->10 S2
10------->10 S1
11->11->11
12->12->12
13->13->13

MASTER

// master

#include <SPI.h>
#include "SPI_anything.h"

// create a structure to store the different data values:
typedef struct myStruct
{
  int a;
  int b;
  int val;
  int valtwo;
};

myStruct foo;

void setup ()
{
  SPI.begin ();
  // Slow down the master a bit
  SPI.setClockDivider(SPI_CLOCK_DIV128);
  pinMode(A1, INPUT);


  foo.b = 0;
  foo.a = 0;
}  // end of setup

void loop ()
{
  int potanalog = analogRead(1);
  int valtwo = map(potanalog, 0, 1023, 255, 0);
  int  val = map(potanalog, 0, 1023, 0, 255);


  foo.b = val;
  foo.a = valtwo;
  digitalWrite(10, LOW);    // SS is pin 10
  SPI_writeAnything (foo.b);

  digitalWrite(10, HIGH);

  digitalWrite(9, LOW);    // SS is pin 10
  SPI_writeAnything (foo.a);
  digitalWrite(9, HIGH);

  //delay (500);  // for testing

}  // end of loop

SLAVE 1

// slave1

#include <SPI.h>
#include "SPI_anything.h"

// create a structure to store the different data values:
typedef struct myStruct
{

  int b;

};

myStruct foo;

void setup ()
{

  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);
}  // end of setup

void loop ()
{
  SPI_readAnything (foo);
  analogWrite(3, foo.b);

}  // end of loop

SLAVE 2

// slave2

#include <SPI.h>
#include "SPI_anything.h"

// create a structure to store the different data values:
typedef struct myStruct
{

  int a;

};

myStruct foo;

void setup ()
  {


  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  
  // turn on SPI in slave mode
  SPCR |= _BV(SPE);
  }  // end of setup

void loop () 
  { 
  SPI_readAnything (foo);
analogWrite(3,foo.a);

  }  // end of loop

SPI_anything.h

#include <Arduino.h>

template <typename T> unsigned int SPI_writeAnything (const T& value)
  {
    const byte * p = (const byte*) &value;
    unsigned int i;
    for (i = 0; i < sizeof value; i++)
          SPI.transfer(*p++);
    return i;
  }  // end of SPI_writeAnything

template <typename T> unsigned int SPI_readAnything(T& value)
  {
    byte * p = (byte*) &value;
    unsigned int i;
    for (i = 0; i < sizeof value; i++)
          *p++ = SPI.transfer (0);
    return i;
  }  // end of SPI_readAnything
  
  
template <typename T> unsigned int SPI_readAnything_ISR(T& value)
  {
    byte * p = (byte*) &value;
    unsigned int i;
    *p++ = SPDR;  // get first byte
    for (i = 1; i < sizeof value; i++)
          *p++ = SPI.transfer (0);
    return i;
  }  // end of SPI_readAnything_ISR

The struct thing threw me off for a while. You create a structure and then just send two ints.

In what way does it not work? No data transmitted? Wrong data?

And your receivers are trying to receive a struct. If you want the data to not be garbled, the receiver must receive exactly the same type of thing as what was sent.

That's why most communication protocols only send and receive bytes. It's up to the programmer to reassemble the bytes into a meaningful message.

Thanks I'll try to fix that tomorrow morning.

Have I done the correct thing with the SS lines connected to the seperate arduino's?

Master MISO goes to SLAVE MOSI and Master MOSI goes to SLAVE MISO

11 -> 12 -> 12
12 -> 11 -> 11