Help in SPI daisy chaining code
This code is basically for three arduinos in daisy chain SPI configuration, each arduino is connected to a pair of led and button, now if any of the button is pressed once the led will start to glow that is connected to master arduino, if any of the button is pressed twice the led on slave 1 will start to glow, if any of the button is pressed thrice the led on slave 2 will start to glow, but this code is not working can anyone help fix it?
`
//mastercode
#include<SPI.h>
const int button = 2;
const int led = 3;
int buttonread = 0;
int counts = 0;
long currentTime = 0;
const int del = 3000;
byte returns;
void setup() {
Serial.begin(9600);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
pinMode(button, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
buttonread = digitalRead(button);
if (buttonread == HIGH) {
counts++;
delay(250);
}
if (counts == 1) {
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led, LOW);
} else if (counts == 2) {
digitalWrite(SS, LOW);
returns = SPI.transfer(2);
digitalWrite(SS, HIGH);
}
else if (counts == 3) {
digitalWrite(SS, LOW);
returns = SPI.transfer(3);
digitalWrite(SS, HIGH);}
else {
digitalWrite(SS, LOW);
byte returns = SPI.transfer(0);
if (returns == 1){
digitalWrite(led,HIGH);}
else{digitalWrite(led, LOW);}
digitalWrite(SS, HIGH);
}
if (millis() - currentTime >= del) {
counts = 0;
currentTime = millis();
}
Serial.print("return: ");
Serial.println(returns);
Serial.println(counts);}
//slave1
#include <SPI.h>
const int button = 2;
const int led = 3;
boolean received;
byte receives, transfers;
int counts = 0;
long currentTime = 0;
const int del = 3000;
int x;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT);
pinMode(button, INPUT);
SPCR |= 1<<SPE;
received = false;
SPI.attachInterrupt();
}
ISR(SPI_STC_vect)
{
receives = SPDR;
received = true;}
void loop() {
//digitalWrite(led, LOW);
int buttonread = digitalRead(button);
if (buttonread == HIGH) {
counts++;
delay(250);}
if(received){
if (receives == 2) {
digitalWrite(led, HIGH);
}
else{
digitalWrite(led, LOW);
}
if (counts == 1){x=1;}
else if(counts == 2){digitalWrite(led, HIGH);}
else if(counts == 3){x=3;}
else{x=0;}
transfers=x;
SPDR = transfers; }
Serial.println(counts);
if (millis() - currentTime >= del) {
counts = 0;
currentTime = millis();
}
}
//slave2
#include <SPI.h>
const int button = 2;
const int led = 3;
boolean received;
byte receives, transfers;
int counts = 0;
long currentTime = 0;
const int del = 5000;
int x;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT);
pinMode(button, INPUT);
SPCR |= 1<<SPE;
received = false;
SPI.attachInterrupt();
}
ISR(SPI_STC_vect)
{
receives = SPDR;
received = true;}
void loop() {
digitalWrite(led, LOW);
if(received){
if (receives == 3) {
digitalWrite(led, HIGH);
}
else{
digitalWrite(led, LOW);
}
int buttonread = digitalRead(button);
if (buttonread == HIGH) {
counts++;
delay(250);}
if (counts == 1){x=1;}
else if(counts == 2){x=2;}
else if(counts == 3){digitalWrite(led, HIGH);}
else{x=0;}
transfers=x;
SPDR = transfers;
if (millis() - currentTime >= del) {
counts = 0;
currentTime = millis();
} }
Serial.println(counts);
}}` ` `