SPI

Hi,

I need to send 2 bits through SPI protocol from one Arduino to another, but I'm not sure on how to code the slave to receive both and store them in separate variables.

Here is how I am sending the bits from the Master.

void move_x_up(){
    dir_x=HIGH;
    SPI.transfer(dir_x);   //CW
    //TON
    step_x_status = !step_x_status;
    SPI.transfer(step_x_status);
    delayMicroseconds(DELAY);
    //TOFF
    step_x_status = !step_x_status;
    SPI.transfer(step_x_status);
    delayMicroseconds(DELAY);

    //Update position
    pos_x = pos_x + mm_per_step; 
  }

It is a code to control several stepper motors. One of them is in a different Arduino board, so I have to send the direction bit, and the step signal, which is a PWM signal, and then store them in two different variables so I can write them in the corresponding pins that go to the gate driver.

I am doing this project for a university subject, and I am new to Arduino, and this is the first time I use SPI protocol so I would appreciate any help.

SPI.transfer sends whole bytes, not bits.
The receive side can just react to bits once it receives a byte.

Suggest you start by reading Nick Gammon's article here on SPI between devices and receiving on another Arduino.

martagdm:
It is a code to control several stepper motors. One of them is in a different Arduino board, so I have to send the direction bit, and the step signal, which is a PWM signal,

Stepper motors don't use PWM.

Please provide some examples of the data you want to send.

You can send several bytes with a single instruction with the SPI.transfer() function. The several bytes could be in an array or a struct. By sending all the items in a single message the receiving Arduino can't get them mixed up.

(Use I2C for communication between Arduinos - Exhibition / Gallery - Arduino Forum)]I2C for communication between ArduinosI recently wrote this Tutorial about using . Even if you don't want to use I2C the way I have organised the data can probably also be applied with SPI

...R

PS ... I just had a look at Nick Gammon's excellent SPI tutorial and I reckon I2C is easier to use for communication between two Arduinos because the standard Wire library can be master or slave.

It would be better if you use any other protocol like UART (Serial) communication. SPI is complicated if you use it for such projects where you need to share very small information like 2 bits. So I would suggest you to use Serial communication instead of SPI. SPI is complicated if you are new to programming. And it is made for transferring large amounts of data at faster speeds. Serial is good for you for this type of communication where you have to send only 2 bits. It's code is very simple and easy.

Robin2:
Stepper motors don't use PWM.

Please provide some examples of the data you want to send.

I am using A4989 gate driver, and it moves the motor one step with each low-to-high transition of the step signal.

I am using SPI because the subject staff recommended me to use it, so I want to try to make it work.

This is how I wrote part of the code for the master device.

bool dir_x;
bool step_x;

void move_x_down_y_up(){
    
    dir_x=LOW;   //CCW
    SPI.transfer(dir_x);   //CCW
    digitalWrite(dir_y,HIGH);   //CW
    //TON
    step_x_status = !step_x_status;
    step_y_status = !step_y_status;

    SPI.transfer(step_x_status);
    digitalWrite(step_y,step_y_status); 
       
    delayMicroseconds(DELAY);
    //TOFF
    step_x_status = !step_x_status;
    step_y_status = !step_y_status;

    SPI.transfer(step_x_status);
    digitalWrite(step_y,step_y_status); 
       
    delayMicroseconds(DELAY);    
    pos_x = pos_x - mm_per_step;    
    pos_y = pos_y + mm_per_step;  
  }

If I send it like this, what would the slave receive :confused: ?

martagdm:
I am using A4989 gate driver, and it moves the motor one step with each low-to-high transition of the step signal.

That driver uses step and direction inputs just like the other drivers (such as the A4988 and DRV8825) that Arduino folk are familiar with. It does not use PWM.

You have not responded to my question "Please provide some examples of the data you want to send."

...R