Arduino lento

Salve a tutti,
Ho realizzato. anche con l'aiuto di lesto, un un codice per arduino + GUI Processing che mi permette di visualizzare la velocità di un motore, il numero delle volte in cui si e chiuso un pulsante e mi permette anche l'attivazione di alcuni relè.Tutto ovviamente gestito da grafica.Il tutto funziona anche se noto una lentezza.
Quando provo ad attivare un relè, il comando viene eseguito da arduino dopo alcuni secondi e non subito.Come potrei fare per velocizzare?

I codici sono i seguenti:
Processing

// Example by Tom Igoe

import processing.serial.*;
//IMPOSTAZIONI GRAFICHE//
int width = 800;
int height = 600;
int statoluci= 0;
int statomot= 0;
int button1X;
int button1Y;
int button1Width = width/10;
int button1Height = height/20;
int button2X;
int button2Y;
int button3X;
int button3Y;
int button4X;
int button4Y;
int button2Width = button1Width;
int button2Height = button1Height;
int button3Width = button1Width;
int button3Height = button1Height;
int button4Width = button1Width;
int button4Height = button1Height;
float buttonFont = button1Height/1.5;
float wordFont = button1Height;
color buttonColor, buttonHighlight, buttonPressed;
PFont font;
boolean button1Over = false;
boolean button2Over = false;
boolean button3Over = false;
boolean button4Over = false;
int velocita;
int pass;
//END//
Serial myPort;    // The serial port
     // The display font
String inString;  // Input string from serial port
int lf = 10;      // ASCII linefeed

void setup() {
  size(width,height);
  smooth();
  // Make your own font. It's fun!
  font = loadFont("CourierNew36.vlw");
  buttonColor = color(255);
  buttonHighlight = color(200);
  buttonPressed = color(50);
  button1X = width/10 - button1Width/2;
  button1Y = height/5 - button1Height/2;
  button2X = 2*width/10 - button1Width/2;
  button2Y = height/5 - button1Height/2;
    button3X = 3*width/10 - button1Width/2;
  button3Y = height/5 - button1Height/2;
      button4X = 3*width/10 - button1Width/2;
  button4Y = height/5 - button1Height/2;
  
  textFont(font, 18);
  // List all the available serial ports:
  //println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil(lf);
}

void draw() {
  background(200);
  noFill();
  stroke (255);
strokeWeight(2);
  rect(width/25, height/13,280,100);
  strokeWeight(2);
  rect(width/2.3, height/13,405,300);
    strokeWeight(2);
  rect(width/25, height/3.5,280,175);
      strokeWeight(2);
  rect(width/25, height/1.6,720,175);
  update(mouseX,mouseY);

  
  if(button1Over) {
    fill(buttonHighlight);
  } else {
    fill(buttonColor);
  }
  stroke(0);
  rect(width/8,height/5.8, 132, button1Height);
  
  if(button2Over) {
    fill(buttonHighlight);
  } else {
    fill(buttonColor);
  }
  stroke(0);
  rect(width/1.72,height/5.8, 132, button2Height);
  
  if(button3Over) {
    fill(buttonHighlight);
  } else {
    fill(buttonColor);
  }
  stroke(0);
  rect(377,250, 134, button3Height);

    if(button4Over) {
    fill(buttonHighlight);
  } else {
    fill(buttonColor);
  }
  stroke(0);
  rect(530,250, 185, button3Height);
  
  
  textFont(font, buttonFont);
   textFont(font, wordFont);
  fill(0);
  text("Controllo Luci",width/20,height/8);
    fill(0);
  text("Controllo Motore",width/1.9,height/8);
      fill(0);
  text("Produzione",width/20,height/3);
  fill(0);
  if(statoluci == 0)
  {
  text("Accendi",width/7.6,height/4.7);
  }else if(statoluci == 1)
  {
    text("Spegni",width/7.6,height/4.7);
    }
    
    
      if(statomot == 0)
  {
  text("Accendi",width/1.7,button1Y + button1Height/1.3);
  }else if(statomot == 1)
  {
  text("Spegni",width/1.7,button1Y + button1Height/1.3);
    }

  text("Velocità",width/1.7,height/2.9);
     text("Aumenta",width/2.1,height/2.2);
   text("Diminuisci",width/1.5,height/2.2);
 textFont(font, wordFont);
  fill(0);
 // text("Accendi/Spegni Led ",button3X + button1Width + width/20,button1Y + button1Height/1.2);
    fill(0);
  text("Velocità: " + velocita, 40,248);
  text("Carrelli: " + pass, 40,310);
      
}
void update(int x, int y) {
  
  if(overButton(width/8,height/6,width/6,button1Height)){
    button1Over = true;
  } else {
    button1Over = false;
  }
  if(overButton(462,height/6,135,button2Height)){
    button2Over = true;
  } else {
    button2Over = false;
  }
  //
  if(overButton(381,250,130,button3Height)){
    button3Over = true;
  } else {
    button3Over = false;
  }
  
    if(overButton(530,250,180,button4Height)){
    button4Over = true;
  } else {
    button4Over = false;
  }
}

  



