Help me please , Im so lost..

Hello great community !
I have a problem with my project. I want to control an rc car using arduino , accelerometer adxl345 , 2 wireless modules NRF24L01+ with antenna and L298N motor.

I have 2 files , one for transmitting the values of accelerometer to the radio to communicate with the car and one for receiving data of the accelerometer.
The problem is that i see the communication works , because when I turn on the car it doesnt move , but when i power the transmitter arduino it automatically starts moving… Well I just can’t figure out what to do in order to control the car using the accelerometer , the car only moves forward and never stops unless i power off the car. And if i power off the transmitter the car still moving !??

So this is the code for transmitting arduino :

#include <ADXL345.h>
#include <SPI.h>        //SPI library for communicate with the nRF24L01+
#include "RF24.h"       //The main library of the nRF24L01+
#include <Wire.h>


// Accelerometer ADXL345
#define ACC (0xA7>>1)    //ADXL345 ACC address
#define A_TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)

void initAcc() {
//Turning on the ADXL345
writeTo(ACC, 0x2D, 1<<3);      
writeTo(ACC, 0x31, 0x0B);
//by default the device is in +-2g range reading
}

void getAccelerometerData(int * result) {
int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
byte buff[A_TO_READ];
readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
//thus we are converting both bytes in to one int
result[0] = (((int)buff[1]) << 8) | buff[0];   
result[1] = (((int)buff[3])<< 8) | buff[2];

}
//Define object from RF24 library - 9 and 10 are a digital pin numbers to which signals CE and CSN are connected.
RF24 radio(9,10);

//Create a pipe addresses for the communicate                                    
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void){
  Serial.begin(9600);
  Wire.begin();
  initAcc();
  radio.begin();                 //Start the nRF24 communicate     
  radio.openWritingPipe(pipe);   //Sets the address of the receiver to which the program will send data.
  
}

void loop(void){
  
int hx,hy;
int acc[2];
getAccelerometerData(acc);
hx = acc[0];
hy = acc[1];
 Serial.println();
Serial.print(" X=");
Serial.print(hx);
 Serial.println();
Serial.print(" Y=");
Serial.print(hy);
delay(50); 

  acc[0] = map(hx, -17000, 17000, 300, 400 ); //Send X axis data
  acc[1] = map(hy, -17000, 17000, 100, 200);  //Send Y axis data
  radio.write(acc, sizeof(acc));
  
}

//---------------- Functions
//Writes val to address register on ACC
void writeTo(int DEVICE, byte address, byte val) {
 Wire.beginTransmission(DEVICE); //start transmission to ACC 
 Wire.write(address);        // send register address
 Wire.write(val);        // send value to write
 Wire.endTransmission(); //end transmission
}
//reads num bytes starting from address register on ACC in to buff array
void readFrom(int DEVICE, byte address, int num, byte buff[]) {
Wire.beginTransmission(DEVICE); //start transmission to ACC 
Wire.write(address);        //sends address to read from
Wire.endTransmission(); //end transmission
 
Wire.beginTransmission(DEVICE); //start transmission to ACC
Wire.requestFrom(DEVICE, num);    // request 6 bytes from ACC
int i = 0;
while(Wire.available())    //ACC may send less than requested (abnormal)
{ 
  buff[i] = Wire.read(); // receive a byte
  i++;
}
   Wire.endTransmission(); //end transmission
}

And this is the code of the arduino for the car :

#include <SPI.h>      //SPI library for communicate with the nRF24L01+
#include "RF24.h"     //The main library of the nRF24L01+

/* NRF PINS
 *  CE - PIN 9
 *  CSN - PIN 10
 *  SCK - PIN 13
 *  MOSI - PIN 11
 *  MISO - PIN 12
 */
 

//Define enable pins of the Motors
const int enbA = 3;
const int enbB = 5;

//Define control pins of the Motors
//If the motors rotate in the opposite direction, you can change the positions of the following pin numbers
const int IN1 = 2;    //Right Motor (-)
const int IN2 = 4;    //Right Motor (+)
const int IN3 = 7;    //Left Motor (+)
const int IN4 = 6;    //Right Motor (-)

//Define variable for the motors speeds
//I have defined a variable for each of the two motors 
//This way you can synchronize the rotation speed difference between the two motors
int RightSpd = 255;
int LeftSpd = 255;

//Define packet for the direction (X axis and Y axis)
int acc[2];

//Define object from RF24 library - 9 and 10 are a digital pin numbers to which signals CE and CSN are connected
RF24 radio(9,10);

//Create a pipe addresses for the communicate
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(){
  //Define the motor pins as OUTPUT
  pinMode(enbA, OUTPUT);
  pinMode(enbB, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  
  Serial.begin(9600);
  radio.begin();                    //Start the nRF24 communicate            
  radio.openReadingPipe(1, pipe);   //Sets the address of the transmitter to which the program will receive data.
  radio.startListening();             
  }

void loop(){
  if (radio.available()){
    radio.read(acc, sizeof(acc));

    if(acc[0] > 380){
      //forward            
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
    }
    
    if(acc[0] < 310){
      //backward              
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
    }
     
    if(acc[1] > 180){
      //left
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
    }

    if(acc[1] < 110){
      //right
      analogWrite(enbA, RightSpd);
      analogWrite(enbB, LeftSpd);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
    }

    if(acc[0] > 330 && acc[0] < 360 && acc[1] > 130 && acc[1] < 160){
      //stop car
      analogWrite(enbA, 0);
      analogWrite(enbB, 0);
    }
  }
}

And i saw another problem , i guess , if I remove the pin MISO (12) from the NRF24L01 board from the receiver , the car starts moving , but if i plug back the pin , i hear something like , motors ticking but doesnt move… LOL
Help ??

I’m lost too - how do you control a car with an accelerometer ? - that measures the cars acceleration?

Another prize example of writing a complex program and then testing it rather than testing EACH step to see if it works as expected and if not fixing the bugs.

If not the case, at what point did you discover the problem?

Paul

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

And, as @Paul_KD7HB, has said, programs should be developed in little pieces with testing at each stage before moving on to the next. Have a look at Planning and Implementing a Program

...R

Everythings works except the NRF24L01 module wifi .... I need help to figure out how to fully communicate with my 2 arduino's.

My accelerometer gives me correct distances from X and Y axys so its good , but i think it doesnt receive correcly the data. That's all i need to figure out

So i need a quick check-up for the code please

quote: "My accelerometer gives me correct distances from X and Y axys so its good , but i think it doesnt receive correcly the data. That's all i need to figure out"..

Then you are just guessing. Take time to find a way to determine exactly what is being received and if it is what you expect. Then you or someone will be able to help fix the problem.

Paul

Horo1:
Everythings works except the NRF24L01 module wifi …
I need help to figure out how to fully communicate with my 2 arduino’s.

An nRF24 is not WiFi.

Have you tried the examples in the link I gave you at the top of Reply #3?

…R

Hi, Have you got some very simple code that just tests the NRF link, forget the accel and motor control. The link that @Robin2 has provided is very good, and has sample code to test your NRF link.

Tom... :)