SPI.transfer(Mastersend); //Send the mastersend value to slave also receives value from slave
//Slave Arduino:
#include<SPI.h>
const int segmentPins[] = {9, 8, 7, 6, 5, 4, 3, 2};
volatile boolean received = false;
volatile byte Slavereceived;
int index;
void setup(){
Serial.begin(9600);
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
}
SPCR |= _BV(SPE); //Turn on SPI in Slave Mode
SPI.attachInterrupt(); //Interuupt ON is set for SPI commnucation
}
ISR (SPI_STC_vect){ //Inerrrput routine function
Slavereceived = SPDR; // Value received from master if store in variable slavereceived
received = true; //Sets received as True
}
void loop(){
Serial.println(Slavereceived);
if(received){//Logic to SET LED ON OR OFF depending upon the value recerived from master
displayCharacter(Slavereceived);
delay(1000);
}
}
void displayCharacter(int ch) {
byte patterns[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 0, 1, 0, 0} // 9
};
if ((ch >= 0 && ch <= 9)) {
// Get the digit index (0-9) from the character
int index = ch;
// Write the pattern to the segment pins
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], patterns[index][i]);
}
}
}