Untitled
unknown
plain_text
a year ago
1.4 kB
11
Indexable
#include <Arduino.h>
#include <ESP32SPISlave.h>
#define CS_PIN 5
const int buzz = 17;
ESP32SPISlave spiSlave;
static constexpr size_t BUFFER_SIZE = 8;
uint8_t tx_buf[BUFFER_SIZE] = {0}; // Data to send
uint8_t rx_buf[BUFFER_SIZE] = {0}; // Buffer for received data
void setup() {
Serial.begin(115200);
pinMode(buzz, OUTPUT);
pinMode(CS_PIN, INPUT_PULLUP); // Use pull-up for CS_PIN
// Initialize the SPI slave
if (!spiSlave.begin(VSPI, 18, 19, 23, CS_PIN)) { // SCK=18, MISO=19, MOSI=23, CS=CS_PIN
Serial.println("Failed to initialize SPI slave!");
while (true);
}
}
void loop() {
if (digitalRead(CS_PIN) == LOW) { // Check if CS is active
size_t received_bytes = spiSlave.transfer(tx_buf, rx_buf, BUFFER_SIZE);
if (received_bytes > 0) {
// Print received data
Serial.print("Received data: ");
for (size_t i = 0; i < received_bytes; i++) {
Serial.print(rx_buf[i]);
Serial.print(" ");
}
Serial.println();
// Play sound if received data is 1
if (rx_buf[0] == 1) {
Serial.println("Playing sound...");
tone(buzz, 2100, 100); // Play tone for 100 ms
}
}
}
delay(10); // Reduced delay for faster polling
}Editor is loading...
Leave a Comment