1

i can't figure this out, i have a task to get data from RX module that led has turned on. The Idea. I press push button on TX part, and led diode turn on, on RX part. Then RX send's data to TX that led have been turn'd on and print "LED HAVE BEEN TURNED ON". I have made turn on part, but i struggle with sending from rx to tx. When i add this part code just stops

 while (!radio.available()); {
   radio.read(&buttonState, sizeof(buttonState));
   if (buttonState == 0) {
     Serial.print("LED HAS BEEN TURNED ON");
     Serial.print("\n");
//RX
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define led1 6
#define mot 5

int buttonState = 0;

bool ledbutton = 0;

RF24 radio(9, 8);
const byte address[][6] = {"00001", "00002"};

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

  pinMode(led1, OUTPUT);
  digitalWrite(led1, HIGH);

  pinMode(mot, OUTPUT);
  analogWrite(mot, HIGH);

  radio.begin();
  radio.setChannel(80);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_HIGH);

  radio.openWritingPipe(address[0]); // 00001
  radio.openReadingPipe(1, address[1]); // 00002

  radio.setPALevel(RF24_PA_MIN);

}
void loop() {
  radio.startListening();
  if ( radio.available()) {
    while (!radio.available()); {
      radio.read(&buttonState, sizeof(buttonState));

      ledON();
      motON();
    }
    radio.stopListening();
    ledbutton = digitalRead(led1);
    radio.write(&ledbutton, sizeof(ledbutton));
  }
}

void ledON() {
  if (buttonState == 1) {
    digitalWrite(led1, LOW);
  }
  else  if (buttonState == 0) {
    digitalWrite(led1, HIGH);
    Serial.print("LED ON");
    Serial.print("\n");
  }

}

void motON() {
  if (buttonState == 3) {
    analogWrite(mot, 0);
  }
  else  if (buttonState == 2) {
    analogWrite(mot, 255);
    Serial.print("MOTOR ON");
    Serial.print("\n");
  }

}
//TX
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define buttonPin1 7
#define buttonPin2 4

int buttonState1 = 0;
int buttonState2 = 0;

int ledbutton = 0;

RF24 radio(9, 8);
const byte address[][6] = {"00001", "00002"};

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);

  Serial.begin(9600);

  radio.begin();
  radio.setChannel(80);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_HIGH);

  radio.openWritingPipe(address[1]); // 00002
  radio.openReadingPipe(1, address[0]); // 00001

}
void loop() {
  radio.stopListening();
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  LedControl();
  MotorControl();

  radio.write(&buttonState1, sizeof(buttonState1));
  radio.write(&buttonState2, sizeof(buttonState2));

  delay(5);
  radio.startListening();
  while (!radio.available()); {
    radio.read(&ledbutton, sizeof(ledbutton));
    if (ledbutton = 0) {
      Serial.print("LED HAS BEEN TURNED ON");
      Serial.print("\n");
    }
  }
}


void LedControl() {
  if (buttonState1 == 1) {
    buttonState1 = 1;
  }
  else  if (buttonState1 == 0) {
    buttonState1 = 0;
  }
  Serial.print(buttonState1);
  Serial.print("\n");
}
void MotorControl() {
  if (buttonState2 == 1) {
    buttonState2 = 3;
  }
  else  if (buttonState2 == 0) {
    buttonState2 = 2;
  }
  Serial.print(buttonState2);
  Serial.print("\n");
}
1
  • 1
    while (!radio.available()); {. Why do a while loop, when you just checked that there is radio-data available? Also, using a semi-colon and following that with a curly-bracket is terrible. I'm not sure what it is you want to do here. I think you could remove that line entirely (including the closing bracket 3 lines below).
    – Gerben
    Jan 2, 2021 at 17:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.