I wanna control the servo motor to rotate as i press the button from the transmitter via 2 NRF24L01 connection.
However, the result is that:
- Serial monitor from the receiver showed ON, OFF message randomly even I switched in OFF mode.
- As the receiver got msg from the transmitter of "ON", "OFF" , my servo motor won't rotate, there 2 times for rotation as i accidentally touch the antenna of the NRF24L01, i think this might be programming problem.
I tried to fix the issue for few days and also tested out the NRF24L01 connection issue and it works fine in terms of connection. (testing method: Simple nRF24L01+ 2.4GHz transceiver demo - #2 by Robin2)
And the servo motor is fine that it has it own testing method and it can rotate freely, the motor is HTD45H from Hiwonder tech, please tell me if you need those testing file.
I am a noob in coding, i am so frustrated in this, plz help, thanks
Transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <include.h>
#include <SerialServo.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
int switchpin = 2;
byte switchpinbyte;
void setup() {
Serial.begin(115200);
radio.begin();
//radio.setChannel(70);
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
//radio.setDataRate(RF24_250KBPS);
radio.stopListening();
pinMode(switchpin, INPUT);
delay(1000);
}
void loop() {
switchpin = digitalRead(2);
//Prepare data packet
byte data[] = { switchpin };
radio.write(&data, sizeof(data));
delay(100);
}
Receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <include.h>
#include <SerialServo.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
const byte pipe = 1;
byte data[1];
int switchpin;
void setup() {
Serial.begin(115200);
delay(1000);
radio.begin();
//radio.setChannel(70);
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
//radio.setDataRate(RF24_250KBPS);
radio.startListening();
}
void loop()
{
if (radio.available()) //keep checking on each loop to see if any data comes in
{
radio.read(&data, sizeof(data));
switchpin = data[1];
if (switchpin == 0) {
LobotSerialServoMove(Serial, ID_ALL, 0, 500);
Serial.println("Switch off");
delay(100);
}
else if (switchpin == 1) {
LobotSerialServoMove(Serial, ID_ALL, 1000, 500);
Serial.println("Switch on");
delay(100);
}
}
}
My code in zip format:
Controller and receiver problem zip.zip (761.4 KB)
Transmitter:
Receiver:
Whole setup:
The result from the receiver: