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));
}