Notifications
Clear all

Need help in fixing this code  

  RSS

Gulshan
(@gulshan)
Estimable Member
Joined: 1 year ago
Posts: 126
2020-08-14 12:13 pm  

Hi, Recently I found code by which I can(supposed to) control an Arduino RC car by using a joystick shield with NRF24l01 Modules. this code also has a code by which I can control two servo motors or Pan Tilt servo by using 4 buttons on the joystick shield. but when I tried the code, both servos are working properly using buttons on the joystick shield but the motors of the robot car are continuously running in the same direction. they are responding to the buttons on the shield to move the servo motors but they are not taking the command of the joystick on the shield. can anyone tell me how to fix this code properly?

 

Transmitter

 

/* Example: nRF24L01 
 
   1 - GND 
   2 - VCC 3.3V  
   3 - CE - Arduino pin 9
   4 - CSN - Arduino pin 10
   5 - SCK - Arduino pin 13
   6 - MOSI - Arduino pin 11
   7 - MISO - Arduino pin 12
   8 - UNUSED 
 
  You can used an Analog Joystick or 2 10K potentiometers 
 
   GND - Arduino GND 
   VCC - Arduino +5V
   X Pot - Arduino A0
   Y Pot - Arduino A1
 
/*----- Import all required Libraries -----*/
 
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
 
/*----- Declare all constant pin to be used ----*/ 
#define CE_PIN   9  // Uno= 9     // Mega = 48
#define CSN_PIN 10  // Uno= 10    // Mega = 53

const uint64_t pipe = 0xE8E8F0F0E1LL; // This is the transmit pipe to communicate the two module
RF24 radio(CE_PIN, CSN_PIN); // Activate  the Radio 
int joystick[7];  // 7 element array holding the Joystick readings

#define JOYSTICK_X A0
#define JOYSTICK_Y A1
#define JOYSTICK_UP   2
#define JOYSTICK_RIGHT  3
#define JOYSTICK_DOWN   4
#define JOYSTICK_LEFT   5

void setup()   
{
  Serial.begin(9600); /* Opening the Serial Communication */
  radio.begin();
  radio.openWritingPipe(pipe);

}//--(end setup )---
 
void loop()   /* Runs Continuously */
{
  joystick[0] = map(analogRead(JOYSTICK_X), 0, 1023, 0, 255); // Reading Analog X
  joystick[1] = map(analogRead(JOYSTICK_Y), 0, 1023, 0, 255); // Reading Analog Y 
 
  joystick[2] = digitalRead(JOYSTICK_UP); 
  joystick[3] = digitalRead(JOYSTICK_RIGHT); 
  joystick[4] = digitalRead(JOYSTICK_DOWN); 
  joystick[5] = digitalRead(JOYSTICK_LEFT); 
 
  radio.write( joystick, sizeof(joystick) );

}//--(end main loop )---


Reciever

