Multitasking: Send command signal and receive data

Hello,

Basically, the first Arduino (Ar1) has the button that sends a control signal to the second Arduino (Ar2) to tell Ar2 to turn on a digital pins. I have that part working.

Now, I want Ar1 to tell Ar2 to turn on the pins, and I want Ar2 to be taking analog data from the analog port (A0) and transmitting that data back to Ar1. So, both Arduino's need to be working as transmitters and receivers at the same time. I can do everything individually; I just have to figure out how to integrate them together.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(23, 27); // CE, CSN

const byte addresses [][6] = {"00001", "00002"};

const int chutePin = 2;

int launchState = LOW;
const int analogPin = A0;

void setup() {


  pinMode(chutePin, OUTPUT);


  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[0]);      //Setting
  radio.openReadingPipe(1, addresses[1]);
  radio.setPALevel(RF24_PA_MIN);
radio.startListening();

}
void loop() {
// delay(5);
//   radio.startListening();
if (radio.available()) {
  char txt1[5] = "";
  radio.read(&txt1, sizeof(txt1));
  switch (txt1[1]){
case '1': launchState = !launchState;
digitalWrite(chutePin, launchState); Serial.print("lol");
delay(1000);
 radio.stopListening();
 while(1);
   int sensorValue = analogRead(analogPin);
  radio.write(&sensorValue, sizeof(sensorValue));
  Serial.print("Sent Sensor Value: ");
  Serial.println(sensorValue);
break;
}
 }
}
//TRANSMITTER CODE (A1)

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const int B1_Pin = 2;
char txt1[] = "B1", txt2[]="0";


RF24 radio(7, 8); // CE, CSN

const byte addresses [][6] = {"00001", "00002"};

int sensorValue = 0;

void setup() {
  Serial.begin(9600);
  radio.begin();
  pinMode(B1_Pin,INPUT);
  radio.openWritingPipe(addresses[1]);     //Setting
  radio.openReadingPipe(1, addresses[0]);
  radio.setPALevel(RF24_PA_MIN);
}

void loop() {
  delay(5);
  radio.stopListening();
// Transmitting Light Signal
int B1_State = digitalRead(B1_Pin);

if (B1_State == HIGH){
  radio.write(&txt1,sizeof(txt1));Serial.println("Pressed1");Serial.println(" ");
  delay(1000);

    radio.startListening();
    while(!radio.available());            
    radio.read(&sensorValue, sizeof(sensorValue));
    Serial.print("Received Sensor Value: ");
    Serial.println(sensorValue);
}
else {
  radio.write(&txt2, sizeof(txt2));
  };

   
}

You’ve been here a while, have you read about making non blocking delays using the millis( ) function ? :wink:

When A1 asks A2 to make a reading and send the result A1 needs to keep quite, not transmit anything but wait for the reply from the A2. Any security arrangement like CRC code?
Then You need to consider the situation that the received data is corrupted or worse, there's no reply coming from A2.
The name for this is "Protocol".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.