Hello I was wandering if anyone could help me with a remote control car that I was making. I used pieces from other codes and when I got to testing it didn't work. Pls help.
Here is the code for the controller:
#include <ServoTimer2.h>
#include <SPI.h>
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
// Define the Joystick Connections
int joyY(0);
int joyX(1);
/*
int joyposY = 512; //to use with the actual joystick
int joyposX = 512;
*/
int valY;
int valX;
// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
// Sets the radio driver to NRF24 and the client address to 1
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
ServoTimer2 myservo1;
ServoTimer2 myservo2;
//******************************************************************************
void setup() {
// Setup Serial Monitor
Serial.begin(9600);
// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
myservo1.attach(3);
myservo2.attach(4);
}
//******************************************************************************
void loop() {
if (valX < 460) // turn left
{
valY = analogRead(joyY);
valY = map(valY, 0, 1023, 0, 180);
valX = analogRead(joyX);
valX = map(valX, 0, 1023, 0, 90);
myservo1.write(valY - 2 * valX); // servo 1 slows
myservo2.write(180 - valY);
delay(15);
}
else if (valX > 564) // turn right
{
valY = analogRead(joyY);
valY = map(valY, 0, 1023, 0, 180);
valX = analogRead(joyX);
valX = map(valX, 0, 1023, 0, 90);
myservo1.write(valY);
myservo2.write((180 - valY) - 2 * valX); // servo 2 slows
delay(15);
}
}
//******************************************************************************
And here is the car code:
#include <ServoTimer2.h>
#include <SPI.h>
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
// Define the Joystick Connections
int joyY(0);
int joyX(1);
/*
int joyposY = 512; //to use with the actual joystick
int joyposX = 512;
*/
int valY;
int valX;
// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
// Sets the radio driver to NRF24 and the client address to 1
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
ServoTimer2 myservo1;
ServoTimer2 myservo2;
//******************************************************************************
void setup() {
// Setup Serial Monitor
Serial.begin(9600);
// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
myservo1.attach(3);
myservo2.attach(4);
}
//******************************************************************************
void loop() {
if (valX < 460) // turn left
{
valY = analogRead(joyY);
valY = map(valY, 0, 1023, 0, 180);
valX = analogRead(joyX);
valX = map(valX, 0, 1023, 0, 90);
myservo1.write(valY - 2 * valX); // servo 1 slows
myservo2.write(180 - valY);
delay(15);
}
else if (valX > 564) // turn right
{
valY = analogRead(joyY);
valY = map(valY, 0, 1023, 0, 180);
valX = analogRead(joyX);
valX = map(valX, 0, 1023, 0, 90);
myservo1.write(valY);
myservo2.write((180 - valY) - 2 * valX); // servo 2 slows
delay(15);
}
}
//******************************************************************************
Pls help I have been trying to get it ti work for ages and I have got no results. I am using two nRF24LO1s , two DSO4-NFC servos , and two arduino nanos. I have the joystick to control it swapped out with two 10K potentiometers until it arrives.
Thank you!
Jethro Crooke