Drehscheibe Modellbahn mit RF24

Guten Abend,

danke für deine Antwort.

zu 1. es sind diese Module https://www.amazon.de/SainSmart-NRF24L01-drahtlos-Transceiver-2-4GHz/dp/B006CHFPFU

zu 2. Versorgt werden sie momentan über den Arduino NANO am 3,3Volt Ausgang mit Kondensator zur Masse

zu 3. Mit dem While das ist durch das testen entstanden. In manchen Codes steht while in anderen if.

zu 4. Das mit Kommentaren ist auch durch brobieren zu Stande gekommen.

Habe den Code nochmal geändert. Das Empfangen funktioniert super nur das zurück Senden nicht.

Wie gesagt der Sender ist nur zum testen. Ich habe die Variable Schritte immer händisch abgeändert.
Mit den Orginal Scetches von https://www.instructables.com/id/Arduino-and-NRF24L01/ hat das gegenseitige Senden und Empfangen funktioniert.

Hier meine Codes:
Sender (Provisorisch)

/*
  Arduino NRF24L01 simple transmit and receive - Created October 2017
    by DonX Developer
  *** This code was created for an Arduino UNO using the Arduino IDE v1.8.4. ***

  Parts:
  2 X Arduinos
  2 X Breadboards
  2 X NRF24L01
  [optional] 2 X YL-105 breakout boards for the NRF24L01. This allows for 5v connection and easier wiring
  2 X Momentary switches
  2 X Red LEDs
  2 X Yellow LEDs
  4 X 220 ohm resistors
  Jumper wires


  NRF24L01  Arduino pin
  VCC       3.3 V
  GND       GND
  CS        8
  CE        7
  MOSI      11 or ICSP-4
  MISO      12 or ICSP-1
  SCK       13 or ICSP-3

  If you use the YL-105 breakout board, the Vcc lead can go to the 5v Arduino pin

  Arduino pin 2 to Yellow LED long lead - anode
  Yellow short lead - cathode to 220ohm resistor, then second resistor lead to GND

  Arduino pin 3 to Red LED long lead - anode
  Red short lead - cathode to 220ohm resistor, then second resistor lead to GND

  Arduino pin 4 to switch, other side of switch to GND

  The physical build of the two boards are identical
  There are minor software differences for the reading and writing pipes for the respective boards.

*/

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

#define button 4
#define confirmLed 2
#define led 3

RF24 NRF24L01 (8, 9);//create object called NRF24L01. specifying the CE and CSN pins to be used on the Arduino

byte address[] [6] = {"pipe1", "pipe2"};//set addresses of the 2 pipes for read and write
boolean buttonState = false;//used for both transmission and receive
byte schritte[1];
void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT_PULLUP);
  pinMode(confirmLed, OUTPUT);//yellow LED
  pinMode(led, OUTPUT);//red LED

  NRF24L01.begin();
  //open the pipes to read and write from board 1
  NRF24L01.openWritingPipe(address[0]);//open writing pipe to address pipe 1
  NRF24L01.openReadingPipe(1, address[1]);//open reading pipe from address pipe 2

  NRF24L01.setPALevel(RF24_PA_MAX);//set RF power output to minimum, RF24_PA_MIN (change to RF24_PA_MAX if required)
  NRF24L01.setDataRate(RF24_250KBPS);//set data rate to 250kbps
  NRF24L01.setChannel(110);//set frequency to channel 110
}

void loop() {
  schritte[0] = 105;
  //Transmit button change to the other Arduino
  delay(10);
  NRF24L01.stopListening();
  buttonState = digitalRead(button);//test for button press on this board
  if (buttonState == LOW)//button is pulled up so test for LOW
  {
    Serial.println("1");
    
    NRF24L01.write(&schritte, sizeof(schritte));//send LOW state to other Arduino board
    //flash the yellow LED to show progress
    digitalWrite(confirmLed, HIGH);
    Serial.println(schritte[0]);
    delay(100);
    digitalWrite(confirmLed, LOW);
  }

  buttonState = HIGH;//reset the button state variable

  //Receive button change from the other Arduino
  delay(10);
  NRF24L01.startListening();
  if (NRF24L01.available())//do we have transmission from other Arduino board
  {
    Serial.println("2");
    
    NRF24L01.read(&buttonState, sizeof(buttonState));//update the variable with new state
    NRF24L01.stopListening();
    Serial.println(buttonState);
  }
  if (buttonState == HIGH)//test the other Arduino's button state
  {
    digitalWrite(led, LOW);
  }
  else
  {
    flashLed();//indicate that the button was pressed on the other board
  }
  buttonState = HIGH;//reset the button state variable
}

