I have 2 10k potentiometers nrf24l01 (working with 5v with adapter) connected to my 1 arduino uno circuit. I want to command 1 servo and 1 dc motor on the other arduino with these. but i think there is an error in my transmitter code it is not working. how can i solve it please help.
transmitter;
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
const uint64_t my_radio_pipe = 0xE8E8F0F0E1LL;
RF24 radio(9, 10);
struct Data_to_be_sent {
byte ch1;
byte ch2;
};
Data_to_be_sent sent_data;
void setup() {
radio.begin();
radio.openWritingPipe(my_radio_pipe);
Serial.begin(9600);
}
void loop() {
sent_data.ch1 = map(analogRead(A0), 0, 1024, 0, 255);
sent_data.ch2 = map(analogRead(A1), 0, 1024, 0, 255);
if (radio.write(&sent_data, sizeof(Data_to_be_sent))) {
Serial.println("Data Sent Successfully");
} else {
Serial.println("Data Sending Failed");
}
delay(10);
}
receiver;
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
const uint64_t my_radio_pipe = 0xE8E8F0F0E1LL;
RF24 radio(9, 10);
Servo myservo;
#define in1 3
#define in2 4
#define enA 5
int motorspeed = 0;
struct Received_data {
byte ch1;
byte ch2;
};
Received_data received_data;
int ch1_value = 0;
int ch2_value = 0;
void reset_the_Data() {
received_data.ch1 = 128;
received_data.ch2 = 0;
}
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enA, OUTPUT);
analogWrite(enA, motorspeed);
myservo.attach(2);
reset_the_Data();
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, my_radio_pipe);
radio.startListening();
}
unsigned long lastRecvTime = 0;
void receive_the_data() {
while (radio.available()) {
radio.read(&received_data, sizeof(Received_data));
lastRecvTime = millis();
}
}
void loop() {
receive_the_data();
unsigned long now = millis();
// Veri alındıktan sonra 1 saniye boyunca başka veri alınamazsa verilerin sıfırlanması.
if (now - lastRecvTime > 1000) {
reset_the_Data();
}
ch1_value = map(received_data.ch1, 0, 255, -127, 127); // İletilen verilerin aralığı değiştirildi (-127 ila 127).
ch2_value = map(received_data.ch2, 0, 255, 0, 180);
myservo.write(ch2_value);
if (ch1_value < -10) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
motorspeed = map(ch1_value, -127, 0, 0, 255);
analogWrite(enA, motorspeed);
} else if (ch1_value > 10) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
motorspeed = map(ch1_value, 0, 127, 0, 255);
analogWrite(enA, motorspeed);
} else {
motorspeed = 0;
analogWrite(enA, motorspeed);
}
Serial.print("Ch1: ");
Serial.print(ch1_value);
Serial.print(" | Ch2: ");
Serial.println(ch2_value);
delay(10);
}