SPI Multi Slaves sends to a Master

Here I use 3 arduino slaves. (But In the future I will use 5 slaves)

// master

#include <SPI.h>

#define SS_1 7
#define SS_2 8
#define SS_3 6
//#define SS_4 5
//#define SS_5 4

void setup (void)
{
  Serial.begin (115200);
  Serial.println ("Starting");

  pinMode(SS_1, OUTPUT);
  pinMode(SS_2, OUTPUT);
//  pinMode(SS_3, OUTPUT);
//  pinMode(SS_4, OUTPUT);
//  pinMode(SS_5, OUTPUT);

  digitalWrite(SS_1, LOW);  // ensure SS stays high for now
  digitalWrite(SS_2, LOW);  // ensure SS stays high for now
 digitalWrite(SS_3, LOW);  // ensure SS stays high for now
//  digitalWrite(SS_4, LOW);  // ensure SS stays high for now
//  digitalWrite(SS_5, LOW);  // ensure SS stays high for now

  SPI.begin ();

  SPI.setClockDivider(SPI_CLOCK_DIV128);
}

void loop (void)
{
  char buf_1[54]; //54
  char buf_2[54]; //54
//  char buf_3[54]; //54
//  char buf_4[54]; //54
//  char buf_5[54]; //54

  digitalWrite(SS_1, HIGH );

  SPI.transfer (1);
  for (int pos = 0; pos < sizeof (buf_1) - 1; pos++)
  {
    delayMicroseconds (20);
    buf_1 [pos] = SPI.transfer (0);
    if (buf_1 [pos] == 0)
    {
      break;
    }
  }

  buf_1 [sizeof (buf_1) - 1] = 0;

  Serial.print("Slave 1:");
  Serial.print(buf_1);
  memset(buf_1, 0, sizeof(buf_1)); //clear array
  delay(10);
  digitalWrite(SS_1, LOW);
  delay(10);
  Serial.print("\t");
  //================SLAVE 2===================//
  digitalWrite(SS_2, HIGH);

  SPI.transfer (2);
  for (int pos = 0; pos < sizeof (buf_2) - 1; pos++)
  {
    delayMicroseconds (20);
    buf_2 [pos] = SPI.transfer (0);
    if (buf_2 [pos] == 0)
    {
      break;
    }
  }

  buf_2 [sizeof (buf_2) - 1] = 0;

  Serial.print("Slave 2:");
  Serial.print(buf_2);

  memset(buf_2, 0, sizeof(buf_2)); //clear array
  delay(10);

  digitalWrite(SS_2, LOW);
  Serial.print("\t");
  //================SLAVE 3===================//
//  digitalWrite(SS_3, HIGH);
//
//  SPI.transfer (3);
//  for (int pos = 0; pos < sizeof (buf_3) - 1; pos++)
//  {
//    delayMicroseconds (20);
//    buf_3 [pos] = SPI.transfer (0);
//    if (buf_3 [pos] == 0)
//    {
//      break;
//    }
//  }
//
//  buf_3 [sizeof (buf_3) - 1] = 0;
//
//  Serial.print("Slave 3:");
//  Serial.print(buf_3);
//
//  memset(buf_3, 0, sizeof(buf_3)); //clear array
//  delay(10);
//
//  digitalWrite(SS_3, LOW);
//  Serial.print("\t");
  //================SLAVE 4===================//
//  digitalWrite(SS_4, HIGH);
//
//  SPI.transfer (4);
//  for (int pos = 0; pos < sizeof (buf_4) - 1; pos++)
//  {
//    delayMicroseconds (20);
//    buf_4 [pos] = SPI.transfer (0);
//    if (buf_4 [pos] == 0)
//    {
//      break;
//    }
//  }
//
//  buf_4 [sizeof (buf_4) - 1] = 0;
//
//  Serial.print("Slave 4:");
//  Serial.print(buf_4);
//
//  memset(buf_4, 0, sizeof(buf_4)); //clear array
//  delay(10);
//
//  digitalWrite(SS_4, LOW);
//  Serial.print("\t");
//  //================SLAVE 5===================//
//  digitalWrite(SS_5, HIGH);
//
//  SPI.transfer (5);
//  for (int pos = 0; pos < sizeof (buf_5) - 1; pos++)
//  {
//    delayMicroseconds (20);
//    buf_5 [pos] = SPI.transfer (0);
//    if (buf_5 [pos] == 0)
//    {
//      break;
//    }
//  }
//
//  buf_5 [sizeof (buf_5) - 1] = 0;
//
//  Serial.print("Slave 5:");
//  Serial.print(buf_5);
//
//  memset(buf_5, 0, sizeof(buf_5)); //clear array
//  delay(10);
//
//  digitalWrite(SS_5, LOW);
  Serial.println("");
  delay(500);

}  // end of loop
// Slave

#include <Wire.h>
#include <VL53L1X.h>

//#define max_range 1000
//#define min_range 20
#define XSHUT_1 3

VL53L1X sensor1;

//uint8_t value1;

volatile char buf[54];
volatile int pos;
volatile bool active;

void setup (void)
{
  int x;
  int i;
  int j;
  int jaraktotal;

  unsigned long t;
  unsigned long tx;
  unsigned long ty;


  while (!Serial) {}
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  pinMode(MISO, OUTPUT);

  pinMode(XSHUT_1, OUTPUT);

  digitalWrite(XSHUT_1, LOW);

  SPCR |= bit(SPE);

  SPCR |= bit(SPIE);

  //================================================
  //Conf. Sensor1
  //================================================
  delay(50);
  digitalWrite(XSHUT_1, HIGH);
  delay(50);

  sensor1.setTimeout(500);
  if (!sensor1.init())
  {
    Serial.println("Failed to detect and initialize sensor1!");
  }
  else {
    sensor1.setAddress(24);
    sensor1.setDistanceMode(VL53L1X::Long);
    sensor1.setMeasurementTimingBudget(23500); //microsecond => 10 milisecond = 10ms
    sensor1.startContinuous(45);
    sensor1.setROISize(1, 8);

  }

}

ISR (SPI_STC_vect)
{
  byte c = SPDR;
  if (c == 3)    { //different value for every string
    active = true;
    pos = 0;
    SPDR = buf [pos++];

    return;
  }

  if (!active)
  {
    SPDR = 0;
    return;
  }

  SPDR = buf [pos];
  if (buf [pos] == 0 || ++pos >= sizeof(buf))
    active = false;

}

void loop (void)
{
  int x = 0;
//  int hitung = 0;
  int hasil[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  for (int i = 151; i <= 247;) {
    sensor1.setROICenter(i);
    sensor1.startContinuous(10);
    hasil[x] = sensor1.read();
    Serial.print(hasil[x]);
    Serial.print(",");
    x++;
    i += 8;

  }
  Serial.println("");
  sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,", hasil[0],hasil[1],hasil[2],hasil[3],hasil[4],hasil[5],hasil[6],hasil[7],hasil[8],hasil[9],hasil[10],hasil[11],hasil[12]);
}

but the new problem is I can not read the data from the slave 3. I succeed to read data from slave 1 and 2, but when I activate slave 3 I got problem and the output is unwanted characters. When I tried between slave 2 and slave 3 it only showed me the output of slave 3. I wonder what happened and what's wrong with my code? Thank you

Can you post your annotated schematic showing how you have wired it.

Schematic_LIDAR.pdf (103,7 KB)
Schematic_LIDAR_RX.pdf (41,5 KB)

Here are the schemstic of the wiring

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.