Brief - I am using two arduino Uno's. One acts as a master and the other as slave using Arduino IDE. Below is the Master Code:
// Taken from Nick Gammon's Forum//
#include "SPI.h"
#include"pin_arduino.h"
int ss = 10;
void setup()
{
pinMode(ss,OUTPUT);
Serial.begin(9600);
Serial.println();
digitalWrite(ss,HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
byte transferAndWait(const byte what)
{
byte a = SPI.transfer(what);
delayMicroseconds(20);
return a;
}
void loop()
{
byte a,b,c;
digitalWrite(ss,LOW);
transferAndWait('a');
a = transferAndWait(17);
b = transferAndWait(33);
c = transferAndWait(0);
digitalWrite(ss,HIGH);
Serial.println(a,DEC);
Serial.println(b,DEC);
Serial.println(c,DEC);
delay(1000);
}
Before I go into the details of the results I obtained and the problem, below is the Slave code:
//Taken from Nick Gammon's Forum//
#include "SPI.h"
#include "pins_arduino.h"
int slave = 10;
int miso = 11;
byte c = 0;
void setup()
{
pinMode(miso,OUTPUT);
pinMode(slave,INPUT);
Serial.begin(9600);
SPR|= BV_(SPE);
SPR|= BV_(SPIE);
}
ISR(SPI_STC_vect)
{
c = SPDR;
switch(c)
{
case 0:
SPDR = 0;
break;
case 'a':
SPDR = c + 15;
break;
}
}
void loop(void)
{
if(digitalRead(slave)==HIGH)
c = 0;
}
My Results : After dumping the master and slave codes into their respective arduino uno's, then when I open the serial monitor of the master code, It displays the number 17, 33, 0 and the same goes with the slave, meaning that the data is being sent to the slave and received back successfully, and hence I presume the SPI bus is working properly!. But why isn't the add operation happening?
Problem: Now the problem is, when the command 'a' is sent from the master to the slave, the slave has to interpret this 'a' command as Addition command and perform the addition of a number 15 to the incoming byte from the master as shown in the slave code, and return the results to the master, which should display the addition results in the masters serial monitor. I am having problem performing this task and looking forward to suggestions on how to test or where I am doing wrong or make changes to the code or methods in order to get this working. I am expecting answers relevant to the below codes and method.