Good morning, I am already feeling bad for asking my 2nd question on this topic, but I don't know much about the details of the SPI.
Thanks to GolamMostafa, I was able to create an SPI connection between an Arduino Nano (Slave) and an Esp8266 (Master) so that the Master can RECEIVE data from the Slave, this works perfectly fine.
But there is a problem, and that is that this project is portable, the Arduino Nano (Slave) must always be on (but when not in use it would be in Deep Sleep so as not to waste energy). And when I order you to wake up with a button, it will wake up and turn on the Esp8266 (but I will not turn it on via SPI, but by another pin, that is already resolved).
=============
Arduino Nano Code
#include "Adafruit_Keypad.h"
#include <SPI.h>
char a;
const byte ROWS = 8;
const byte COLS = 5;
char keys[ROWS][COLS] = {
{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','W','X','Y'},
{'Z','a','b','c','d'},
{'e','f','g','h','i'},
{'j','k','l','m','n'},
};
byte rowPins[ROWS] = {3, 4, A5, A4, A3, A2, A1, A0};
byte colPins[COLS] = {5, 6, 7, 8, 9};
Adafruit_Keypad customKeypad = Adafruit_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
//Serial.begin(115200);
customKeypad.begin();
/*SPI.setClockDivider(SPI_CLOCK_DIV16);
pinMode(SS, INPUT_PULLUP);
pinMode(MISO, OUTPUT);
SPCR |= _BV(SPE);
SPCR |= !(_BV(MSTR));
SPI.attachInterrupt();*/
}
void loop() {
customKeypad.tick();
while(customKeypad.available()){
keypadEvent e = customKeypad.read();
if(e.bit.EVENT == KEY_JUST_PRESSED) {
a = (char)e.bit.KEY;
}
else if(e.bit.EVENT == KEY_JUST_RELEASED) {
a = 1;
}
if ((char)e.bit.KEY == 'A') {
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16);
pinMode(SS, INPUT_PULLUP);
pinMode(MISO, OUTPUT);
SPCR |= _BV(SPE);
SPCR |= !(_BV(MSTR));
SPI.attachInterrupt();
} else if ((char)e.bit.KEY == 'B') {
SPI.end();
}
}
delay(10);
}
ISR(SPI_STC_vect)
{
SPDR = a;
}
==========
Esp8266 Code
#include<SPI.h>
byte myData;
byte buff = 0;
void setup() {
Serial.begin(115200);
SPI.begin();
delay(100);
SPI.setClockDivider(SPI_CLOCK_DIV16);
pinMode(SS, OUTPUT);
digitalWrite(SS, LOW);
}
void loop() {
myData = SPI.transfer(myData);
if (myData != 0 && myData != 1) {
if (buff != myData) {
buff = myData;
Serial.println(buff);
}
} else {
buff = myData;
}
delayMicroseconds(1000);
}
=====
Circuit
Arduino Nano - Esp8266
10 <------> D8
11 <------> D7
12 <------> D6
13 <------> D5
I am going to describe exactly the things that happen so that there are no problems... To simulate things, first, nothing is connected (Arduino and Esp are without power), first I connect the Arduino Nano, then I connect the Esp8266 (no matter how much time elapses since I connect the Arduino and the Esp8266), then I press the "A" button (the Arduino is connected to a Matrix keyboard), this button causes the SPI to be configured (cut what was in "void setup () "to make him do that when he pressed this button). By doing all this it works perfectly fine, the Esp8266 receives the data of all the buttons read by the Arduino, but when I disconnect the Esp8266 (remember that the Arduino will always stay on, this simulates that the project is turned off and is not being used by the moment) and I reconnect it, SPI communication does not work, so I also tried to put a function by pressing button "B" to make it "SPI.end ()", but it does not work.
What I was trying to do was press the reset button on the Arduino Nano while the Esp8266 was off, I reconnected the Esp and it still didn't read anything, disconnected it, I pressed the reset button on the Nano again and this time if it worked. Simulate this over and over again, in the end I realized that since I connected it, this pattern continues, works - doesn't work - works - doesn't work - works. I don't know why it does that, I don't know much about this protocol, but I need to use SPI because I don't have extra pins left (this I say because the Esp8266 has to use SPI to read an SD and I run out of pins, so I can't use I2C).
I'm sure it has something to do with initializing the SPI, I have no idea how to solve it, it must be a technical detail that I can't see.
Thank you!!!