Controlling Servo Motor and Stepper Motor with NRF24L01

Hi Guys! I am a beginner in Arduino IDE and I am facing the below problem:

I am trying to control a Servo Motor along with a Stepper Motor with NRF24L01. using potentiometer for Servo Motor and JoystickX axis for Stepper Motor.
the Servo Motor is responding appropriately. However, there is an issue with Stepper Motor.
when I use while() to create a dead Zone, the motor keeps moving in one direction with a normal speed. without responding to the joystick when I want to shift the opposite direction. but when I change the while() with if() without changing anything else, the motor responds to both directions but I barely can notice its speed as it is too much slow.
any one can assist please?

receiver codes :
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#include <Stepper.h>

RF24 radio (9, 10);
const byte address[6] = {"00001"};
Servo MyServo;
Stepper MyStepper (2048, 2, 4, 3, 5);
struct DataPacket{
int potValue;
int joyValue;
};
DataPacket receivedData;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_HIGH);
radio.startListening();
MyServo.attach(7);
}

void loop() {
if(radio.available()){
radio.read(&receivedData, sizeof(receivedData));
int potValue = receivedData.potValue;
int joyValue = receivedData.joyValue;

int mappedPot = map(potValue, 0, 1023, 0, 180);
MyServo.write(mappedPot);// move the servo motor according to received potvalue.

int Speed = map(abs(joyValue), 0, 1023, 5, 20);
if(joyValue < 508 || joyValue > 550){
if(joyValue < 508){
MyStepper.setSpeed(Speed);
MyStepper.step(-1);
}else if(joyValue > 550){
MyStepper.setSpeed(Speed);
MyStepper.step(1);
}
radio.read(&receivedData, sizeof(receivedData));
}
}
}

Sender Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio (9, 10);
int potentiometer = A1;
int joystickX = A4;

struct DataPacket{
int potValue;
int joyValue;
};
DataPacket dataToSend;

const byte address[6] = {"00001"};

void setup(){
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();

}

void loop() {

dataToSend.potValue = analogRead(potentiometer);// potentiometer value
dataToSend.joyValue = analogRead(joystickX);// joystickX value.

radio.write(&dataToSend, sizeof(dataToSend));// send value of joystickX
Serial.println("joyValue: ");
Serial.println(dataToSend.joyValue);
Serial.println("potValue: ");
Serial.println(dataToSend.potValue);
radio.write(&dataToSend, sizeof(dataToSend));

}

Welcome!
You make it very difficult as you do not post enough information or post the code in the improper format.

That could be a very interesting project that you could have lot of fun with!

Your problem is not unexpected, and your wiring may be the root cause. Since hardware is involved, it’s crucial to provide an accurate, annotated schematic of your circuit as it is currently wired. Please note that Fritzing diagrams are not considered proper schematics; they are wiring diagrams and are often not helpful for troubleshooting.

What to Include:

  1. Annotated Schematic: Show all connections, including power, ground, and power sources. This helps us understand how your circuit is set up and identify any potential issues.
  2. Technical Information Links: Provide links to technical documentation for each hardware device used in your setup. Avoid links to sales sites like Amazon, as they usually lack the necessary technical details. We need complete specifications to help you effectively.
  3. Additional Information Needed: If the above details are incorrect, more information is required. Tell us what hardware and software you are using, the format of any data (like map data), and how your system determines its position. For example, if your project involves a robot, describe how it navigates and what computers are involved.
  4. Code Without code we cannot see the interrelationship between the code and hardware.

Why This Matters:

We have no way of knowing the specifics of your setup unless you provide that information. Clear and detailed descriptions enable us to offer the most accurate help possible. Without these details, it’s difficult to diagnose and solve the issues you're experiencing.

1 Like

You might want to look at this How to get the best out of this forum before you proceed any further.
We only know what you tell us, and without knowing what you have, we don't stand a chance.

This sounds like a classic case of you trying to do two things at once, but actually doing them one after the other.

The solution to this is to use a state machine as a time division multiplex program. That is you look at one task and do it, then return instantly the task is compleat. There is an example in the IDE examples page called "blink without delay", this is the basis of the solution, but it requires a step up in your programming techniques (knowledge).

There have been many ways this has been explained on this forum.

Try looking at this answer
How to write non blocking code

thank you guys! I solved the issue . My problem was about the line "MyStepper.step(-1) and (1). I changed the values to (15) and then I got a normal speed when moving to positive. as I mapped it with the joystickX. as for the negative movement I created another int Speed1 and I mapped it with the JoyValue . I did the same as mapping with "int Speed" with abs() but I changed "0, 1023, 5, 20" to "0, 1023, 20, 5" as I am initially operating with negative values and therefore 5 would be greater than 20. Now it is working fine and both are responding simultaneously :upside_down_face:

please ignore the unfinished notes in codes.

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

RF24 radio (9, 10);
const byte address[6] = {"00001"};
Servo MyServo;
Stepper MyStepper (2048, 2, 4, 3, 5);
struct DataPacket{
  int potValue;
  int joyValue;
};
DataPacket receivedData;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_HIGH);
radio.startListening();
MyServo.attach(7);
}

void loop() {
if(radio.available()){
  radio.read(&receivedData, sizeof(receivedData));
  int potValue = receivedData.potValue;
  int joyValue = receivedData.joyValue;

  int mappedPot = map(potValue, 0, 1023, 0, 180);
  MyServo.write(mappedPot);// move the servo motor according to received potvalue.
  
  int Speed = map(abs(joyValue), 0, 1023, 5, 20);
  int speed = map(abs(joyValue),0, 1023, 20, 5);
  if(joyValue < 508 || joyValue > 550){
  if(joyValue < 508){
  MyStepper.setSpeed(Speed);
  MyStepper.step(-15);
  }else if(joyValue > 550){
  MyStepper.setSpeed(abs(speed));
  MyStepper.step(15);
  }
  radio.read(&receivedData, sizeof(receivedData)); 
}
}
}




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



RF24 radio (9, 10);
int potentiometer = A1;
int joystickX = A4;

struct DataPacket{
  int potValue;
  int joyValue;
};
DataPacket dataToSend;

const byte address[6] = {"00001"};



void setup(){
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_HIGH);
  radio.stopListening();

}

void loop() {



   dataToSend.potValue = analogRead(potentiometer);// potentiometer value
   dataToSend.joyValue = analogRead(joystickX);// joystickX value.
   
 radio.write(&dataToSend, sizeof(dataToSend));// send value of joystickX 
  Serial.println("joyValue: ");
  Serial.println(dataToSend.joyValue);
  Serial.println("potValue: ");
  Serial.println(dataToSend.potValue);
  radio.write(&dataToSend, sizeof(dataToSend));

  
  
 
  
 
  

  

  
  


}



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