'UNO Master SPI' acquires temp signal from LM35 sensor via 'NANO Slave SPI'

The following 'functional' codes (Step-4) have been developed on trial-and-error basis. Please, place your criticisms on the weakness of these codes and the directions for creating robust/alternate/descent codes.

1. The setup
multicomspiy.png

2. The Slave updates the temperature at 1-sec interval using TC1 overflow interrupt.

3. The Master collects temperature signal from Slave at 2-sec interval. The Slave places the temperature signal on SPI Port based on flag activated in the 'ISR due to SPI Interrupt'.

4. The Codes
Master SPI Codes:

#include<SPI.h>
byte dataBuffer[4] = {0};

union
{
  float rxTemp;
  byte rxArray[4];
} rxData;


void setup (void)
{
  Serial.begin(9600);
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV128);
  digitalWrite(SS, LOW);
}

void loop(void)
{
  SPI.transfer(dataBuffer, sizeof(dataBuffer));

  Serial.println(dataBuffer[0], HEX);   //dataBuffer[0] = rxArray[3]3 //test/debug purpose
  Serial.println(dataBuffer[1], HEX);   //dataBuffer[1] = rxArray[0]0 //test/debug purpose
  Serial.println(dataBuffer[2], HEX);   //dataBuffer[2] = rxArray[1]1 //test/debug purpose
  Serial.println(dataBuffer[3], HEX);   //dataBuffer[3] = rxArray[2]2 //test/debug purpose
  rxData.rxArray[0] = dataBuffer[1];  //there is 1-byte offset, and it is due to 'the way SPI works'
  rxData.rxArray[1] = dataBuffer[2];
  rxData.rxArray[2] = dataBuffer[3];
  rxData.rxArray[3] = dataBuffer[0];
  Serial.print("Temperatue received from LM35 via SPI Port: ");
  Serial.print(rxData.rxTemp, 2);  Serial.println(" degC");
  Serial.println("======================================================");
  delay(2000);
}

Slave SPI codes:

#include<SPI.h>
volatile bool flag1 = false;
int i = 0;

//---- to convert float into bytes------------------
union
{
  float txTemp;  //1.23 test code
  byte txArray[4];  //0x3F9D70A4for 1.23  little endinaness
} txData;
//--------------------------------------------------

void setup()
{
  Serial.begin(9600);
  
  //--TC1 initialize to generate 1-sec TimeTick----
  TCCR1A = 0x00;
  TCCR1B = 0x00;
  TCNT1 = 0xC2F7;
  bitSet(TIMSK1, 0);  //TC1 Overflow will generate interrupt)
  interrupts();   //global interrupt bit is enabled
  TCCR1B = 0x05;  //TC1 is ON with clkTC1 = clkSYS/1024
  //-------------------------------------------------  
  analogReference(INTERNAL);
  
  //--SPI Port intilization od Slave-1---------------
  pinMode(MISO, OUTPUT);  //MISO as OUTPUT to send data to Master
  pinMode(10, INPUT_PULLUP);  //DPin-10 = Slave Select
  bitSet(SPCR, 6); //SPI Port is enabled or SPCR |=_BV(SPE);
  bitClear(SPCR, 4);  //SPI Port in slave mode
  SPI.attachInterrupt();
  //---------------------------------------------------
}

void loop()
{
  if (flag1 == false)
  {
    ; //wait ofr interruts both from SPI and TC1
  }
  else  //flag1 = true; place data to SPI Port byte-by-byte for transfer to Master
  {
    SPDR = txData.txArray[i];
    flag1 = false;
    i++;
    if (i == 4)
    {
      i = 0;
    }
  }
}

ISR (SPI_STC_vect)  //ISR due to SPI interrupt      
{
  flag1 = true;
}

ISR(TIMER1_OVF_vect)  //ISR due to TC1 overflow interrupt
{
  TCNT1 = 0xC2F7; //re-load 1-sec time delay pre-set value
  txData.txTemp = (float)100 * (1.1 / 1023.0) * analogRead(A5);
}

Master Serial Monitor
sm103.png

sm103.png

multicomspiy.png