NRF24L01 and motor control

Good afternoon everyone!

I am currently trying to combine two projects into one, the first being a basic text communications between two NRF24L01 wireless transceivers and the second one being a simple text input, motor output code. My goal is to be able to send a simple text (ex 'A3') wirelessly to the other Arduino which will power Motor A for 3 seconds. Both of my NRF send/receive codes and my Text/Motor code work when I run them independently but when trying to mix and match them together I hit trouble.

Any help is greatly appreciated! Also, is it even possible for this to work?

NRF Send Code:


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10, 9); // CE, CSN
const byte address[6] = "00001";
void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {
  const char text[] = "A1";
  radio.write(&text, sizeof(text));
  delay(5000);
}

.

NRF receive code:


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(10, 9); // CE, CSN
const byte address[6] = "00001";
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

Text/Motor code:

String input;
// Motor A

int enA = 7;
int in1 = 6;
int in2 = 5;


// Motor B

int enB = 2;
int in3 = 4;
int in4 = 3;

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();

  Serial.println("Type A or B followed by time in sec, e.g. A55 or A 55");

  input.reserve(10); // pre-allocate some space

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

}

// read Serial until until_c char found, returns true when found else false
// non-blocking, until_c is returned as last char in String, updates input String with chars read
bool readStringUntil(String& input, char until_c) {
  while (Serial.available()) {
    char c = Serial.read();
    input += c;
    if (c == until_c) {
      return true;
    }
  }
  return false;
}

void loop() {
  if (readStringUntil(input, '\n')) {
    // go a line of input
    input.trim(); // remove any \r and leading/trailing space
    input.toUpperCase(); // a -> A etc
    char AB_type = input[0];
    input.remove(0, 1); // remove the char
    input.trim();
    long time = input.toInt(); // to int actually returns a long
    if (input != String(time)) {
      // invalid time
      Serial.print(input); Serial.println(F(" not a valid time"));
    } 
    
    else {
      if (AB_type == 'A') {
        Serial.print(F("Picked A")); Serial.print(F(" with time:")); Serial.println(time);
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        analogWrite(enA, 200);
        delay (time * 1000);
        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);

      }
      
      else if (AB_type == 'B') {
        Serial.print(F("Picked B")); Serial.print(F(" with time:")); Serial.println(time);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
        analogWrite(enB, 200);
        //delay (3000);
        delay(time * 1000);
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
      } else {
        Serial.print(AB_type); Serial.println(F(" invalid type, must be A or B followed by time in sec"));
      }
    }
    input = ""; // ALWAYS clear for next read
  }


}

Okey. Describe the trouble You experience.
I'll take a look. In the mean time....
In general keep the wireless code together. Let it do its job and deliver the text, the command. Use debug print via Serial.println( received text); Than, below that code, You decode the text. Use Serial.println(decoded text);
Taking a look now...

1 Like

It's late here and the code is quite advanced. I'll postpone deeper analysis.

1 Like

No worries and thank you so much! Excited to see how this goes.

You look like using a step by step method. That's a good way to develop software.

1 Like

Thank you but all the credit goes to YouTubers and this Forum, I am very new and most of my stuff is just my remakes of others.

1 Like

You say both the radio parts and the motor control part work separately which is a good start.

It looks like your motor control code is designed to read instructions from the Serial port. You have to modify it to read from the char array in the receiver part.

1 Like

Sorry for the late reply but thank you for the catch, I will start to edit this on my free time and let you know how it turns out this weekend!

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