UDP conexión raspberry to arduino.

his is my code
When there are changes of value in the rasberry it is not updated immediately in arduino the previous value is maintained for a moment. how to fix it

input signal is a constant
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

int led1 = 6;
int led2 = 7;
//DIRECION MAC DEL ETHERNET
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(169,254,0,55);

unsigned int localPort = 42500;

EthernetUDP Udp;

byte packetBuffer [UDP_TX_PACKET_MAX_SIZE];
byte ReplyBuffer []="acknowledged";

void setup(){
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
pinMode (6,OUTPUT);
pinMode (7,OUTPUT);
}

void loop(){

int packetSize = Udp.parsePacket();
if (packetSize>0){
Serial.print("Recibido mensaje de tamaño ");
Serial.println(packetSize);
Serial.print("From ");

IPAddress remote = Udp.remoteIP();
for (int i=0; i < 4; i++) {
Serial.print(remote, DEC);
if (i < 3) {
Serial.print(".");
}
}

Serial.print(", port ");
Serial.println(Udp.remotePort());

Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.print("Contenido:");

int a1=packetBuffer[0];
Serial.print(a1);
if (a1==30) {

digitalWrite(led1 , HIGH);
delay(1000);
digitalWrite(led1 , LOW);
delay(1000);}
else {
if (a1==40) {
digitalWrite(led2 , HIGH);
delay(1000);
digitalWrite(led2 , LOW);
delay(1000);

}} }

delay(100);
}

Why did you start another thread on the same topic?

Your code indenting is horrid. Put EVERY { on a line by itself. Put every } on a line BY ITSELF. Use Tools + Auto Format to properly indent your code.

Then, ask yourself what a packet size of 0 means. When the packet size is 0, does it make sense to read the packet?

And, forget about writing responsive code when you litter your code with delay()s.