Hi guys
I have a master Arduino and 3 slave Arduinos.
I have tried to research how to send and receive string between them, but the answers i get are vague and I need some help to understand how to implement this into my project.
Say for example I have a serial number stored as a String, we'll call it SN
Now i want to send that String over my SPI bus to my first slave Arduino, but, the slave must respond with a String if the Serial Number is correct.
How would I go about this?
This is what i have so far:
String SN = "Some Serial Number";
void setup()
{
Serial.begin(9600);
digitalWrite(SLAVE1, LOW);
digitalWrite(SLAVE2, HIGH);
digitalWrite(SLAVE3, HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
}
void loop()
{
for(int i = 0; i < SN.length(); i ++)
{
SPI.transfer(SN[i]);
}
}
Easy method would be for the master to send the char array to the slave, then subsequently request data from the slave, at which time it sends back the response char array. Depends on how much processing time the slave needs as to whether that could all be done in the same SPI communications, or if there needs to be a delay between the two.
@mr_tropica
Carry out the following steps to send Serial Number (1234) from Master UNO to Slave NANO. 1. Connect UNO-Master and NANO-Slave as per Fig-1 using SPI Port.
Figure-1:
2. Upload the following sketch in UNO-AMster.
//Master sends: 1234 as Serial Number at 1-sec interval
#include <SPI.h>
int sNo = 1234; //0x04D2
void setup ()
{
Serial.begin(9600);
SPI.begin(); //LH --> SPE-bit, LH--> I-bit, LL-->SPIE
SPI.setClockDivider(SPI_CLOCK_DIV16); // 16 MHz/4 = 1 Mbits/sec
digitalWrite(SS, LOW); //Slave NANO is selcete
}
void loop()
{
SPI.transfer(highByte(sNo));
SPI.transfer(lowByte(sNo));
delay(1000);
}
3. Upload the following sketch into NANO-Slave.
#include<SPI.h>
byte rxData[2];
volatile int i = 0;
volatile bool flag = false;
void setup()
{
Serial.begin(9600);
bitSet(SPCR, MSTR);//this is Slave
bitSet(SPCR, SPE);
pinMode(SS, INPUT_PULLUP);
pinMode(MISO, OUTPUT);
SPI.attachInterrupt();
}
void loop()
{
if (flag == true)
{
int sNo = rxData[0] << 8 | rxData[1];
Serial.println(sNo, DEC);
flag = false;
Serial.println("==================");
}
}
ISR(SPI_STC_vect)
{
if (flag == false)//this flag syncheonizes data with Master
{
rxData[i] = SPDR;
i++;
if (i == 2)
{
flag = true;
i = 0;
}
}
}
5. Consider the above sketches as building blocks, add codes with Master and Slave sketches to receive the following message from Slave and show it on SM1.