I have a master and a slave (both are arduino UNO) connected through SPI connected. I want to transmit a character to the slave and receive true or false to the master. The transmission is working perfectly. If I send '2', slave is receiving '2'. When I try to transmit an integer from slave to master, the master is always receiving some arbitrary number like 128 or 255.
Master Code:
void setup() {
Serial.begin(9600);
Serial.println("Master");
digitalWrite(SS,HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
void loop() {
digitalWrite(SS,LOW); // Slave select to low
received1 = SPI.transfer(test); // test is a variable which is '2'. The data received by master is stored in received1 variable which is char.
Serial.println(received1);
digitalWrite(SS,HIGH);
}
Slave code:
void setup() {
Serial.begin(9600);
Serial.println("Slave");
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
process = false;
SPI.attachInterrupt();
}
ISR(SPI_STC_vect){
received = SPDR; // the data received by slave is slored in **received** variable(the variable is char type)
Serial.println(received);
process = true;
}
void loop() {
if(process){
SPDR = jj; // Updating SPDR to send data from slave. jj is char type
process = false;
}
}
process, is not here. Is it volatile? Also do you realize that SPDR on slave must be set before the master starts transmission?process. It's abooleanvariable which will be true when something is received by slave.