Help me with the code

I have 2 devices ( Sender and Receiver )

On Receiver

  1. put your response code in here. For now, it just adds 1 to it
    On Sender:
  2. put your code here to print a success or failure.

//This program issues the challenge, listens for the response and determines if it's correct or not
long int challenge, response;
char message[20];
int messageType; // 0 is challenge, 1 is response
void setup() {

// Begin the Serial at 9600 Baud

Serial.begin(9600);

// message with a 0 is a challenge. Send only once and wait for response.
challenge = 125;
messageType = 0;
sprintf(message, "%1d %8d", messageType, challenge);
Serial.write(message,20); //Write the serial data
delay(1000);

}

void loop() { //expecting a response to the challenge
if (Serial.available() > 0) {
Serial.readBytes(message,20); //Read the response
sscanf(message, "%1d %8d", &messageType, &response);
Serial.println("");
Serial.println(message);
if ((messageType == 1)){ //It's a response to the challenge

      //*********************put your code here to print a success or failure. *********************************
     
      Serial.print("response received "); Serial.println(response);
      }

delay(1000);
}

}

****************** Reciver ************************

//This program responds to the challenge
char message[20]; //Initialized variable to store recieved data
int messageType; //0 = challenge, 1 = response
long int challenge, response;

void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
Serial.readBytes(message,20); //Read the serial data and store in var
sscanf(message, "%1d %8d", &messageType, &challenge);
// Serial.print(" "); Serial.print(messageType); Serial.print(" "); Serial.println(challenge); //Print data on Serial Monitor
if (messageType == 0){ //send a response back, otherwise ignore it
//*****put your response code in here. For now it just adds 1 to it
messageType = 1;
response = challenge + 1;
sprintf(message, "%1d %8d", messageType, response);
Serial.write(message,20);
}
delay(1000);
}
}

Is this not working? From what I could understand from the code, the challenge sending should not be done the way it is in the setup(). What this will do is simply send the challenge as soon as the board is powered up or reset. It has to be event driven at the challenger side - by a button press or something. So implement an interrupt routine attached to one of the interrupt inputs that sends the challenge and in its loop() wait for the response. The receiver side has to just listen in the loop() and send a response when it gets the challenge.

Yuh, exactly.

I need to
On Reciver

  1. put your response code in here. For now, it just adds 1 to it
    On Sender:
  2. put your code here to print a success or failure.

On the challenger side, move the sending code into an interrupt handler. If you must use loop() then check if the button is high (or low, if pulled up) then send the challenge else simply wait for serial data to be available.

You are using two separate boards, right? One for the sender and one for the receiver...?

Yes I have two Arduino uno

this line generated an error (have preferences set to warning More) because the 2nd value is long and the format needs to be "8ld" (8 el d)

code worked when manually entering input thru serial monitor