1 motor for steering 1 for motion

Hello everyone,
Thank you for viewing my issue big thank you if you help :slight_smile:

I am a beginner and I have code (from followed tutorial with unhelpful writer) for an rc car powered by 2 wheels. my problem is I have a set up where 1 motor controls forward and back and the other controls steering, whereas this code uses both motors for motion and steering. And was wondering if any could help me change the code as such I’ve had no help from the post of tutorial and would be appreciated. If I've missed any needed info out do not hesitate to ask.

Joystick/Transmitter code:

// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>

// Include dependant SPI Library
#include <SPI.h>

// Define Joystick Connections
#define joyVert A0
#define joyHorz A1

// Define Joystick Values - Start at 512 (middle position)
int joyposVert = 512;
int joyposHorz = 512;

// 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);

// Declare unsigned 8-bit motorcontrol array
// 2 Bytes for motor speeds plus 1 byte for direction control
uint8_t motorcontrol[3];

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

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");

// Set initial motor direction as forward
motorcontrol[2] = 0;

}

void loop()
{
// Print to Serial Monitor
Serial.println("Reading motorcontrol values ");

// Read the Joystick X and Y positions
joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);

// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction

if (joyposVert < 460)
{
// This is Backward
// Set Motors backward
motorcontrol[2] = 1;

//Determine Motor Speeds
// As we are going backwards we need to reverse readings
motorcontrol[0] = map(joyposVert, 460, 0, 0, 255);

motorcontrol[1] = map(joyposVert, 460, 0, 0, 255);

}
else if (joyposVert > 564)
{
// This is Forward
// Set Motors forward
motorcontrol[2] = 0;

//Determine Motor Speeds
motorcontrol[0] = map(joyposVert, 564, 1023, 0, 255);
motorcontrol[1] = map(joyposVert, 564, 1023, 0, 255);

}
else
{
// This is Stopped
motorcontrol[0] = 0;
motorcontrol[1] = 0;
motorcontrol[2] = 0;

}

// Now do the steering
// The Horizontal position will "weigh" the motor speed
// Values for each motor

if (joyposHorz < 460)
{
// Move Left
// As we are going left we need to reverse readings
// Map the number to a value of 255 maximum
joyposHorz = map(joyposHorz, 460, 0, 0, 255);

motorcontrol[0] = motorcontrol[0] - joyposHorz;
motorcontrol[1] = motorcontrol[1] + joyposHorz;

// Don't exceed range of 0-255 for motor speeds
if (motorcontrol[0] < 0)motorcontrol[0] = 0;
if (motorcontrol[1] > 255)motorcontrol[1] = 255;

}
else if (joyposHorz > 564)
{
// Move Right
// Map the number to a value of 255 maximum
joyposHorz = map(joyposHorz, 564, 1023, 0, 255);

motorcontrol[0] = motorcontrol[0] + joyposHorz;
motorcontrol[1] = motorcontrol[1] - joyposHorz;

// Don't exceed range of 0-255 for motor speeds
if (motorcontrol[0] > 255)motorcontrol[0] = 255;
if (motorcontrol[1] < 0)motorcontrol[1] = 0;

}

// Adjust to prevent "buzzing" at very low speed
if (motorcontrol[0] < 8)motorcontrol[0] = 0;
if (motorcontrol[1] < 8)motorcontrol[1] = 0;

//Display the Motor Control values in the serial monitor.
Serial.print("Motor A: ");
Serial.print(motorcontrol[0]);
Serial.print(" - Motor B: ");
Serial.print(motorcontrol[1]);
Serial.print(" - Direction: ");
Serial.println(motorcontrol[2]);

//Send a message containing Motor Control data to manager_server
if (RadioManager.sendtoWait(motorcontrol, sizeof(motorcontrol), SERVER_ADDRESS))
{
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is nrf24_reliable_datagram_server running?");
}
}
else
Serial.println("sendtoWait failed");

delay(100); // Wait a bit before next transmission
}

RC Car/Receiver Code:

// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>

// Include dependant SPI Library
#include <SPI.h>

// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2

// Motor A Connections
int enA = 9;
int in1 = 3;
int in2 = 4;

// Motor B Connections
int enB = 7;
int in3 = 5;
int in4 = 6;

// Create an instance of the radio driver
RH_NRF24 RadioDriver;

// Sets the radio driver to NRF24 and the server address to 2
RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);

// Define a message to return if values received
uint8_t ReturnMessage[] = "JoyStick Data Received";

// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];

void setup()
{
// Setup Serial Monitor
Serial.begin(9600);

// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
}

void loop()
{
if (RadioManager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAck(buf, &len, &from))

{

// Serial Print the values of joystick
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": MotorA = ");
Serial.print(buf[0]);
Serial.print(" MotorB = ");
Serial.print(buf[1]);
Serial.print(" Dir = ");
Serial.println(buf[2]);

// Set Motor Direction
if (buf[2] == 1)
{
// Motors are backwards
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}else{
// Motors are forwards
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}

// Drive Motors
analogWrite(enA, buf[1]);
analogWrite(enB, buf[0]);

// Send a reply back to the originator client, check for error
if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
Serial.println("sendtoWait failed");
}
}
}

For informed help, please read and follow the directions in the "How to use this forum" post.

Please edit your post to add code tags, and explain all the problems clearly.

I didn’t see any code tags on my mobile so I just seperated it by title and a clear space, could you please explain how my problem hasn’t been written well as I spent 30 mins making sure I had all the correct info. If you’re referring to the other post I wrote a month ago which you have commented on that’s a different project with completely different script and doesn’t reference this in anyway. But if there is any specific info that is needed I’ll be happy to provide.

It’s only just occurred as I was about sleep, if I provide a schematic for wiring Would it explain my problem easier? I’d be happy to do so.

Your real problem is threefold:

  1. you copy/paste a huge chunk of someone else's code without bothering to study and understand it.

  2. you can't be bothered to read the instructions on how to get informed help.

  3. you think the following is a meaningful statement of your problem:

was wondering if any could help me change the code

We strongly recommend that beginners start with simple tutorials that come with Arduino, like blink an LED without using delay, read a voltage, a button or a sensor, etc. in order to learn the programming language and the special features of the Arduino.

Otherwise, post your request on the Gigs and Collaborations forum section, and be prepared to pay for the help.

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

Finally tell us in detail what the program does when you run it and what you want it to do that is different.

...R

It sounds like you have a completely different type of car from the one the code is for, e.g. steering probably uses a servo not differential power.

So you don't need just a few changes. The first thing you need to do is to find some code for the right type of car, understand what that does and then change that if you need to.

Steve

slipstick:
It sounds like you have a completely different type of car from the one the code is for, e.g. steering probably uses a servo not differential power.

So you don't need just a few changes. The first thing you need to do is to find some code for the right type of car, understand what that does and then change that if you need to.

Steve

Do this. Find something on Google which is closer to what you want. Find another. Compare the two to extract the good features from both.