How to reduce the wait time on button functions.

At the moment I have 4 buttons and a switch to control various functions. The problem is there is up to a 1sec delay between pushing the button and seeing the result.

I have no delay functions in the loop.

I would like a much quicker response from a button press.

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>

//=======For the radio======

#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Singleton instance of the radio driver
RH_NRF24 driver;
// RH_NRF24 driver(8, 7);   // For RFM73 on Anarduino Mini

// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
uint8_t radioArray[5];//Array with 6 positions.
// Dont put this on the stack:
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

//====For the buttons====
const byte button0pin = 4;
const byte button1pin = 5;
const byte button2pin = 6;
const byte button3pin = 7;
const byte switch4pin = 3;
byte button0state;
byte button1state;
byte button2state;
byte button3state;
byte switch4state;

int SetpointVariable = 30;
int rpm;
int runState = 0;

String Str1 = "On";
String Str2 = "Off";
String Str3;

void setup() {

  pinMode(button0pin, INPUT_PULLUP);
  pinMode(button1pin, INPUT_PULLUP);
  pinMode(button2pin, INPUT_PULLUP);
  pinMode(button3pin, INPUT_PULLUP);
  pinMode(switch4pin, INPUT_PULLUP);

  lcd.init();                      // initialize the lcd
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3, 0);
  lcd.print("Lava Valley");
  lcd.setCursor(5, 1);
  lcd.print("Produce");
  delay(2000);
  lcd.clear();
  lcd.print("Seedr Controller");
  lcd.setCursor(1, 1);
  lcd.print("by Chris Dalby");
  delay(2000);
  lcd.clear();
  lcd.print("Set RPM  ");
  lcd.print(SetpointVariable);
  Serial.begin(9600);
  if (!manager.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm



}

void loop()
{

  readButtons();
  adjRequiredRpm();
  setStartStop();
  exchangeData();
  displayRpm();

  //  sendDatatoServer();//Send RequiredRpm & Start/Stop to Server.

}

void readButtons() {
  button0state = digitalRead(button0pin);
  button1state = digitalRead(button1pin);
  button2state = digitalRead(button2pin);
  button3state = digitalRead(button3pin);
  switch4state = digitalRead(switch4pin);

  //for debugging.
  Serial.print(button0state);
  Serial.print(button1state);
  Serial.print(button2state);
  Serial.print(button3state);
  Serial.println(switch4state);
}

void setStartStop() {
if (switch4state == LOW) {
  runState=1;
  if (runState==1) {
    Str3=Str1;
  }
  
}
if (switch4state == HIGH) {
  runState=0;
if (runState==0) {
    Str3=Str2;
  }
}
}
void adjRequiredRpm() {
  if (button0state == LOW) {
    SetpointVariable = SetpointVariable + 1;
    if (SetpointVariable > 60) {
      SetpointVariable = 60;
    }
  }
  if (button1state == LOW) {
    SetpointVariable = SetpointVariable - 1;
    if (SetpointVariable < 10) {
      SetpointVariable = 10;
    }
  }


}


void listenForServer() {
  /*    if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
    {
      uint8_t len = sizeof(buf);
      uint8_t from;
      if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
      {
        Serial.println((char*)buf);
      }
        }  */
}

void displayRpm() {
  lcd.clear();
  lcd.print("Set RPM  ");
  lcd.print(SetpointVariable);
  lcd.setCursor(0, 1);
  lcd.print("Motor ");
  lcd.print(Str3);
  lcd.print(" RPM ");
  lcd.print(rpm);
}



void exchangeData()  {
  if (manager.sendtoWait(radioArray, sizeof(radioArray), SERVER_ADDRESS))
  {
    Serial.println("Message sent, waiting for reply");
    // Now wait for a reply from the server
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
    {
      Serial.print("got reply from : 0x");
      Serial.print(from, HEX);
      Serial.print(": ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("No reply, is nrf24_reliable_datagram_server running?");
    }
    /*char msg[4];
      itoa(data, msg, 10);

      driver.send((uint8_t *)msg, strlen(msg));
      driver.waitPacketSent();*/
  }
}

What do mean by result, in "pushing the button and seeing the result"?

In the main loop, your program is doing many other things than reading the buttons. Those things must take some time.

Is 2000 below a timeout value in milliseconds ?
if (manager.recvfromAckTimeout(buf, &len, 2000, &from)) . . .

Such statements could cause delays in your loop.

Found it. It was in the radio, "wait for reply".

edit: I see you found it too 6v6gt.

Although, I changed the 2000 to 200 but it made no difference. This is code I sourced from the nrf24 radio client example so not sure what the variables in these brackets are.

Now I don't know how to keep the loop running while the radio is waiting for a reply.

I presume you are using nRF24l01+ transceivers. I am not familiar with the library you are using.

I use the TMRh20 version of the RF24 library

The pair of programs in this link exchange data without any delays.

...R