Spi communication between two arduino boards


I wanna communicate through spi.
can some one help to understand the issue here ?
I wanna print as "greetings #### in slave arduino connected LCD"
master arduino code :

#include <Wire.h>
#include <Adafruit_LiquidCrystal.h>
#include <Keypad.h>

// Keypad configuration
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

// Define keypad layout
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; // Pin assignments for rows
byte colPins[COLS] = {5, 4, 3, 2}; // Pin assignments for columns

// Create the keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

// Initialize the LCD
Adafruit_LiquidCrystal lcd_1(0);

// Define variables
String inputString = "";  // To store the 4-digit input
int maxDigits = 4;        // Maximum number of digits allowed

void setup() {
  lcd_1.begin(16, 2);   // Initialize the LCD to 16x2 dimensions
  lcd_1.print("Enter Reg.no:");  // Display prompt message
  delay(2000);  // Wait for 2 seconds before starting the loop
  lcd_1.clear();
}

void loop() {
  // Check if a key is pressed on the keypad
  char key = customKeypad.getKey();
  if (key) {
    // If key is pressed, handle it
    if (key == '#') {  // Use '#' key to submit the input
      lcd_1.clear();
      lcd_1.setCursor(0, 0);
      lcd_1.print("Reg.no: ");
      lcd_1.print(inputString);  // Display the entered number
      delay(500);  // Wait 2 seconds before clearing
      lcd_1.clear();
      inputString = "";  // Clear the input after submission
      lcd_1.print("Reg.no:");  // Prompt again for new input
    } else if (key == '*') {  // Use '*' key to clear input
      inputString = "";  // Clear the current input string
      lcd_1.clear();
      lcd_1.print("Reg.no:");
    } else if (inputString.length() < maxDigits) {
      // Append the pressed key to the input string if under 4 digits
      inputString += key;
      lcd_1.clear();
      lcd_1.setCursor(0, 0);
      lcd_1.print("Reg.no: ");
      lcd_1.print(inputString);  // Display the entered digits so far
    }
  }
}

slave arduino code :

#include <Wire.h>
#include <Adafruit_LiquidCrystal.h>

Adafruit_LiquidCrystal lcd_1(0);

void setup() {
  lcd_1.begin(16, 2);  // Initialize the LCD
  lcd_1.print("Waiting...");  // Display waiting message

  Wire.begin(8);  // Start I2C communication, address 8
  Wire.onReceive(receiveData);  // Register the function to receive data
}

void loop() {
  // Nothing to do in the loop, just wait for incoming data
}

// Function to handle received data from Master Arduino
void receiveData(int byteCount) {
  String receivedString = "";  // Variable to store the received string

  while (Wire.available()) {
    char receivedChar = Wire.read();  // Read each byte
    receivedString += receivedChar;  // Append the byte to the string
  }

  lcd_1.clear();
  lcd_1.setCursor(0, 0);
  lcd_1.print("Reg.no: ");  // Display the received registration number
  lcd_1.print(receivedString);  // Display the entered code
}

You say you want to use SPI, but your code is using Wire (I2C). If you want to use SPI see https://docs.arduino.cc/language-reference/en/functions/communication/SPI/ Can you clarify whether you really want to use SPI or I2C?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.