I'm trying to set up SPI communication between two Arduinos (UNO and Mega), I want to read the status of the slave output pin from the master.
I connected a diode to pin 9 of Mega and I wanted to display its state on the virtual terminal of the master (Uno). The problem is that when the LED flashes I do not receive values just at the terminal level but when I hold it to zero or 1 the display is correct. I used another code I found online.
MASTER code
#include <SPI.h>
const byte btn =8;
void setup(void) {
Serial.begin (9600);
pinMode (btn, INPUT_PULLUP);
digitalWrite (SS, HIGH);
SPI.begin ();
SPI.setClockDivider (SPI_CLOCK_DIV8);
}
void loop(void) {
byte Mvalsent,Mvalreceived;
digitalWrite (SS, LOW);
for (int jj=0; jj<255; jj++)
{
Mvalsent =jj;
Mvalreceived =SPI.transfer (Mvalsent);
Serial.print ("Retou d'etat ");
Serial.print (Mvalsent);
Serial.print ("\t ,l'etat de la diode est ");
Serial.println (Mvalreceived);
delay (400);
}
digitalWrite (SS,HIGH);
}
SLAVE Code
#include <SPI.h>
byte state;
const byte led =9;
volatile boolean process_it;
volatile byte Svalreceived, Svalsent;
int jj=0;
void setup (void)
{
Serial.begin (9600);
pinMode (led,OUTPUT);
//set MISO pin as output
pinMode (MISO, OUTPUT);
//turn on SPI in slave mode
SPCR |= _BV (SPE);
//get ready for an interrupt
process_it = false;
//now turn on interrupts
SPI.attachInterrupt ();
}
ISR (SPI_STC_vect)
{
Svalreceived = SPDR;
process_it =true;
}
void loop (void)
{
if (process_it)
{
digitalWrite (led, LOW);
state =digitalRead(led);
SPDR = state;
Serial.print("l'etat de la diode est: ");
Serial.println (state);
delay (400);
process_it =false;
}
}
-
Not too familiar with arduino, but this seems to be a simple case of sampling. You also need to clarify what you mean by "I do not receive values just at the terminal level" - do you get always incorrect values? Random values? Or does it not read at all?kabZX– kabZX02/22/2017 16:08:55Commented Feb 22, 2017 at 16:08
1 Answer 1
Why not use I2C slave-master between arduinos? You can hook up other arduinos or devices on I2C simultaneously (as long as each uses a distinct address).
And you still keep the SPI for a microsd card adapter, for example.
Follow this official tutorial:
MASTER ARDUINO 1
// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
}
byte x = 0;
void loop() {
Wire.beginTransmission(8); // transmit to device #8
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;
delay(500);
}
SLAVE ARDUINO 2
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
This is an alternative example of a setup using a nodemcu (which doesnt function in slave mode, btw).