issues with spi slave, with ultrasonic sensor.


i have a issue with my spi config, i have an ultrasonic sensor loop, and it block the response of my spi comunication, end only the byte i was receive from the master, i processed the data received from the master but i can´t sent back again the result.

can you help me with this?
im based on the gammon scketch, Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino

this is my slave code

#define MEDIA_VELOCIDAD_SONIDO 0.017175 // Mitad de la velocidad del sonido a 20 °C expresada en cm/µs
#define PIN_TRIGGER 6
#define PIN_ECHO 7
#define ESPERA_ENTRE_LECTURAS 10 // tiempo entre lecturas consecutivas en milisegundos
#define TIMEOUT_PULSO 25000 // la espera máxima de es 30 ms o 30000 µs
#include <SPI.h>

volatile byte command = 0;
byte distancia1;
float distancia2;
int p;
byte q;
int x;
unsigned long tiempo;
unsigned long cronometro;
unsigned long reloj=0;
int r = 0;

void setup (void)
  digitalWrite(PIN_TRIGGER,LOW); // Para «limpiar» el pulso del pin trigger del módulo

  pinMode(MISO, OUTPUT);
  SPCR |= _BV(SPE);
  SPCR |= _BV(SPIE);

ISR (SPI_STC_vect)
  volatile byte w;
  byte c = SPDR;
  switch (command)
    case 0:
      command = c;
      SPDR = 0;

    case 1:
     if (distancia2 > c + 1 ){
      q= distancia2 - c;
      w= (q/0.2) + 1;
      //Serial.print("w es= ");
     // SPDR= w;
     else if (c - 1 <= distancia2 <= c + 1){
     w = 23;
     //Serial.print("w es= ");
     else if (distancia2 < c - 1 ){
      q= c - distancia2;
      w= (q/0.2) + 1;
     // Serial.print("w es= ");
      //SPDR= w;
     Serial.print("w es= ");
     SPDR= w;
     delayMicroseconds (2000);


void loop (void)
  if (digitalRead (SS) == HIGH)
    command = 0;

  void sus()
       digitalWrite(PIN_TRIGGER,HIGH); // Un pulso a nivel alto…
       delayMicroseconds(10); // …durante 10 µs y
       digitalWrite(PIN_TRIGGER,LOW); // …volver al nivel bajo
       tiempo=pulseIn(PIN_ECHO,HIGH,TIMEOUT_PULSO); // Medir el tiempo que tarda en llegar un pulso
       Serial.println(" cm"); 

You have to restructure your program. You can’t have blocking code or Serial prints called from an interrupt service routine.

Don’t synchronize the reading of the sensor with the SPI request, simply have the code to operate the sensor running continuously in the loop() calculating the distance and any values derived from that.
When the SPI request comes in, send back the current value of distance etc. in the interrupt service routine.