How To Transfer Multiple Controls Between Setup() and Loop() ?

I am working on a project in which 2 communications are working 1st is serial comm. and 2nd is radio comm. and through serial comm, I got one value from Tx to Rx and I want to use that value in radio comm. So I want to transfer my control from setup() to loop() to get that serial value and after getting that value I want to again transfer control from loop() to setup() for radio comm.

::: TX :::

pipe=1234 // use for radio comm void setup() { ... }

void loop() { serial.write(1234); // send 1234 serialy ------*---- // code for radio comm. }

:::RX:::

pipe = I dont want to assign value in starting I want to set a value that receive from TX Serially.

void setup() { serial.begin(9600);

--** before radio comm i want to transfer control to loop() and hold setup().

radio.begin(); radio.openReadingPipe(1,pipe); radio.startListening(); }

void loop() { if (Serial.available() > 0) { val = Serial.read(); if (val == 123 && pos == 0) { digitalWrite(2, HIGH); pos = 1; } else if (val == 123 && pos == 1) { digitalWrite(2, LOW); pos = 0; } Serial.println(val); Serial.println(*value); } // If Serial Close.

if(radio.available()) {

} // IF CLOSE } //VOID LOOP CLOSE

explain again what you are trying to do...

start by describing what is connected to what and then what you would want to happen

This is not what the setup() and loop() functions are intended for. Instead create two new functions

int handleSerial(int valueFromRadio)
{
    ...
    return receivedValue;
}

int handleRadio(int valueFromSerial)
{
    ...
    return receivedValue;
}

void setup()
{
    // setup serial and radio
}

int serialValue = 0;
int radioValue = 0;

void loop()
{
    // check serial
    serialValue = handleSerial(radioValue);
    radioValue = handleRadio(serialValue);
}

Please use the code-tags to format your code. Also why are you posting your code a second time, without any text? I have already given you some hints at how to proceed.

Amongst other issues, this does not do what you think...  serial.write(1234);  // send 1234 serially

Everything is working and I got a value of PIPE through serially in RX. But I will not fetch that value from LOOP() and set it to PIPE of RX

Do you have two separate arduinos for sending and receiving (because you posted two sketches)? Could you explain your architecture a bit more?

I can guarantee that

 serial.write(1234);

does not work in the code you posted… Neither would

serial.begin(..)

Sagar_Bansal:
Everything is working and I got a value of PIPE through serially in RX.

–>that’s BS… it probably does not even compile

you are making us loose our time because you don’t provide necessary info nor answer questions.

bye

Yaa Man, I am using two Arduinos And I connected it serially as well as through NRF24. And to use NRF24 it is must to set PIPE value for uniquely identify a receiver. Now in TX I set PIPE value in starting but not in RX. Now when my TX and RX will communicate serially then I pass a copy of PIPE value from TX to RX. But now before radio communication, i want to set that value to " RX PIPE " but for me it’s impossible to break loop() of RX.

I am genuinely getting a value In RX loop but not able to set that value to RX PIPE for radio communication.

if pipe does not change once set, then wait in setup to receive the value and only then go to the loop.

if it can change at any point in time, you'll need to close the former connection and open a new one... depends on your components, libraires etc..

I am also done one thing but it was not working. I call an integer type user-defined function in setup() like:

void setup() { Serial.begin(9600) int temp = xyz(); }

and define it in loop() like:

void loop() { xyz() { if (Serial.available() > 0) { val = Serial.read(); if (val == 123 && pos == 0) { digitalWrite(2, HIGH); pos = 1; } else if (val == 123 && pos == 1) { digitalWrite(2, LOW); pos = 0; } Serial.println(val); return val; } }

it gives an out of scope error.....

Sagar_Bansal:
I am also done one thing but it was not working. I call an integer type user-defined function in setup() like:
[…]
it gives an out of scope error…

It seems that you are lacking basic knowledge about the C/C++ language. Maybe you should work through some basic tutorials to get a feel for the structure of a C/C++ program.