Build a task, where SPI Master Arduino will read data from the slave Arduino and print it on a serial monitor. Slave Arduino is a dice roller, which returns any random number between 1 to 6. The code for it is given below.
The user will press a button connected to the master Arduino & the random number will be read and printed on the Serial monitor.
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() {
}