Hello everyone I am having troubles in finishing my project about a communication between two Arduinos using a bidirectional communication. The first arduino controls a probe which has two stepper motors connected, which move a probe in X and Y axis'. The X moves from position 0 to 8000 and Y-axis from 0 to 10000. The probe stops on each position for 5 seconds and then proceeds to the next position. On the second Arduino I have 2 BD sensors attached which measure pressure (and air flow) on each of the positions of the previous Arduino. Now, what I need is to establish a link between the positions of the first Arduino and the readings from the sensor. After all the measurements are taken, the second Arduino needs to signal the first via a NRF24L01 WiFi module to move to the position where the highest air flow has been detected.
So far I've come up with these codes:
First Arduino (stepper motors):
#include <AccelStepper.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
AccelStepper stepperX(AccelStepper::DRIVER,8,9);
AccelStepper stepperY(AccelStepper::DRIVER,10,11);
RF24 radio (4,5);
int largestSum;
float node_A_address[32] = {0101010100111000101010010101000};
float node_B_address[32] = {0000010101010101010001010101101};
int positionX=0;
int positionY=0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.setPALevel (RF24_PA_MIN);
radio.openWritingPipe(node_B_address);
radio.openReadingPipe(1, node_A_address);
stepperX.setMaxSpeed(2000);
stepperX.setAcceleration(1500);
stepperY.setMaxSpeed(2000);
stepperY.setAcceleration(1500);
}
radio.stopListening();
while (positionY<=10000)
{
stepperY.runToNewPosition(positionY);
delay(5000);
radio.write(&positionY, sizeof(positionY));
while(positionX<=8000)
{
stepperX.runToNewPosition(positionX);
delay(5000);
radio.write(&positionX, sizeof(positionX));
positionX+=2000;
}
positionX=0;
positionY+=2000;
}
radio.startListening();
radio.read(&largestSum, sizeof(largestSum));
}
void loop() {
}
the code for the second Arduino (with the BD pressure sensors):
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const int numReadings=10;
const long int atmosferski=101325;
const int temperaturaK=293;
const int gasnakonst=287;
int deltaP=0;
float gustina=0;
float brzina=0;
float povrsina=0.0784;
int readings[numReadings];
int readIndex = 0;
int suma=0;
float Srednabrzina;
float protok=0;
const long int RT=84100;
int c=0;
const int analogInPin1=A0;
int sensorValue1=0;
int outputValue1=0;
const int analogInPin2=A1;
int sensorValue2=0;
int outputValue2=0;
static int positionX = 0;
static int positionY = 0;
RF24 radio (7,8);
float node_A_address[32] = {0101010100111000101010010101000};
float node_B_address[32] = {0000010101010101010001010101101};
void setup() {
Serial.begin(9600);
radio.begin();
radio.setPALevel (RF24_PA_MIN);
radio.openWritingPipe(node_A_address);
radio.openReadingPipe(1, node_B_address);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop()
{
radio.startListening();
sensorValue1=analogRead(analogInPin1); // читање на вредностите од сензор 1
outputValue1=map(sensorValue1,138,1023,-1000,1000); // калибрација на сензорот 1
// печатење резултати од сензор 1
Serial.print("sensor1 ");
Serial.println(sensorValue1);
Serial.print("Paskali ");
Serial.println(outputValue1);
sensorValue2=analogRead(analogInPin2); // читање на вредностите од сензор 2
outputValue2=map(sensorValue2, 138, 1023, -1000, 1000); // калибрација на сензорот 2
//печатење на резултати од сензор 2
Serial.print("sensor2 ");
Serial.println(sensorValue2);
Serial.print("Paskali ");
Serial.println(outputValue2);
deltaP = outputValue1-outputValue2;
gustina=((float(atmosferski+outputValue2)/RT));
brzina=sqrt(((2*deltaP)/gustina));
Serial.print("Delta P ");
Serial.println(deltaP);
Serial.print("Gustina ");
Serial.println(gustina);
Serial.print("Brzina ");
Serial.println(brzina);
Serial.print("readIndex ");
Serial.println(readIndex);
Serial.println("");
suma=suma-readings[readIndex];
readings[readIndex]=brzina;
suma=suma+readings[readIndex];
readIndex=(readIndex+1) % numReadings;
if (readIndex==9){
// добивање и печатење на средната брзина и протокот на воздух
Srednabrzina=suma/numReadings;
protok=Srednabrzina*povrsina;
Serial.print("Q=");
Serial.println(protok);
Serial.print("Vsr=");
Serial.println(Srednabrzina);
}
else
{
Serial.println("error opening test.txt");
}
delay(1000);
}
int sum=sensorValue1+sensorValue2;
float largestSum=0
if (protok > largestSum) {
largestSum = protok;
positionX = radio.read(&positionX, sizeof(positionX));
positionY = radio.read(&positionY, sizeof(positionY));
}
Serial.print("Largest sum: ");
Serial.println(largestSum);
delay(1000);
return 0;
radio.stopListening();
radio.write(&largestSum, sizeof(largestSum));
delay (1000);
}
Now, a suggestion of how to correlate the readings from the BD pressure sensors (variable "protok") to the positions of where the probe is while the highest value occurs.
Any help is highly appreciated.