I want to use 4 DC Motors regulated by a joystick, a servo motor commanded by a push-button, and all data sent by an nrf24l01. This is a copy of my question on another group (guessed that group).
Transmitter code:
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#define joyV A0
#define joyH A1
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
int joyposV = 512;
int joyposH = 512;
RH_NRF24 RadioDriver;
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
uint8_t motorcontrol[3];
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
void setup() {
// put your setup code here, to run once:
pinMode(3,INPUT);
if(!RadioManager.init())
motorcontrol[2] = 0;
}
void loop() {
// put your main code here, to run repeatedly:
joyposV = analogRead(joyV);
joyposH = analogRead(joyH);
if(joyposV < 460){
motorcontrol[2] = 1;
motorcontrol[0] = map(joyposV, 460, 0, 0, 255);
motorcontrol[1] = map(joyposV, 460, 0, 0, 255);
}
else if(joyposV > 564){
motorcontrol[2] = 0;
motorcontrol[0] = map(joyposV, 564, 1023, 0, 255);
motorcontrol[1] = map(joyposV, 564, 1023, 0, 255);
}
else{
motorcontrol[0] = 0;
motorcontrol[1] = 0;
motorcontrol[2] = 0;
}
if(joyposH < 460){
joyposH = map(joyposH, 460, 0, 0, 255);
motorcontrol[0] = motorcontrol[0] - joyposH;
motorcontrol[1] = motorcontrol[1] + joyposH;
if(motorcontrol[0] < 0)motorcontrol[0] = 0;
if(motorcontrol[1] > 255)motorcontrol[1] = 255;
}
else if(joyposH > 564){
joyposH = map(joyposH, 564, 1023, 0, 255);
motorcontrol[0] = motorcontrol[0] + joyposH;
motorcontrol[1] = motorcontrol[1] - joyposH;
if(motorcontrol[0] > 255)motorcontrol[0] = 255;
if(motorcontrol[1] < 0)motorcontrol[1] = 0;
}
if(motorcontrol[0] < 8)motorcontrol[0] = 0;
if(motorcontrol[1] < 8)motorcontrol[1] = 0;
if(RadioManager.sendtoWait(motorcontrol, sizeof(motorcontrol), SERVER_ADDRESS)){
uint8_t len = sizeof(buf);
uint8_t from;
}
delay(50);
}
Receiver:
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
int enA = 9;
int in1 = 14;
int in2 = 4;
int enB = 5;
int in3 = 7;
int in4 = 6;
RH_NRF24 RadioDriver;
RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);
uint8_t ReturnMessage[] = "JoyStick Data Received";
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
void setup() {
// put your setup code here, to run once:
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(RadioManager.available()){
uint8_t len = sizeof(buf);
uint8_t from;
if(RadioManager.recvfromAck(buf, &len,&from)){
if(buf[2] == 1){
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
else{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
analogWrite(enA, buf[1]);
analogWrite(enB, buf[0]);
}
}
}