Build a task, where the SPI Master microcontroller will read data from the slave microcontroller and print it on a serial terminal (e.g., Putty, Arduino IDE).
The Slave microcontroller is a dice roller, which returns any random number between 1 to 6.
The code of Arduino as a slave is given below.
The user will press a push button switch connected to the master microcontroller, and the random number will be read and printed on the Serial terminal.
Remember: Arduino Dice roller uses only SPI Mode 2.
Slave Code (Arduino Dice roller)
#include <SPI.h>
void setup() {
pinMode(MISO, OUTPUT); // Set MISO as OUTPUT to send data to the master
// Enable SPI in slave mode
SPCR |= _BV(SPE);
// Send SPI data in mode 2
SPI.setDataMode(SPI_MODE2);
// Attach SPI interrupt
SPI.attachInterrupt();
// Generate a random number and store it to transmit during SPI communication.
SPDR = random(1, 7);
}
ISR(SPI_STC_vect) {
// Generate a random number and send it.
SPDR = random(1, 7);
}
void loop() {
}