#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #include <Servo.h> /*----- Declare all constant pin to be used ----*/ #define CE_PIN   9    //2.4GHz CE_pin of NRF24L01 module #define CSN_PIN  10    //2.4GHz CSN_pin of NRF24L01 module const uint64_t pipe = 0xE8E8F0F0E1LL; // This is the transmit pipe to communicate the two module RF24 radio(CE_PIN, CSN_PIN);        // Activate  the Radio   int joystick[7];  // 7 element array holding the Joystick readings int motorspeed; int pos,pos2,pos3,pos4;    // variable to store the servo position int ledpin = A0; const int pingPin = A4;    // Trigger pin on Analog 4 const int echoPin = A5;    // Echo pin on Analog 5 int motor1pin1 = 3;       // left motors 2 int motor1pin2 = 4;       // left motor 1 int motor2pin1 = 7;       // right motor 1 int motor2pin2 = 8;       // right motor 2 int EnableA = 5;        // enable pin for motor 1 int EnableB = 6;        // enable pin for motor 2 Servo panServo;         // the servo for panning Servo tiltServo;        // the servo for tilting void setup() { Serial.begin(9600); /* Opening the Serial Communication */ delay(1000); Serial.println("Nrf24L01 Receiver Starting"); radio.begin(); radio.setPALevel(RF24_PA_MIN); radio.setAutoAck(true); radio.openReadingPipe(1,pipe); radio.startListening(); radio.powerUp(); pinMode(motor1pin1, OUTPUT); pinMode(motor1pin2, OUTPUT); pinMode(motor2pin1, OUTPUT); pinMode(motor2pin2, OUTPUT); pinMode(EnableA, OUTPUT); pinMode(EnableB, OUTPUT);  panServo.attach(A2);      // pan servo on A2  tiltServo.attach(A3);     // tilt servo on A3  panServo.write(90);       // sets pan servo to middle  tiltServo.write(50);      // sets tilt servo to middle } void loop()   /****** MAIN LOOP: RUNS CONSTANTLY ******/ {  if (radio.available())  {    // Read the data payload until we've received everything       radio.read( joystick, sizeof(joystick) );      // if(joystick[6] == 0) { autobot(); } //else { nrf24l01();  }       nrf24l01();    // all the joystick events in this function   }  else  {          Serial.println("No radio available");      halt();     // if no radio transmission is received, the car stops  } }//--(end main loop )--- void nrf24l01() {      radio.powerUp();      Serial.print("Y =");      Serial.print(joystick[0]);      Serial.print("\tX =");            Serial.print(joystick[1]);      // motors for moving Forward or Backward      if(joystick[0] >65) { motorspeed=joystick[0]+134; forward(); }      if(joystick[0] <55) { motorspeed=255-joystick[0]; backward(); }      if(joystick[0] >=55 && joystick[0] <=65) { halt();  motorspeed=0;}      Serial.print("\tY-Speed: ");            Serial.print(motorspeed);          // motors for moving Left or Right      if(joystick[1] >60) { motorspeed = 255; turnleft(); }      if(joystick[1] <=55) { motorspeed = 255; turnright(); }      if(joystick[1] >=60 && joystick[1] <=65) { halt(); motorspeed=0; }      Serial.print("\tX-Speed: ");            Serial.print(motorspeed);      // Operates the pan servo turning right      Serial.print("\tUp = ");            Serial.print(joystick[3]);      if(joystick[3] == HIGH) {        pos = panServo.read();              panServo.write(pos + 1);      }      // Operates the pan servo turning left      Serial.print("\tDown = ");            Serial.print(joystick[5]);      if(joystick[5] == HIGH) {        pos2 = panServo.read();              panServo.write(pos2 - 1);      }      // Operates the tilt servo turning up      Serial.print("\tRight = ");            Serial.print(joystick[2]);      if(joystick[2] == HIGH) {        pos3 = tiltServo.read();              tiltServo.write(pos3 + 1);      }                  // Operates the tilt servo turning down      Serial.print("\tLeft = ");            Serial.println(joystick[4]);      if(joystick[4] == HIGH) {        pos2 = tiltServo.read();              tiltServo.write(pos2 - 1);      } } void backward() {   analogWrite(EnableA, motorspeed);   analogWrite(EnableB, motorspeed);   digitalWrite(motor1pin1, HIGH);   digitalWrite(motor1pin2, LOW);   digitalWrite(motor2pin1, HIGH);   digitalWrite(motor2pin2, LOW); } void forward() {   analogWrite(EnableA, motorspeed);   analogWrite(EnableB, motorspeed);   digitalWrite(motor1pin1, LOW);   digitalWrite(motor1pin2, HIGH);   digitalWrite(motor2pin1, LOW);   digitalWrite(motor2pin2, HIGH); } void halt() {       // stops all motors   digitalWrite(motor1pin1, LOW);   digitalWrite(motor1pin2, LOW);   digitalWrite(motor2pin1, LOW);   digitalWrite(motor2pin2, LOW); } void turnleft() {   digitalWrite(EnableA, HIGH);   digitalWrite(EnableB, HIGH);   digitalWrite(motor1pin1, HIGH);   digitalWrite(motor1pin2, LOW);   digitalWrite(motor2pin1, LOW);   digitalWrite(motor2pin2, HIGH); } void turnright() {   digitalWrite(EnableA, HIGH);   digitalWrite(EnableB, HIGH);   digitalWrite(motor1pin1, LOW);   digitalWrite(motor1pin2, HIGH);   digitalWrite(motor2pin1, HIGH);   digitalWrite(motor2pin2, LOW); }                                              

