Hello, i am working on a simple project that is a remote controlled car and i encountered with a problem. I am using two 6v 250 rpm motors, to control them a L298N motor driver, to power up the driver an 11.1v battery and nrf24l01 module to get information from remote controller. Arduino uno is connected to the power seperately. Now, the problem is:
when i use this code and get data from remote controller, left motor turns slower than the right motor. The code is unfinished and I just wanted to try only the positive Y axis of the joystick.
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// radio
#define CE_PIN 7
#define CSN_PIN 8
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
int joystick[3];
int mapX,mapY;
int SW_state=0;
int xPosition ;
int yPosition ;
// radio end
#define rightMotorSpeed 6
#define leftMotorSpeed 2
#define rightMotorFwd 5
#define rightMotorBwd 4
#define leftMotorFwd 1
#define leftMotorBwd 3
int actualX;
int actualY;
void setup() {
Serial.begin(9600);
pinMode(rightMotorSpeed, OUTPUT);
pinMode(leftMotorSpeed, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorBwd, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorBwd, OUTPUT);
// radio
radio.setPALevel(RF24_PA_MIN);
radio.begin();
radio.openReadingPipe(1, pipe);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
//radio end
}
void loop() {
// radio
if ( radio.available())
{
radio.read( joystick, sizeof(joystick) );
int xpos=joystick[0];
int ypos=joystick[1];
int button = joystick[2];
xPosition = analogRead(xpos);
yPosition = analogRead(ypos);
SW_state = digitalRead(button);
mapX = map(joystick[0], 0, 1023, -512, 512);
mapY = map(joystick[1], 0, 1023, -512, 512);
Serial.print("RX: ");
Serial.print(mapX);
Serial.print(" | RY: ");
Serial.print(mapY);
Serial.print(" | Button: ");
Serial.println(joystick[2]);
//radio end
actualX = constrain(xpos,0,255);
actualY = constrain(mapY,0,255);
if(mapY>= 50 ){
forward();
}
}
void forward(){
digitalWrite(rightMotorFwd,1);
digitalWrite(rightMotorBwd,0);
digitalWrite(leftMotorFwd,1);
digitalWrite(leftMotorBwd,0);
Serial.print(actualY);
analogWrite(leftMotorSpeed,actualY);
analogWrite(rightMotorSpeed,actualY);
}
On the other hand, when i use this code, two motors works at equal speed and fine.
#include <SPI.h>
#define rightMotorSpeed 6
#define leftMotorSpeed 2
#define rightMotorFwd 5
#define rightMotorBwd 4
#define leftMotorFwd 1
#define leftMotorBwd 3
void setup() {
Serial.begin(9600);
pinMode(rightMotorSpeed, OUTPUT);
pinMode(leftMotorSpeed, OUTPUT);
pinMode(rightMotorFwd, OUTPUT);
pinMode(rightMotorBwd, OUTPUT);
pinMode(rightMotorSpeed, OUTPUT);
pinMode(leftMotorFwd, OUTPUT);
pinMode(leftMotorBwd, OUTPUT);
}
void loop() {
digitalWrite(rightMotorFwd,1);
digitalWrite(rightMotorBwd,0);
digitalWrite(leftMotorFwd,1);
digitalWrite(leftMotorBwd,0);
analogWrite(leftMotorSpeed,255);
analogWrite(rightMotorSpeed,255);
}