Hello everyone,
I have 2 Arduino Uno boards wired for SPI communication. On the first board (Master) I read some data from a potentiometer (i have to simulate a pressure sensor) and I want to send the data to the second board (slave) to display the results on an LCD. The thing is that every time I run the program, the loop freeze at the first execution, on the SPI.transfer(0x01) exactly. I tried a lot of version of different codes but without success.
Here is the code for Master:
#include <SPI.h>
int sensorValue = 0;
float presure = 0;
int Vs = 0;
void setup(){
pinMode(A0, INPUT);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SPI.begin();
Serial.begin(57600);
}
void loop()
{
// read the value from the sensor
sensorValue = analogRead(A0);
Vs = 1023;
presure = (sensorValue/Vs + 0.095) / 0.009;
Serial.print(sensorValue);
SendDataToSlave(presure);
}
void SendDataToSlave(float presure)
{
digitalWrite(10, LOW);
delay(1);
SPI.transfer(0x00); //just a mock
digitalWrite(10, HIGH);
delay(500);
}
And this one is for the slave:
#include <SPI.h>
bool data_received = false;
float presure_received = 1996;
void setup() {
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
ISR(SPI_STC_vect){
uint8_t presure_test = SPDR; // Get the presure sent from the data register of SPI
data_received = true;
}
void loop() {
//Sample code.
//This section is for sending data to LCD.
//Mock for testing only. Please delete this in the last version.
if(data_received == true)
{
Serial.print(presure_received);
}
}
I uploaded the circuit diagram in the attach
Thank you, everyone!