void mousePressed() {
  
  if(button1Over) {
    println("Button1 pressed");
    if(statoluci == 0)
    {
   myPort.write('A');
  statoluci=1;
  }else if(statoluci == 1)
  {
      myPort.write('B');
    statoluci=0;
    }
    fill(buttonPressed);
    rect(width/8,height/5.9, width/6, button1Height);
  }
  if(button2Over) {
    println("Button2 pressed");
    if(statomot == 0)
    {
    myPort.write('C');
  statomot=1;
  }else if(statomot == 1)
  {
      myPort.write('D');
    statomot=0;
    }

    fill(buttonPressed);
    rect(465,height/5.8, 130, button2Height);
  }
  if(button3Over) {
  println("Button3 pressed");
    myPort.write('E');
   fill(buttonPressed);
    rect(381,250, 130, button3Height);

  }
  
  if(button4Over) {
  println("Button4 pressed");
     myPort.write('F');
   fill(buttonPressed);
    rect(530,250, 180, button4Height);

  }
}

boolean overButton(int x, int y, int width, int height) {
  
  if(mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
  
}
void serialEvent(Serial p) {
 inString = (myPort.readString());
int sensors[] = int(split(inString, ','));
velocita=sensors[0];
pass=sensors[1];
}

Arduino

#define lucion 13//pin luci accese
#define lucioff 12//pin luci spente
#define moton 11//pin accensione motore
#define motoff 10//pin spegnimento motore
#define velon 9//pin aumento velocità motore
#define veloff 8//pin diminuizione velocità motore
#define conta A0 //pin conteggio carrelli
#define velsens A1//pin magnete velocità



//storage variables
int reedVal;
long timer;// time between one full rotation (in ms)
float mph;
float radius = 13.5;// tire radius (in inches)
float circumference;
int val;
int car=0;
int pass=0;
int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;
 void setup(){
   Serial.begin(9600);
   
  pinMode(lucion, OUTPUT);//
  pinMode(lucioff, OUTPUT);//
  pinMode(moton, OUTPUT);//
  pinMode(motoff, OUTPUT);//
  pinMode(velon, OUTPUT);//
  pinMode(veloff, OUTPUT);//          
  pinMode(velsens, INPUT);//sensore velocità
 pinMode(conta, INPUT);
    reedCounter = maxReedCounter;
  circumference = 2*3.14*radius;

  
  // TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch
  //for more info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
  cli();//stop interrupts

  //set timer1 interrupt at 1kHz
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;
  // set timer count for 1khz increments
  OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS11 bit for 8 prescaler
  TCCR1B |= (1 << CS11);   
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  
  sei();//allow interrupts
  //END TIMER SETUP
   }
   ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure reed switch
  reedVal = digitalRead(velsens);//get val of A0
  if (reedVal){//if reed switch is closed
    if (reedCounter == 0){//min time between pulses has passed
      mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
      timer = 0;//reset timer
      reedCounter = maxReedCounter;//reset reedCounter
    }
    else{
      if (reedCounter > 0){//don't let reedCounter go negative
        reedCounter -= 1;//decrement reedCounter
      }
    }
  }
  else{//if reed switch is open
    if (reedCounter > 0){//don't let reedCounter go negative
      reedCounter -= 1;//decrement reedCounter
    }
  }
  if (timer > 2000){
    mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
  }
  else{
    timer += 1;//increment timer
  } 
}
void displayvalue(){
 pass = digitalRead(A1);
 if(pass != 0)
 {
   car++;
   }

  Serial.print(mph);
  Serial.print(",");
  Serial.println(car);
 delay(1000);      
}

  void loop()
  {
    if(Serial.available() >0)
    {
       val= Serial.read();
      if(val == 'A')
      {
  digitalWrite(lucion, HIGH);
  delay(1000);
  digitalWrite(lucion, LOW);
      }else if(val == 'B')
      {
  digitalWrite(lucioff, HIGH);
  delay(1000);
  digitalWrite(lucioff, LOW);
  
      }else if(val=='C')
      {
  digitalWrite(moton, HIGH);
  delay(1000);
  digitalWrite(moton, LOW);
      }else if(val =='D')
      {
  digitalWrite(moton, HIGH);
  delay(1000);
  digitalWrite(moton, LOW);
        }else if(val == 'E')
        {
  digitalWrite(velon, HIGH);
  delay(1000);
  digitalWrite(velon, LOW);
        }else if(val == 'F')
        {
  digitalWrite(veloff, HIGH);
  delay(1000);
  digitalWrite(veloff, LOW);     
          }
    }
 pass = digitalRead(A1);
 if(pass != 0)
 {
   car++;
   }
 //  print mph once a second
  displayvalue();
   
  }

Ho fatto multiposting perchè nel postare il forum diceva che il messaggio era troppo lungo. scusate
Ringrazio tutti in anticipo per l'aiuto

Dentro la funzione displayvalue() c'è un delay di 1 secondo.
Se poi va a coincidere con la lettura da seriale si somma ad un delay presente nell'if e il ciclo successivo avverrà 2 secondi dopo.
I delay così lunghi sono deleteri.
Usa la funzione millis() per richiamare la funzione displayvalue() ogni secondo oppure la libreria looper di Leo.
Anche il funzionamento dei relè andrebbe rivisto per eliminare l'altro delay.

Al posto degli if a casata uno uno switch case --> C/Blocchi e funzioni/Blocchi if e switch - Wikibooks, manuali e libri di testo liberi