//flash the red LED five times
void flashLed()
{
  for (int i = 0; i < 1; i++)
  {
    digitalWrite(led, HIGH);
    delay(200);
    digitalWrite(led, LOW);
    delay(200);
  }

}

Empfänger (Bühne)

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

int data[1];
boolean var = true;
//const byte addresses[][6] = {"00001", "00002"};
//const uint64_t pipe = 0xF0F0F0F0A1LL;
RF24 radio (8, 9);
byte address[] [6] = {"pipe1", "pipe2"}; // Adressen der beiden Pipes
byte rever_start = false;
const byte hal = 3;
byte schritte[1];                                 // Schritte die deer Motor (Hallsensor betätigt) zurücklegen muss
int sensor = LOW;
unsigned long startZeit;                          // Abfrage begin Sensor an Bühne
unsigned long interval = 2000;                    // Zeitabstand nachdem ein Zustand des Sensors erkannt wird
unsigned long vergangeneZeit = 0;                 // Speichervariable um die Vergangene Zeit zu speichern
/*int pin_motor_links = A1;                         // Richtungspin an H-Brücke für Links
  int pin_motor_rechts = A2;                        // Richtungspin an H-Brücke für Rechts
*/
// Statusvariablen
bool nullStellung = false;                      // Speichervariable ob Referenzfahrt erfolgt ist
bool ergebnis = false;                          // Berechnung der Schritte und Richtung beendet/gestartet
bool start = false;                           // Schritte Zaehlen beendet/gestartet

void setup() {
  radio.begin();
  //radio.openReadingPipe(1, pipe);
  radio.openWritingPipe(address[1]); // 00002
  radio.openReadingPipe(1, address[0]); // 00001
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate (RF24_250KBPS);
  radio.setChannel(110);

  radio.startListening();
  pinMode(hal, INPUT);
  Serial.begin(9600);
  //delay (2000);
}

void loop() {

  referenzFahrt();
  encoder_lesen();
  schritteZaehlen();
}

void referenzFahrt() {
  if (radio.available())
  {
    Serial.println("Data there");
    radio.read(data, 1);
    Serial.println(data[0]);
  }

  // Reverenzfahrt

  if ((data[0] == 255) && (rever_start == false) && (nullStellung == false)) {
    rever_start = true;
    Serial.println("Reverenzfahrt gestartet");
    //    digitalWrite (pin_motor_rechts, HIGH);
    //  digitalWrite (pin_motor_links, LOW);
  }
  else if ((data[0] == 254) && (rever_start == true)) {
    nullStellung = true;
    rever_start = false;
    Serial.println("Reverenzfahrt beendet");
    Serial.println (" ");
    //      digitalWrite (pin_motor_rechts, LOW);
    //        digitalWrite (pin_motor_links, LOW);
  }
}

void encoder_lesen () {
  if ((data[0] <= 48) && (data[0] > 0) && (nullStellung == true) && (ergebnis == false)) {
    //rechtsrum data[0] Schritte
    schritte[0] = data[0];
    ergebnis = true;
    // Motor
    Serial.println (schritte[0]);
    Serial.print ("  Schritte nach Rechts");
    Serial.println (" ");
  }
  else if ((data[0] >= 100) && (data[0] <= 149) && (nullStellung == true) && (ergebnis == false)) {
    //linksrum data[0] - 100 Schritte
    schritte[0] = data[0] - 100;
    ergebnis = true;
    // Motor
    Serial.println (schritte[0]);
    Serial.print ("  Schritte nach links");
    Serial.println (" ");
  }
}

void schritteZaehlen () {

  sensor = digitalRead (hal);
  startZeit = millis();
  if ((schritte[0] != 0) && (ergebnis == true)) {
    start = true;
    if ((startZeit - vergangeneZeit >= interval) && (sensor == LOW) && (start == true)) {
      vergangeneZeit = startZeit;
      schritte[0]--;
      Serial.println("Drehung start");
      Serial.println("Restl. Pos.");
      Serial.println(schritte[0]);
      radio.stopListening();
      radio.write (&schritte, sizeof (schritte));
      radio.startListening();
    }
  }
  else if ((schritte[0] == 0) && (start == true)) {
    start = false;
    ergebnis = false;
    data[0] = 0;
    Serial.println("Ziel");
    Serial.println("Drehung");
    Serial.println("beendet");
    Serial.print("daten");
    Serial.println(schritte[0]);
    radio.stopListening();
    radio.write (&schritte, sizeof (schritte));
    radio.startListening();
    delay(2000);
    Serial.println(ergebnis);
  }
}