JoeLyddon liked
Quote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 342
2020-08-14 7:40 pm  
Posted by: @gulshan

Hi, Recently I found code by which I can(supposed to) control an Arduino RC car by using a joystick shield with NRF24l01 Modules. this code also has a code by which I can control two servo motors or Pan Tilt servo by using 4 buttons on the joystick shield. but when I tried the code, both servos are working properly using buttons on the joystick shield but the motors of the robot car are continuously running in the same direction. they are responding to the buttons on the shield to move the servo motors but they are not taking the command of the joystick on the shield. can anyone tell me how to fix this code properly?

 

Transmitter

 

/* Example: nRF24L01 
 
   1 - GND 
   2 - VCC 3.3V  
   3 - CE - Arduino pin 9
   4 - CSN - Arduino pin 10
   5 - SCK - Arduino pin 13
   6 - MOSI - Arduino pin 11
   7 - MISO - Arduino pin 12
   8 - UNUSED 
 
  You can used an Analog Joystick or 2 10K potentiometers 
 
   GND - Arduino GND 
   VCC - Arduino +5V
   X Pot - Arduino A0
   Y Pot - Arduino A1
 
/*----- Import all required Libraries -----*/
 
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
 
/*----- Declare all constant pin to be used ----*/ 
#define CE_PIN   9  // Uno= 9     // Mega = 48
#define CSN_PIN 10  // Uno= 10    // Mega = 53

const uint64_t pipe = 0xE8E8F0F0E1LL; // This is the transmit pipe to communicate the two module
RF24 radio(CE_PIN, CSN_PIN); // Activate  the Radio 
int joystick[7];  // 7 element array holding the Joystick readings

#define JOYSTICK_X A0
#define JOYSTICK_Y A1
#define JOYSTICK_UP   2
#define JOYSTICK_RIGHT  3
#define JOYSTICK_DOWN   4
#define JOYSTICK_LEFT   5

void setup()   
{
  Serial.begin(9600); /* Opening the Serial Communication */
  radio.begin();
  radio.openWritingPipe(pipe);

}//--(end setup )---
 
void loop()   /* Runs Continuously */
{
  joystick[0] = map(analogRead(JOYSTICK_X), 0, 1023, 0, 255); // Reading Analog X
  joystick[1] = map(analogRead(JOYSTICK_Y), 0, 1023, 0, 255); // Reading Analog Y 
 
  joystick[2] = digitalRead(JOYSTICK_UP); 
  joystick[3] = digitalRead(JOYSTICK_RIGHT); 
  joystick[4] = digitalRead(JOYSTICK_DOWN); 
  joystick[5] = digitalRead(JOYSTICK_LEFT); 
 
  radio.write( joystick, sizeof(joystick) );

}//--(end main loop )---


Reciever

