Hello everyone? I am working on a terrain robot. Driven car up to 1 km. I can do this with the following code. I have used Nrf24l01. As an added feature, I wanted to shoot video with the esp32cam. I wanted to control this camera by installing a servo motor under this camera. Esp32 cam part works fine. Whenever I "attach" the servo motor, my car just stops. My car, which can normally go forward, right, left and backward, only goes forward when I attach the servo motor. other functions are lost. The car's right, left and reverse functions are lost. Even when trying to send the car back, the servo motor sometimes starts to vibrate. As if its value goes to the servo motor.
I note that I do this through the remote control. I also used nrf24l01 in the remote.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h> //To create PWM signals we need this lybrary
const uint64_t pipeIn = 0xE8E8F0F0E1LL; //Remember that this code is the same as in the transmitter
RF24 radio(5, 6); //CSN and CE pins
int m1a = 3;
int m1b = 4;
int m2a = 7;
int m2b = 8;
int ena = 9;
int enb = 10;
// The sizeof this struct should not exceed 32 bytes
struct Received_data {
byte ch1;
byte ch2;
byte ch3;
byte ch4;
byte bt1;
};
Received_data received_data;
Servo ch_3_esp_servo;
int ch1_value = 0;
int ch2_value = 0;
int ch3_value = 0;
int ch4_value = 0;
int s_sw_value = 1;
void reset_the_Data()
{
// 'safe' values to use when NO radio input is detected
received_data.ch1 = 127; //Throttle (channel 1) to 0
received_data.ch2 = 127;
received_data.ch3 = 130;
received_data.ch4 = 127;
received_data.bt1 = 1;
}
/******************/
void setup()
{
Serial.begin(9600);
ch_3_esp_servo.attach(2);
ch_3_esp_servo.write(90);
pinMode(m1a, OUTPUT);
pinMode(m1b, OUTPUT);
pinMode(m2a, OUTPUT);
pinMode(m2b, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
//We reset the received values
reset_the_Data();
//Once again, begin and radio configuration
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1,pipeIn);
//We start the radio comunication
radio.startListening();
}
/******************/
unsigned long lastRecvTime = 0;
//We create the function that will read the data each certain time
void receive_the_data()
{
while ( radio.available() ) {
radio.read(&received_data, sizeof(Received_data));
lastRecvTime = millis(); //Here we receive the data
}
}
/******************/
void loop()
{
//Receive the radio data
receive_the_data();
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
// signal lost?
reset_the_Data();
//Go up and change the initial values if you want depending on
//your aplications. Put 0 for throttle in case of drones so it won't
//fly away
}
ch1_value = received_data.ch1;
ch2_value = received_data.ch2;
ch3_value = map(received_data.ch3,0,255,0,180);
//ch4_value = received_data.ch4;
//s_sw_value = received_data.bt1;
ch_3_esp_servo.write(ch3_value);
if (ch1_value > 135)
{
//Front
analogWrite(ena,255);
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
analogWrite(enb, 255);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
}
else if (ch1_value < 115 )
{
//Back
analogWrite(ena,150);
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
analogWrite(enb,150);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
}
else if (ch2_value > 135)
{
//Left
analogWrite(ena,120);
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
analogWrite(enb,120);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
else if (ch2_value < 115)
{//Right
analogWrite(ena,120);
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
analogWrite(enb,120);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
}
else if(ch1_value>123 || ch1_value<128){
//stop
analogWrite(ena,0);
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
analogWrite(enb,0);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
Serial.println(ch1_value);
Serial.println(ch2_value);
Serial.println(ch3_value);
Serial.println();
delay(600);
}
This is my receiver code.
#include <SPI.h>
#include <nRF24L01.h> //Remember to isntall this bibrry: http://www.electronoobs.com/engarduino_NRF24_lib.php
#include <RF24.h>
int sw=2;
const uint64_t my_radio_pipe = 0xE8E8F0F0E1LL; //Remember that this code should be the same for the receiver
RF24 radio(5, 6); //Set CE and CSN pins
// The sizeof this struct should not exceed 32 bytes
struct Data_to_be_sent {
byte ch1;
byte ch2;
byte ch3;
byte ch4;
byte bt1;
};
//Create a variable with the structure above and name it sent_data
Data_to_be_sent sent_data;
void setup()
{
pinMode(sw,INPUT_PULLUP);
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(my_radio_pipe);
//Reset each channel value
sent_data.ch1 = 127;
sent_data.ch2 = 127;
sent_data.ch3 = 130;
sent_data.ch4 = 127;
sent_data.bt1 = 1;
}
/******************/
void loop()
{
sent_data.ch1 = map( analogRead(A0), 0, 1024, 0, 255);
sent_data.ch2 = map( analogRead(A1), 0, 1024, 0, 255);
sent_data.ch3 = map( analogRead(A2), 0, 1024, 0, 255);
// sent_data.ch4 = map( analogRead(A3), 0, 1024, 0, 255);
//sent_data.bt1 = digitalRead(sw);
radio.write(&sent_data, sizeof(Data_to_be_sent));
}
This is my transmitter code.