0
votes

Need your help again: I'm doing this time Master - Slave Using SPI communication, there is no error in the code when I simulate the code but the LED won't turn on.

The supposed outcome that should happen is that when I push the push button on master board the LED on the slave board will turn on.

Master code:

// Master Board
#include <SPI.h>

#define button1 4
#define SS 10
int buttonvalue;
int x;

void setup(void) {
  Serial.begin(115200);                 //set baud rate to 115200 for usart
   digitalWrite(SS, HIGH);              // disable Slave Select
   SPI.begin ();
   SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}

void loop(void) {
   digitalWrite(SS, LOW);
  buttonvalue = digitalRead(button1);
  if (buttonvalue == HIGH) {
    x = 1;
  } else {
    x = 0;
  }
  digitalWrite(SS, HIGH);
  delay(1000);
}

Slave code:

// Slave Board
#include <SPI.h>

#define led1 2
volatile byte Slavereceived;
volatile boolean received;
int x;

void setup(void) {
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(MISO,OUTPUT); 
  SPCR |= _BV(SPE); 
  received = false;
  SPI.attachInterrupt();   
}

ISR (SPI_STC_vect) {
   Slavereceived = SPDR;   
   received = true;   
}
 
void loop() { 
  if (received) {
    if (Slavereceived == 1) {
      digitalWrite(led1, HIGH);       
    } else {
      digitalWrite(led1, LOW);         
    }
    delay(1000);
  }
}

enter image description here

1
Just a bit of an enhancement to your code: rather than using that variable buttonvalue, you can just set x = digitalRead(button1);. If you want buttonvalue, you can do x = buttonvalue rather than having the whole if statement. - Rojo
I think you have a wiring error. MOSI from the master arduino should be connected to MISO in the slave arduino. I don't too much about SPI, but try that. - Rojo

1 Answers

0
votes

I too was stuck in the same situation, there is no support for the SPI library in tinkercad, you can include it without errors, and even use it, but any useful command will let the code stuck at that command Sorry, but there no much you can do this link if for a tinkercad forum, where one of the people said SPI library amoung two others are not supported