#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> #include <Servo.h> /*----- Declare all constant pin to be used ----*/ #define CE_PIN   9    //2.4GHz CE_pin of NRF24L01 module #define CSN_PIN  10    //2.4GHz CSN_pin of NRF24L01 module const uint64_t pipe = 0xE8E8F0F0E1LL; // This is the transmit pipe to communicate the two module RF24 radio(CE_PIN, CSN_PIN);        // Activate  the Radio   int joystick[7];  // 7 element array holding the Joystick readings int motorspeed; int pos,pos2,pos3,pos4;    // variable to store the servo position int ledpin = A0; const int pingPin = A4;    // Trigger pin on Analog 4 const int echoPin = A5;    // Echo pin on Analog 5 int motor1pin1 = 3;       // left motors 2 int motor1pin2 = 4;       // left motor 1 int motor2pin1 = 7;       // right motor 1 int motor2pin2 = 8;       // right motor 2 int EnableA = 5;        // enable pin for motor 1 int EnableB = 6;        // enable pin for motor 2 Servo panServo;         // the servo for panning Servo tiltServo;        // the servo for tilting void setup() { Serial.begin(9600); /* Opening the Serial Communication */ delay(1000); Serial.println("Nrf24L01 Receiver Starting"); radio.begin(); radio.setPALevel(RF24_PA_MIN); radio.setAutoAck(true); radio.openReadingPipe(1,pipe); radio.startListening(); radio.powerUp(); pinMode(motor1pin1, OUTPUT); pinMode(motor1pin2, OUTPUT); pinMode(motor2pin1, OUTPUT); pinMode(motor2pin2, OUTPUT); pinMode(EnableA, OUTPUT); pinMode(EnableB, OUTPUT);  panServo.attach(A2);      // pan servo on A2  tiltServo.attach(A3);     // tilt servo on A3  panServo.write(90);       // sets pan servo to middle  tiltServo.write(50);      // sets tilt servo to middle } void loop()   /****** MAIN LOOP: RUNS CONSTANTLY ******/ {  if (radio.available())  {    // Read the data payload until we've received everything       radio.read( joystick, sizeof(joystick) );      // if(joystick[6] == 0) { autobot(); } //else { nrf24l01();  }       nrf24l01();    // all the joystick events in this function   }  else  {          Serial.println("No radio available");      halt();     // if no radio transmission is received, the car stops  } }//--(end main loop )--- void nrf24l01() {      radio.powerUp();      Serial.print("Y =");      Serial.print(joystick[0]);      Serial.print("\tX =");            Serial.print(joystick[1]);      // motors for moving Forward or Backward      if(joystick[0] >65) { motorspeed=joystick[0]+134; forward(); }      if(joystick[0] <55) { motorspeed=255-joystick[0]; backward(); }      if(joystick[0] >=55 && joystick[0] <=65) { halt();  motorspeed=0;}      Serial.print("\tY-Speed: ");            Serial.print(motorspeed);          // motors for moving Left or Right      if(joystick[1] >60) { motorspeed = 255; turnleft(); }      if(joystick[1] <=55) { motorspeed = 255; turnright(); }      if(joystick[1] >=60 && joystick[1] <=65) { halt(); motorspeed=0; }      Serial.print("\tX-Speed: ");            Serial.print(motorspeed);      // Operates the pan servo turning right      Serial.print("\tUp = ");            Serial.print(joystick[3]);      if(joystick[3] == HIGH) {        pos = panServo.read();              panServo.write(pos + 1);      }      // Operates the pan servo turning left      Serial.print("\tDown = ");            Serial.print(joystick[5]);      if(joystick[5] == HIGH) {        pos2 = panServo.read();              panServo.write(pos2 - 1);      }      // Operates the tilt servo turning up      Serial.print("\tRight = ");            Serial.print(joystick[2]);      if(joystick[2] == HIGH) {        pos3 = tiltServo.read();              tiltServo.write(pos3 + 1);      }                  // Operates the tilt servo turning down      Serial.print("\tLeft = ");            Serial.println(joystick[4]);      if(joystick[4] == HIGH) {        pos2 = tiltServo.read();              tiltServo.write(pos2 - 1);      } } void backward() {   analogWrite(EnableA, motorspeed);   analogWrite(EnableB, motorspeed);   digitalWrite(motor1pin1, HIGH);   digitalWrite(motor1pin2, LOW);   digitalWrite(motor2pin1, HIGH);   digitalWrite(motor2pin2, LOW); } void forward() {   analogWrite(EnableA, motorspeed);   analogWrite(EnableB, motorspeed);   digitalWrite(motor1pin1, LOW);   digitalWrite(motor1pin2, HIGH);   digitalWrite(motor2pin1, LOW);   digitalWrite(motor2pin2, HIGH); } void halt() {       // stops all motors   digitalWrite(motor1pin1, LOW);   digitalWrite(motor1pin2, LOW);   digitalWrite(motor2pin1, LOW);   digitalWrite(motor2pin2, LOW); } void turnleft() {   digitalWrite(EnableA, HIGH);   digitalWrite(EnableB, HIGH);   digitalWrite(motor1pin1, HIGH);   digitalWrite(motor1pin2, LOW);   digitalWrite(motor2pin1, LOW);   digitalWrite(motor2pin2, HIGH); } void turnright() {   digitalWrite(EnableA, HIGH);   digitalWrite(EnableB, HIGH);   digitalWrite(motor1pin1, LOW);   digitalWrite(motor1pin2, HIGH);   digitalWrite(motor2pin1, HIGH);   digitalWrite(motor2pin2, LOW); }                                              

It might be a good idea to activate the Serial Display  and insert Serial displays at various points showing Joystick values etc. just before Tansmitting the info... Doing the same thing for the Receiver... to isolate the problem.

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote